Page tags

Add a new page

Collision detection is the detection between game characters and sprites and the environment around them. Note this excludes collisions between attacks and players, which would be found in the section hit detection.

I will be using the Separating Axis Theorem detailed in the link. Come on, go read it; it's explained really well and it's kind of cool.

Core Algorithm for Hit Detection

Hit detection will be done with the use of convex polygon boundaries. If an object needs to have collision detection with the player's character, the boundaries of that object needs to be defined as a polygon. This polygon is a separate object that is to be defined by c_GroundBox. Several will be used to span a stage.

Collision detection is performed by a function of c_Stage, taking in an instance of c_Player as a parameter.

For every polygon, each side is used to determine an axis of projection. Trig is used to reduce the object's boundbox and the player's groundbox to a 1 dimensional projection. Then, the minimum and maximum values are compared; if at any point the players minimum is greater than the object's maximum, or vice versa there is no collision.

Variables used

Player

The player the collision detection algorithm is going to affect. Taken in as a parameter.

float pos.x
The x and y axis coordinates of the player.
float grav
The 'gravity' of the player. Influences how fast they fall.
float friction
The 'friction' of the player. Influences how fast they stop on a surface.
point accl
The current acceleration the player. Every frame this will be added to the velocity of the player. It is reccomended that this be manipulated instead of the player's velocity whenever possible.
point vel
The current velocity of the player. Every frame this will be added to the position of the player.
float moveVel
When the player moves horizontally, this is the value .accl.x is set to.
float jumpVel
When the player jumps, this is the upward velocity accl.y is given.
point .lastPos
The last point of the player, used to determine which direction to detect collisions from
float height
The height of the player in standard units.
float width
The width of the player in standard units.
bool airborne
should be set to true when player is in the air, false otherwise
void Jump()
Causes the player to jump.
void Land()
Called when the player hits the ground; takes the player out of airborne status.
void Update()
Called every frame; updates position of the player

What remains to be done

- Jump and movement have only a basic definition. It should be more fluid and have speed limits.

- Limit the number of jumps.

- When walking over blocks, the character will 'stumble' where they meet because a collision will be detected there. There needs to be a way to keep gravity from causing these false collisions.

- Detect when the player has fallen out of bounds and place them at a spawn point on the stage.