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?
Collision detection with Verlet integration
Started by FireViper, Sep 27 2012 01:25 PM
4 replies to this topic
Ad:
#2 Members - Reputation: 341
Posted 28 September 2012 - 12:49 AM
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; //inicializationinterval=1/60 //1/fpsmain_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()
[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; //inicializationinterval=1/60 //1/fpsmain_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, 28 September 2012 - 01:32 AM.
#4 Members - Reputation: 341
Posted 29 September 2012 - 03:05 PM
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;
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, 29 September 2012 - 03:21 PM.






