Jump to content

  • Log In with Google      Sign In   
  • Create Account


Collision detection with Verlet integration


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 FireViper   Members   -  Reputation: 129

Like
0Likes
Like

Posted 27 September 2012 - 01:25 PM

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?

Sponsor:

#2 serumas   Members   -  Reputation: 677

Like
2Likes
Like

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()

Edited by serumas, 28 September 2012 - 01:32 AM.


#3 FireViper   Members   -  Reputation: 129

Like
0Likes
Like

Posted 29 September 2012 - 10:08 AM

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?

#4 serumas   Members   -  Reputation: 677

Like
1Likes
Like

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;

Edited by serumas, 29 September 2012 - 03:21 PM.


#5 FireViper   Members   -  Reputation: 129

Like
0Likes
Like

Posted 30 September 2012 - 01:08 PM

thanks for clearing everything up.
I will check out the article.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS