
collision response from verlet
Hi
Im using the Jacobsen article to work on a 2D physics engine. However, I got the stick to move realistically but Im not sure what to do on the collision detection part. My current method allows me to detect whether or not there's been a collision but not what the penetration points are. How would I go about finding these, or if theres a better way of handling response, what would I do?
Thanks
ps: This isn't in actionScript as I want to use a language Im familiar wiith first before I port it to AS.

Senior Member
start with circles
Hey, i posted here a while back about a 2d verlet engine for flash that i'm working on, and actually i just started working on it again not too long ago
(link)
my suggestion to you is first make circle collision/response
it doesn't have to be completely elastic, or accurate, but it still looks good if you use this method: (which is what i used)
i persume you know how to check if 2 circles have collided (if the distance between their centers in less then the sum of their radii)
for collision response, think about it as putting in a stick in between the circles every time they over lap (for that one timestep only) or in other words, move both circles in the opposite direction so that their distance equals the sum of their radii (and with the verlet integration their speeds will be affected as well)
try that and add as many circles as you want because it is very stable
once you have circle collision you should move onto circleline collision and these are pretty much all of the things that you'll need
if you need any help just post here or send me a message
good luck!
Last edited by ozmic66; 01012006 at 09:27 AM.

Thanks ozmic66. Just a quick question before I start with the circle stuff. What did you use for your (very nice) engine in terms of verlet algorithm? For my stick I was using:
p1.y# = (p1.y# + (p1.y#  o1#) + vy# * 0.5)
p2.y# = (p2.y# + (p2.y#  o2#) + vy# * 0.5)
where p1.y# = current pos and o1# = old position. vy# = gravity and 0.5 is the timestep although Im not sure what I am actually supposed to use there?
however, the jakobsen paper says to use:
x' = 2x  x* + a·Δt2
but that messes up the whole thing for my system if I put a '2' in front of everything.
any ideas why?
Thanks again for the help.

Senior Member
hmm...
all good questions...
here's what I use:
(copied from the code)
x+=((xoldPosition.x)+(Acceleration.x*timeStep*timeStep)) *cWorld.getDrag()
y+=((yoldPosition.y)+(Acceleration.y*timeStep*timeStep)) *cWorld.getDrag()
where:
acceleration is the sum of all the forces acting on the particle (gravity,wind maybe)
also, dont forget to reset the acceleration to 0 at the end of the verlet code
cWorld.getDrag() is a number between 0 and 1
i don't add a '1' to it (hence multiplying by a number from 1 to 2) because as you see, i'm adding that value to the already existing one
now the timestep i keep at one
what the timestep controls is how much of its movement the particle should do in one frame
at 1 it's 100%
if you want to slow down time, then you can set it to something like 0.5 or any other number
to speed up time..guess what..1+
hope that helped

Thanks a lot ozmic66. I tried getting the circle collisions to work. I can get them to detect collision but I cant get them to travel in opposite directions. Is there a chace you could show some psuedo code or an article that might help. Im mainly stuck on how to "put a stick" in between and try and get the objects to move in the right direction after collision.
Thanks again and happy new year!

Senior Member
happy new year
i find it best to use vectors in this case becaue they make everything easier
i don't know how firmiliar you are with vector math so here is a little explenation
a vector holds two pieces of information: 'x' and 'y'  you can also see these as 'run' and 'rise'. a vector can be used as a smiple point in space or something that contains the properties of a line (only without coordiantes in space)  i know it might sound kinda confusing, and that's because this isn't the best explenation so you should probably read some more about it.
now here is how you would use vectors to move to objects away from eachother
psuedo code:
if collision =true then
//find the vector between the two circles
vectorX=circle1.xcircle2.x
vectorY=circle1.ycircle2.y
//find the distance between the circles//
dist = sqrt(vectorx*vectorX+vectorY*vectorY)
//find the distance that both circles should move in order to not overlap
distDiff=(circle1.radius+circle2.radius)dist
distForEachCircle=distDiff/2
//normalize the vector with the desired length  this is what vectors are good for
vectorX=vectorX/dist * distForEachCircle
vectorY=vectorY/dist * distForEachCircle
//add the vector to circle 1
circle1.x+=vectorX
circle1.y+=vectorY
//subtract it from circle 2
circle1.x=vectorX
circle1.y=vectorY
end if
so here, it's a little more than psuedo code, but that can't hurt can it?
good luck

Iron Chef InTraining
A Verlet Flasher's (He he) mustgoto place would be the Metanet tutorials. They have an indepth tutorial just on collision detection using Verlet Integration.
Not that I'm saying Ozmic's method is bad... in fact, he's probably got the best Collision Detection code here, but it is a nice place to see what you can really do with advanced Verlet collision.

Thanks a lot ozmic66, that was really helpful!
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
