collision system (2d)

This topic is 5122 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 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.x + ASTEROID_WIDTH) >= P.x && Asteroid.x <= (P.x + SHIP_WIDTH))    {      if((Asteroid.y + ASTEROID_HEIGHT) >= P.y && Asteroid.y <= (P.y + SHIP_HEIGHT))      {       ............

Thanks again for that tip.
[/EDIT]

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

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:

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.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633731
• Total Posts
3013582
×

Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!