Jump to content

Collision Detection

- - - - -

  • Please log in to reply
8 replies to this topic

#1
cocoa10

cocoa10

    Newbie

  • Members
  • PipPip
  • 12 posts
Looking into collision detection, I found some books that are three years old. I have also found a two tutorials that were one page long.

Is collision detection something that is hard enough that I should get the book on this subject?
Do the higher level languages have built in resources to handle this issue?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It depends a lot on what you're doing. Collision detection with sprites is a lot easier than with 3D graphics.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
cocoa10

cocoa10

    Newbie

  • Members
  • PipPip
  • 12 posts
Is there a way to gage in which direction I should go? Currently I have no preference so I am thinking that I should go the easy path for now. My goal is 3D but working in on two dimensional arena is fine with me. Humm... I am guessing I should go the sprite route.

#4
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
Listen, basic collision detection is fine. I had a VB5 function that checks for collision between two 2D squares - the simplest form of collision detection. I'm gonna search for it a bit and post back when I'll find it.
-EDIT-
Aw man, I forgot that I formatted my HD. My friend will bring me the code tomorrow. I found something, but I'm not sure if it works.
Here's some VB5 code -

Quote

If (Image1.Top+Image1.Height)>=Image2.Top _ 'move Image1 from top downto image2
Or Image1.Top<=(Image2.Top+Image2.Height) _ 'move image1 from bottom upto image2
Or (Image1.Left+Image1.Width)>=Image2.Left _ ' move Image1 from Left to Image2
Or Image1.Left<=(Image2.Left+Image2.Width) _ 'move Image1 from Right to Image2
Then
Msg "Crashed"
End If
In C/C++ it will look like so -


Quote

if( (Image1.Top+Image1.Height)>=Image2.Top || Image1.Top<=(Image2.Top+Image2.Height) || (Image1.Left+Image1.Width)>=Image2.Left || Image1.Left<=(Image2.Left+Image2.Width) )
{
//If it turns out to be true, then both objects are colliding.
}

Again, I'm not sure if it works. Try it, if it doesn't I'll post the working version tomorrow.

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#5
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
Collision detections isn't that hard; it just is expensive in 3d if you do pixel by pixel detection.

I would start with 2D circles, say a simplistic pool/snooker graphic. Then 2d rectangles, then 2d polygons. Then move into the 3rd dimension. It shouldn't take long to
create some code to demo these things.

All the math exists already so it's just reading and experimenting.

#6
cocoa10

cocoa10

    Newbie

  • Members
  • PipPip
  • 12 posts
Thanks, I will start with simple shapes. AdvMutant, thanks for the code, I will play with it over the next two weeks. I have to tare these things down and build them many times.

#7
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
No problem :)
@abzero, Can you please post the circle collision code? It might be useful.

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#8
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
@AdvMutant - Circles are easy. For a circle just store the centre point and the radius. Then if the distance between the two centre points is equal to or less than the sum of the two radii then a collision has taken place.

Ofcause you need to work out the distance between the centre points; this is also simple using Pythagoras:

Given centre of circle one: x_1, y_1 and circle two x_2, y_2
and radii's r_1 and r_2, then it would be somthing like this:

//Calculate distance between the two points.
// a*a + b*b = c*c
c = sqrt ( pow(x_1-x_2,2), pow(y_1-y_2,2));
// Check collision
return c<=(r_1+r_2);


There are some speed ups, like eliminating the SQRT etc. Btw this is off the top of my head from an article about collison detection that i read a while back.

#9
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
Thanks.
As usually, I'm not that good at maths, so I'll try to mess with it a bit and figure out how it works.

Posted Image
There is no problem that cannot be solved by the use of high explosives.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users