Collision detection with Verlet integration

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

Recommended Posts

I recently read an article on Verlet integration (http://lol.zoy.org/b...motion-in-games) and wanted to apply it. It seems to work, but I'm not sure how to use it with my collision detection code. My update function looks something like this:

[source lang="cpp"]void State::Update(float dt)
{
Vector accel = Vector(...) //get accelreation based on user input
Vector oldVel = vel;

vel += (accel) * dt;
pos += (oldVel + vel) * 0.5 * dt;

if( if_collision_is_enabled )
{
Vector newpos = CollideWithWorld(pos,vel); //get new position
//update velocity & position based on the new position
vel = newpos - pos;
pos = newpos;
}
}[/source]

It seems to work, but the movement isnt as smooth as it is without collision. I assume I'm not updating the velocity and position correctly after the collision. Does anyone know a better way to do this?

Share on other sites
1. I would use not velocity , but pos and last_pos, and calc velocity just in one place and not in any place there pos changes.
[source lang="cpp"]
void move()
{
vector2 vel=vec-last_vec;
last_vec = vec*air+acceleration;
vec+=vel;
}
[/source]
2. I would do routine with fixed dt, for example 60 fps, becouse with changing dt your verlet would be unstable, and you wouldnt need use dt in move().
[source lang="cpp"]
deltatime=0; //inicialization
interval=1/60 //1/fps
main_loop(float dt)
{
deltatime+=dt;
iterations=(int)(deltatime/interval);
deltatime=fmodf(deltatime,(float)iterations);
alpha=deltatime/interval; //for smothnes on drawing
for(int i=0;i < iterations;i++)
{
move_all_objects();
collide_all_objects();
satisfy_all_objects_constraints();
}
}
[/source]
3. I would make two functions insted of one move() and collide(), and in main loop will use move_all _objects() and collide_all_objects() Edited by serumas

Share on other sites
thanks for the reply serumas, I have a few questions.
On the move function, is 'air' a float?
Also, what does the 'satisfy_all_objects_constraints' function do?

Share on other sites
air is air density 0.0..1.0f changing this to 0.5f will make movement like in water , with velocity dumping
Satisfy constraints - than your object is from multiple atoms conected with constrains that these atoms keep shape or angle or something other rull.

take a look to http://graphics.cs.c...rs/jakobsen.htm
there is code samples, basic start making verlet system

Oh i made very big mistakes in move(), sorry, I was a little bit drunk that night
it should be:

vector2 vel=vec-last_vec;
last_vec = vec;
vec+=vel*air+acceleration; Edited by serumas

Share on other sites
thanks for clearing everything up.
I will check out the article.