2d game and collision detection

Started by
16 comments, last by Hedos 20 years ago
Hey I'm about to start a new 2d game and I've been thinking about a way to handle collision between objects/world.. And I don't want to have a tile based world So here is what I have been thinking.. Each moving objects ( player, monsters, npc and maybe also some -small- static objects ) would have a bouncing box.. Do you think a bouncing box is the faster way to handle that kind of collisions? And for the static world, I would draw lines, in my Map editor, to specify where the collision is.. So I would just calculate the distance between the line and the objects.. Here is a little schema I made of what I was thinking ( if image doesn't show up, copy/paste this link: geocities.com/darkhedos/gameCollision.gif ) Are my idea good? bad? or even horrible? What do you suggest? Thanks [edited by - Hedos on March 22, 2004 6:42:48 PM]
Advertisement
boxes are fine, but spheres are even simpler

Ball and segment collision

that's a simple algorithm to get you going. Then you can move on prety quickly to boxes, swept spheres, and swept boxes if you need to. If you decide to go ahead, you can email me in case of troubles, coz it's untested code (but the algo is sound).

[edited by - oliii on March 22, 2004 7:23:17 PM]

Everything is better with Metal.

Thanks for your reply I'm currently looking at your code
What would be the faster, boxes or circles?

And what about the lines to detect the static objects, is it a good idea?

Thanks again


edit: just finished reading your post, that looks like some pretty interesting code. I'm gonna try this and tell you if it works

[edited by - Hedos on March 22, 2004 7:33:16 PM]
Here you can find code to handle circle-segment sweeps (ie a moving circle that may have collided with an obstacle between frames).
Circles (from my limited experience) are generally good when dealing with collision because they can be applied well to a variety of objects.
Wow awesome Jack, thanks

I''m currently working on both of your suggestions and I guess the result will be good.. but I''m still wondering if my whole concept is good or if something else would be better? ( Mainly in term of speed, as I want my game to be accessible even on low machines )
here is a really simple bounding box collision detection function

bool Collision(CONST RECT *lprcSrc1, CONST RECT *lprcSrc2){  RECT rcDst;  // Check if the rects intersect  if( IntersectRect(&rcDst, lprcSrc1, lprcSrc2) )    return true;  else    return false;}


if( Collision(sprite1.getBBox(), sprite2.getBBox()) ){  // sprite1 and sprite 2 are colliding} 
Your collision technique looks good to me - line segments to define the game world, spheres to handle the individual objects. That is how I might do it - just make sure that you reject any lines from the test that are not candidates of collision (or you could use a quadtree to speed things up, but then this goes beyond my experience) so that you are only testing against those you actually need to. Bounding boxes are often used to check if a collision is possible before performing pixel-overlap tests, so I would therefore suggest the use of circles as I believe they produce a better result.

[edited by - jack_1313 on March 23, 2004 3:58:17 AM]
as far as speed is concerned, it''s not a problem. either box or spheres, it''s very fast. Especially if you have a way to reduce the tests, by finding the segments close to the object very quickly, like with a quadtree or bsp tree, or any other methods.

The swept test is good if objects move fast, the simple test is a good start to see how things are going, it can be implemented in an hour and already allows you to have some gameplay. Also, to do character vs character, it''s just a matter of applying the same algo, but with sphere-sphere, so straight forward again.

With swept tests, there are a few gotchas, and some complications can arise when dealing with lots of moving objects at the same time.

Generally, the first solution can be applied straight away for slow-paced games (like in a rpg, or rts), while the other if better suited for fast arcade games, like Soldat, or a breakout/pong.

Everything is better with Metal.

Jack_1313: I tried your code, but I'm stuck at the point of using the velocity

Vector2D op = Ball.p;Ball.p + = Ball.v;Vector2D i;Vector2D r;float t;if(CircleSegmentSweep(Ball.r,op,Ball.p,linep1,linep2,i,t,r)){   Ball.p = i;   Ball.v = r;   Ball.p += Ball.v; //Move the ball away from the collision}

I can see that 'v' is the velocity of the ball, but I don't know what are velocity vectors and how to use that?

You said: "I recommend you alter your struct so as that it uses a velocity vector to represent speed and direction."

I guess that's what I would have to do, but I don't know how velocity vectors work... ( I haven't done really advanced math courses yet, haven't learned anything about vectors and that kind of stuff )

Thanks

[edited by - Hedos on March 23, 2004 5:59:35 PM]
as far as game programming, and mostly physics and rendering, Vectors are probably the best thing you can learn. also basic trig. It''s not very complicated.

head towards the article & resources section, here you''ll find a couple of good introductions to basic maths.

try this one
Vectors and Matrices: A Primer (Phil Dadd)

or this one (this site is quite useful as a reference)

Maths : Vectors (MArtin Baker)

you don''t have to start on matrices, but I kind of gave up explaining collision detection and basic physics without the use of the simplest of vector maths. It''s too much work. Besides, they are quite intuitive and not difficult to learn.

Everything is better with Metal.

This topic is closed to new replies.

Advertisement