#### Archived

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

# Collision detection

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

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
Ha, I''m inventing new words... it''s "Collision" not "Collion".

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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:

// 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

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
Should this work?

typedef struct
{
}OBJECT;

OBJECT stuff[2];

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

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

• 16
• 9
• 15
• 10
• 13