Collision detection

Started by
12 comments, last by JonatanHedborg 23 years, 11 months ago
How would i use circles to detect collision in a 2D game? (the circles are around the player/enemys/npc but the walls have "normal" coll.dect) ======================== Game project(s): www.fiend.cjb.net
=======================Game project(s):www.fiend.cjb.net
Advertisement
Don''t use the circles in the calculation, it''s that easy. To calculate a collion, use:

1. object to object, calculate the distance tween their center points.
2. object and wall, calcualte distance from center point to the wall''s ''line''.

You don''t use the circle in the calculation because, by definition, it''s the same distance from your point to anywhere on the circle.

Both of the above numbered examples then become basic geometry (or is it trig?) functions. Distance from p1 to p2 and distance from p1 to line1.

Ha, I''m inventing new words... it''s "Collision" not "Collion".
Use the circle equation x^2 + y^2 = r^2 if something is inside the circle x^2 + y^2 < r^2, and if something is out side x^2 + y^2 > r^2, and if its ban on the edge of the circle then x^2 + y^2 = r^2. Of course you would have to modify/adjust it slightly for use in a game.
------------------------------"My sword is like a menacing cloud, but instead of rain, blood will pour in its path." - Sehabeddin, Turkish Military Commander 1438.
The purpose of using circles (spheres) is
for the radius. As you know, objects
will be of different sizes, so just testing
for a certain distance will not work right
unless all the objects are the same size.
You will need to find the distance between the
two objects, then subtract both of their radius and
see if the result is negative or not.



--------------
My opinions do not
reflect the porn
industry in any way.
Tip when doing the distance formula...

You can avoid the sqrt(). Say you are testing for distance 3.

With sqrt()... this is slow
if (sqrt((x0-x1)^2+(y0-y1)^2)) <= 3) { collision! }

Without sqrt()... this is fast!
if ((x0-x1)^2+(y0-y1)^2) <= 9) { collision! }

Can anyone remember the stupid formula for distance from a point to a line? It escapes me right now.
The objects need not be the same size for a one-size fits all function to work.

bool Collision(Object *a, Object *b, int collisionDist);

where a and b are pointers to your objects.
collision distance is how close the centers can be before a collision occurs.

Call it like this if you'd like:

if (Collision(&ship,&rock,ship->radius+rock->radius)) {
// collision! BOOM!
}

Actually, you don't really need the 3rd parameter now that I look at it. The function can grab that itself.

-Michael

Edited by - Buster on 5/1/00 3:39:07 PM
Ok, thanx for all the great answers!

I must be missing something in my basic C/C++ training but what does the -> do?



========================
Game project(s):
www.fiend.cjb.net
=======================Game project(s):www.fiend.cjb.net
The "->" is used to access members to a pointer of a structure. Example:

If we define a structure like this

struct Object
{
int xpos;
int ypos;
int state;
} game_object,game_object_ptr;

Then if we declare a variable of the structure like this:

game_object ship;

we would access the members like this:

ship.xpos = 100;
ship.ypos = 40;

if we then declares a pointer of this structure instead like this:

game_object_ptr ship;

the members would then be accessed like this:

ship->xpos = 100;
ship->ypos = 40;

I''m no C/C++ expert or anything but i hope this helped.
Should this work?

typedef struct
{
int x,y,radius;
}OBJECT;

OBJECT stuff[2];

if( (stuff[1].x-stuff[2].x)^2+(stuff[1].y-stuff[2].y)^2<=(stuff[1].radius^2)+(stuff[2].radius^2) )

Im not sure about the last part, please correct me if im wrong...
















========================
Game project(s):
www.fiend.cjb.net
=======================Game project(s):www.fiend.cjb.net

This topic is closed to new replies.

Advertisement