Sign in to follow this  
FireViper

Collision detection with Verlet integration

Recommended Posts

FireViper    129
I recently read an article on Verlet integration ([url="http://lol.zoy.org/blog/2011/12/14/understanding-motion-in-games"]http://lol.zoy.org/b...motion-in-games[/url]) 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 this post


Link to post
Share on other sites
serumas    796
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 this post


Link to post
Share on other sites
FireViper    129
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 this post


Link to post
Share on other sites
serumas    796
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 [url="http://graphics.cs.cmu.edu/nsp/course/15-869/2006/papers/jakobsen.htm"]http://graphics.cs.c...rs/jakobsen.htm[/url]
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this