Sign in to follow this  
erickmeister

collision system (2d)

Recommended Posts

Hello, I'm creating a 2d space shooter (one of many projects). So far I have a wire-frame ship which can move around as the space background scrolls downward and a few asteroids come crashing down (while rotating). So far everything is looking very good however I'm planning on creating a collision system for my game now. Maybe something I can re-use in other projects. I'm thinking of a way to do this but i'm afraid of approching it wrong. For example, on a tile game i'm also working on my collision system was planned to check one unit ahead of the player before each attempt to move then find out if that tile is walkable or not. i.e.
struct tiles
{
  BITMAP *image;
  bool walkable;
}
Tiles are 16x16 so...
int map_x = player_x / tile_size;
int map_y = player_y / tile_size;
Let's say if I move right I increase a player_direction_x by one THEN divide and check against my tile structure like so...
...REQUESTED TO MOVE RIGHT
...GET TILE 
if(tile[x].walkable == true) ...THEN WALK
But I don't think I should use something like this in the kind of game I am creating now since I can only move side to side and I have to check for asteroids whom Y values keep changing. Any ideas on how to create a collision system for this type of project OR know of a good tutorial on collision engines? Thanks a lot in advance. Erick

Share this post


Link to post
Share on other sites
Erick this seems simple enough, lets see if I can show you one method for doing it.

For each asteroid (or any object that could collide w/your player) simply check the x,and y values for said object against the x,y values of your player. If they are equal, then you handle the outcome of the collision, depleting the players shields, rendering an explosion, etc. here is some pseudo code:

for each asteroid
if (asteroid.x >= player.x and asteroid.x <= player.x + player_width) and
(asteroid.y >= player.y and asteroid.y <= player.y + player_height)
//we have a collision

of course this method only checks to make sure that the upper left hand corner of the asteroid sprite is within the players sprite bounds. And this assumes you are using square sprites. This is a very rough method but extremely simple. I hope it helps, and good luck

Share this post


Link to post
Share on other sites
This does seem very simple :) Thanks for pointing this out to me. *bangs head on desk*

Erick

[EDIT]
I was able to pull this off pixel perfect pretty much...


// check all asteroids
for(int i=0;i<MAX_ASTEROIDS;i++)
{
// first check x positions, then y
if((Asteroid[i].x + ASTEROID_WIDTH) >= P.x && Asteroid[i].x <= (P.x + SHIP_WIDTH))
{
if((Asteroid[i].y + ASTEROID_HEIGHT) >= P.y && Asteroid[i].y <= (P.y + SHIP_HEIGHT))
{
............


Thanks again for that tip.
[/EDIT]



[Edited by - erickmeister on December 6, 2004 5:06:38 AM]

Share this post


Link to post
Share on other sites
If asteroids are the primary targets, then maybe circle vs circle collisions might work for you better than bounding box. Basicly it works by saying:

if ( distance_between_objects > asteroid.radius + player.radius )
return no_collision;

to find the distance between the 2 objects, you use:

remember this from geometry class?
float distance = sqrt( abs(asteroid.x - player.x)^2 + abs(asteroid.y - player.y)^2 );

although if the x values are the same, the distance is just the difference between the y values... same for if the y values are the same then it is the difference between the x values.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this