definitions
SAT - (seperating axis theorem) used by Metanet.com for their flash game “N”. Advanced uses of this are commonly used in modern games.
hitTest - Commonly used in flash game design as a method of collision detection. Advanced use is rather slow and requires too much memory and processing speed.
This is just the Basics of SAT. All of this has been created as a result of what I have learned from the MetaNet site, and other sites such as, Kirupa, FlashKit, Tutorialized, and Actionscript.org. Everything You really need to Know about why I did what is explained below in the “SAT” function
This code is free to you to learn from and adapt to your own needs as you use it, All I ask is that if you do Find a new use for this code or ways to make this code better, Tell me. My Email Address is: FlashSquirrel@hotmail.com, or add it to this post so others can benifit by you discovery. Also When you email me, ask for a copy of my game which I used this code in. Enjoy!
downlaod the .as version of the following code:
================
SATReqs = function (guy, callThis) {
gx = _root.guy._x;
gy = _root.guy._y;
ghx = _root.guy._width/2;
ghy = _root.guy._height/2;
//
tx = callThis._x;
ty = callThis._y;
thx = callThis._width/2;
thy = callThis._height/2;
//GET ALL INFO… DONE!
if (tx<gx+40) {
if (tx>gx-40) {
if (ty<gy+40) {
if (ty>gy-40) {
cb_y = thy+ghy;
dy = Math.abs(gy-ty)-cb_y;
cb_x = thx+ghx;
dx = Math.abs(gx-tx)-cb_x;
if (dx<0) {
if (dy<0) {
_root.guy.hit = true;
} else {
_root.guy.hit = false;
}
} else {
_root.guy.hit = false;
}
}
}
}
}
};
//----------------
//----------------
//on the MovieClip you Want to test with the above code!
onLoad = function () {
hit = false;
//NOT ALWAYS NECESSARY, but it prevents the chance of any errors w/ undefined Variables.
};
onEnterFrame = function () {
SATReqs(this, wall);
if (hit) {
//Result of Collision!
}
};
//----------------
//----------------
//Variation to act as walls to a given object…
SAT = function (guy) {
gx = _root.guy._x;
gy = _root.guy._y;
ghx = _root.guy._width/2;
ghy = _root.guy._height/2;
//note: all walls in the current screen are identified under the “bounds” Array
for (tt=0; tt<bounds.length; tt++) {
//Using this allows Flash find all WAlls At once, then if they are close enough, then check if there is contact.
temp = bounds[tt];
tx = temp._x;
ty = temp._y;
thx = temp._width/2;
thy = temp._height/2;
//GET ALL INFO… DONE!
if (tx<gx+40) {
if (tx>gx-40) {
if (ty<gy+40) {
if (ty>gy-40) {
//The following code is based on the ACTUAL [seperating axis theorem] by MetaNet
// dist = squareRoot of( (X1-X2)^2 + (Y1-Y2)^2 ) [^2 means Squared]
//--------------------
//dist between the two objects according to the Y-axis, Subtracted by the Y Half-Width of the WALL[cb_y]
cb_y = thy+ghy;
dy = Math.abs(gy-ty)-cb_y;
//-------------------
//dist between the two objects according to the X-axis, Subtracted by the X Half-Width of the WALL[cb_x]
cb_x = thx+ghx;
dx = Math.abs(gx-tx)-cb_x;
//
if (dy<=0) {
if (dx<=0) {
//To understand this better, look around online for a bit of code for objects rotating to face your mouse.
//This in the similar code but applied to a different principle.
//First, It determines Radians between TEMP and GUY.
//Then converts it into degrees…
//the "/90)90" is used with the MATH.ROUND to convert the RESULT into increments of 90 degrees.
//So there is a possiblity of following directions only. (0 degrees, 90 degrees, 180 degrees, -90 degrees, -180 degrees)
myRadians = Math.atan2(temp._y-guy._y, temp._x-guy._x);
myDegrees = Math.round(Math.round((myRadians180/Math.PI))/90)*90;
// by subtracting 90 the starting point for the degrees is set for better use.
lhc = myDegrees-90;
xSpeed = Math.abs(dx)Math.sin(lhc(Math.PI/180));
ySpeed = Math.abs(dy)Math.cos(lhc(Math.PI/180));
guy._y -= ySpeed;
guy._x += xSpeed;
//The rest is used to move “guy” the correct direction and distance away from the Center of the WALL the guy is touching.
}
}
}
}
}
}
}
};
//----------------
//----------------
//on the MovieClip you Want to test with the above code!
onEnterFrame = function () {
SAT(this);
//Much Simplier! :thumb2:
};
//----------------
//----------------