Arrive steering behavior

Started by
13 comments, last by dabo 11 years, 4 months ago

Thank you for the tip.

I noticed that when I change this line:

Velocity = Velocity + acceleration * deltaTime;


into:

Velocity = Velocity + acceleration;


It appears to work like I expect, it also fixes the seek steering behavior. Can anyone explain this? I thought deltaTime had to be included when calculating both velocity and position. Maybe there is still a problem hidden somewhere.


You should definitely keep the multiplication by deltaTime in place. If removing it gives you the behavior you like, you can also get it by dividing the force by deltaTime.

It looks to me like the setup in the code you are using is strange and complicated. I would consider much simpler code, like this (in C++):

Vector2D force_to_arrive(Point2D position, Vector2D velocity, Point2D target_position) {
const double c = 4.0;
const double k = 4.0;

Vector2D position_difference = target_position - position;
return k * position_difference - c * velocity;
}
Advertisement
Thank you for your reply.

Yes I thought keeping deltaTime in the calculation was correct. I am just confused as to why the book author's code work but not mine, I don't see any difference and I have tried using the same default values for mass, max force, max speed.
I found a C# implementation of the same example from the book and it had no parenthesis around deceleration * decelerationTweaker like this:


double speed = distance / (double)deceleration * decelerationTweaker;


When I changed this in my code it appeared to work even with deltaTime:


Velocity = Velocity + acceleration * deltaTime;


I am really confused why I need to remove the parenthesis when all other implementations I have seen (except the one I mentioned above) uses them. I have also tested implementations not from the book and they also result in this oscillating behavior when I use it in my code.
Did you change something else too?.

YES.

I also did what you did like this here.
Velocity = Velocity + acceleration;
note I jast added it back then and tryed it. And it has the same problem.

But this is my code here I thik its the way to go.

SteeringForce = SteeringBehaviors->Calculate();

//Acceleration = Force/Mass
D3DXVECTOR3 acceleration = SteeringForce / ComponentMotion->m_dMass;

//update velocity
ComponentMotion->m_vVelocity += acceleration;// * timedelta; REMOVED IT HERE
//make sure vehicle does not exceed maximum velocity

ComponentMotion->m_vVelocity = Truncate(ComponentMotion->m_vVelocity, ComponentMotion->m_dMaxSpeed);
//update the position
ComponentMotion->m_vPos += ComponentMotion->m_vVelocity * timedelta;
Thanks for the reply.

It does decelerate nicely if I remove delta time from the acceleration calculation, but then I get the problem that my object reaches the max speed directly after just one call to Update().

This topic is closed to new replies.

Advertisement