Collision detection with Verlet integration

Started by
3 comments, last by FireViper 11 years, 6 months ago
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?
Advertisement
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()
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?
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;
thanks for clearing everything up.
I will check out the article.

This topic is closed to new replies.

Advertisement