Skip to content

Instantly share code, notes, and snippets.

@maryrosecook
Created January 14, 2011 23:34
Show Gist options
  • Save maryrosecook/780505 to your computer and use it in GitHub Desktop.
Save maryrosecook/780505 to your computer and use it in GitHub Desktop.
For each tick of the clock
For every object in the game
Get all the other objects in the same grid square
For each other object in the same grid square
If object still wants to hear about square sharers in this tick
Tell the object there is a potential collision
If the object's bounding box overlaps the other's
Object reacts
Object indicates if it wants to hear about more square sharers
// testing whether object a and b overlap
If a's bottom right x coordinate is less than b's top left x coordinate
There is no collision
If a's top left x is greater than b's bottom right x
There is no collision
If a's top left y is greater than b's bottom right y
There is no collision
If a's bottom right y is less than b's top left y
There is no collision
For each other object B in the same grid square as object A
If object B's bounding box overlaps object A's
Add B to the list of colliding objects
If this grenade is colliding with an object
If grenade bounding box overlapping line defining left side of object
Run reverseAngle() function, passing "left" for sideHit
If grenade bounding box overlapping line defining right side of object
Run reverseAngle() function, passing "right" for sideHit
If grenade bounding box overlapping line defining bottom side of object
Run reverseAngle() function, passing "bottom" for sideHit
If grenade bounding box overlapping line defining top side of object
Run reverseAngle() function, passing "top" for sideHit
If an object moves
Remove the object from all grid square containers
Get the x and y coordinates of the object's top left corner
Determine which grid square the coordinates fall into
Add the object to that grid square container
For each of the eight surrounding grid square containers
Add the object to the container
lineLineCollision: function(p1, p2, p3, p4) {
var d = ((p4.y - p3.y) * (p2.x - p1.x)) - ((p4.x - p3.x) * (p2.y - p1.y));
var n1 = ((p4.x - p3.x) * (p1.y - p3.y)) - ((p4.y - p3.y) * (p1.x - p3.x));
var n2 = ((p2.x - p1.x) * (p1.y - p3.y)) - ((p2.y - p1.y) * (p1.x - p3.x));
if ( d == 0.0 )
{
if ( n1 == 0.0 && n2 == 0.0 )
{
return false; //COINCIDENT;
}
return false; // PARALLEL;
}
var ua = n1 / d;
var ub = n2 / d;
return (ua >= 0.0 && ua <= 1.0 && ub >= 0.0 && ub <= 1.0);
},
lineLineCollisionPoint: function(p1, p2, p3, p4) {
var d = ((p4.y - p3.y) * (p2.x - p1.x)) - ((p4.x - p3.x) * (p2.y - p1.y));
var n1 = ((p4.x - p3.x) * (p1.y - p3.y)) - ((p4.y - p3.y) * (p1.x - p3.x));
var n2 = ((p2.x - p1.x) * (p1.y - p3.y)) - ((p2.y - p1.y) * (p1.x - p3.x));
if ( d == 0.0 )
{
if ( n1 == 0.0 && n2 == 0.0 )
{
return false; //COINCIDENT;
}
return false; // PARALLEL;
}
var ua = n1 / d;
var ub = n2 / d;
x = p1.x + ua * (p2.x - p1.x);
y = p1.y + ua * (p2.y - p1.y);
return Point2D.create(x, y);
},
// Make a structure to hold all the grid squares
For each grid square
Make a grid square container
// Put all the objects in the right grid square container
For each object in the world
Get the x and y coordinates of the object's top left corner
Determine which grid square the coordinates fall into
Add the object to that grid square container
// Get all the objects that a certain object is colliding with
Get the grid square container the object is in
Return all the other objects in that grid square container
// Make a structure to hold all the grid squares
For each grid square
Make a grid square container
// Put all the objects in the right grid square container
For each object in the world
Get the x and y coordinates of the object's top left corner
Determine which grid square the coordinates fall into
Add the object to that grid square container
For each of the eight surrounding grid square containers
Add the object to the container
// Get all the objects that a certain object is colliding with
Get the grid square container the object is in
Return all the other objects in that grid square container
If line C to A is intersecting with the line defining the left side of the object
Return "left" and the point of intersection between C to A and the left line
If line C to A is intersecting with the line defining the right side of the object
Return "right" the point of intersection between C to A and the right line
If line C to A is intersecting with the line defining the bottom side of the object
Return "bottom" and the point of intersection between C to A and the bottom line
If line C to A is intersecting with the line defining the top side of the object
Return "top" and the point of intersection between C to A and the top line
// take moving object and return angle of movement in opposite direction
reverseAngle: function(movingObj, sideHit) {
var vector = movingObj.getVelocity().normalize();
var surfaceNormal = this.getSurfaceNormal(sideHit);
var d = vector.angleBetween(surfaceNormal);
return this.adjustForSide(d, sideHit);
},
getSurfaceNormal: function(side) {
if(side == "left")
return Vector2D.create(-1, 0);
else if(side == "right")
return Vector2D.create(1, 0);
else if(side == "top")
return Vector2D.create(0, -1);
else if(side == "bottom")
return Vector2D.create(0, 1);
},
adjustForSide: function(angleBetween, sideHit) {
if(sideHit == "left")
return angleBetween + 90;
else if(sideHit == "right")
return angleBetween - 90;
else if(sideHit == "top")
return angleBetween + 180;
else if(sideHit == "bottom")
return angleBetween;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment