Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Smatthew

2D Collision Detection Problems!!!

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, here''s the dillio: I''ve been working on an oldschool 2D sidescroller engine for a few months now, and most of it is ready to go, but i can''t FOR THE LIFE OF ME figure out a good way to do collision CORRECTION. I can detect collisions, but I haven''t been able to correct them in a reasonable way. This has been driving me crazy, because I know a million and one ways to find out things have collided, but I can''t fix them. I feel pretty stupid because I know it must be simple (because people have been doing it forever), but no one seems to write specifically articles specifically on correcting collisions. If anyone can give me any help/advice/resources/simple code/point me in the right direction, I would be extremely thankful. BTW, I''m dealing with collision correction in a simple Super Mario Bros type game--I just want simple things to happen, like have the characters be able to land on top of bricks or knock coins out of them by hitting them from the bottom. Thank you in advance, Steve

Share this post


Link to post
Share on other sites
Advertisement
It depends what types of collision detection you are using. Rectangles? Circles? Polygons?

If it''s rectangles, it''s a peice of cake. If moving left, get the left most side of the character (charXLeft), and the right most side of the solid object(objXRight. objXRight-charXLeft gives you the distane to move the character to the right to correct the collision.

When using circles or polygons it becomes mroe advanced, but the basic principle is the same. You simply find the distance the character has intersected the geometry by and move said character said distance away from the collision.

Share this post


Link to post
Share on other sites
If you have more complex models, ( a plane with a nose and 2 wings) and don''t want an oversized rectangle, divide the plane into 4 sections: Body sqaure, Wing1 Square, Wing2 Square, and Nose Square. If projectiles only come from one direction, all the better. You end up need to check only between 7 xy positionsto check for a collosion.

-----------------------------
Jammer
Jammer211@comcast.net
http://www.wconline.org : Your Warcraft Source
http://www.fark.com : Laugh, Cry, Photoshop
http://www.natural-selection.org : Online FPS RTS Gaming for Half-Life

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Are you collision testing before or after you move the object?

ie Mario is jumping, will this upward movement cause a collision?
if so
is he jumping towards a moveable object?
if not then stop him
if so then move the object, then move him..

Maybe I have over-simplified ... maybe not...
J.

Share this post


Link to post
Share on other sites
I''m sorry I didn''t really clarify the problem I''ve been having:
Lets say a character is approahing an object diagonally; jumping at a brick from the right, for instance. How do I determine whether the character hits the right side of the brick or comes up from underneath it. This is important because I would like an action to be triggered only if the brick is hit from the bottom. Also, if this were to occur during the game (of course it will), how will I be able to figure out which side to move the character to, the bottom or the right?

Also, I would prefer the simplest solution possible, which implies bounding boxes, but I can''t figure out how to implement them for this.

Thanks,
-Steve

Share this post


Link to post
Share on other sites
I have exactly the same problem you''ve got Smatthew. Unfortunately I haven''t figured it out yet either. I wouldn''t have thought a simple 2D collision check would cause me such a headache.
Are you using a tile based world (2D array of tiles?) or polygons.
I''m using a combination of both and I initially did it like this. I use a bouding rectangle on the character for collisions.
Work out the direction the player is heading. ie. up and left, down and right etc.
If it''s down and right for eg. I use the right and bottom lines of the rectangle for collision checking. If the right line hits a tile, I move the character to the left of that tile. Same deal with the bottom line except you move it above the tile.
This seemed like an easy solution but it still has the same problem you mentioned about landing diagonally on something and whether or not you landed on it or hit it from the side.
The problem is that I''m checking with two lines but they share a common point which can''t be determined as being either one or the other.
I''m going to try using line intersections instead. So for the above collision check, you take the bottom right point and cast a ray out in the direction and length of its velocity vector. Then just get the two lines of the tiles it can possibly intersect and check which one it collides with. You then know whether you landed on top or to the side of a tile.
I only just thought of that then and I think I''ve worked out my own problem. Sweeet. I''ve been thinking about this problem for days and it suddenly just popped into my head. The power of writing out a problem hey.
Once I''ve coded it up, I''ll post it for you Smathew. It should actually be pretty simple.
Jules.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you want to collision test for your ''dude'' jumping up to cause the desired action, and sideways causing the undesired action...

a) test for the greater collision (ie any collision)
if (player.left > object.right
|| player.right < object.right
|| player.top > object.bottom
|| player.bottom < object.top) return(0);// no collision
else ...
b) test if player is moving in an upwards direction
obviously if the player is moving downwards ... he is not moving up, you just have to decide at what angle the collision occurs, like... if (player.velY < -(ABS(player.velX)))

OR you could first test if player has collided with the object then test if player has collided with smaller ''hit zone'' located in the bottom center of the object ... keep in mind that if the player can move a distance greater than the distance from the object top or sides you may have problems...

I have assumed you are in c/c++ ...
j.
aka jetset33@shaw.ca

Share this post


Link to post
Share on other sites
One method of testing ahead is to cast a ray ahead of the moving character. This done _before_ any move. How do you store the background information? Tiles or objects??

ZoomBoy
Developing a iso-tile 2D RPG with skills, weapons, and adventure. See my old Hex-Tile RPG GAME, character editor, diary, 3D Art resources at Check out my web-site

Share this post


Link to post
Share on other sites
Well you might be able to modify this code to check for which side the player hits it from (since I''ve already divided it into up, down, left, and right). It''s simple collision code for a rectangle, assuming you know the x and y positions of both objects involved.


  
bool Collision(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2, GLfloat x2right, GLfloat x2left, GLfloat y2up, GLfloat y2down)
{

if (x1 < x2 + x2right && x1 > x2 - x2left && y1 < y2 + y2up && y1 > y2 - y2down)
{
return TRUE;
}
else
{
return FALSE;
}
}

Share this post


Link to post
Share on other sites
Yeah, Joolean knows exactly what problem I'm having. One of the techniques I have tried involved a little bit of raycasting, but it quickly became a big messy nitemare, so I was wondering if there was something simpler I was missing. Also, my engine will be completely tile based, so another problem I was running into was determining what order I should correct the collisions. This is something I had not anticipated, but when you have a sprite flying at a wall of bricks (each brick is a tile, so they have to be tested separatly), it was hard to figure out which brick the sprite was hitting. For instance, if a character is moving diagonally toward a wall of bricks, within any individual frame it can intersect more than one brick, but logically, it would hit the closest one and never make contact with any of the others. If anyone has any pointers or would like to share info about similar problems, please do.

-Steve

[edited by - Smatthew on August 28, 2002 8:34:01 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!