1
\$\begingroup\$

I am trying put a seek feature in my code.

Just frame code, not actual code.

 Seek(TargetPos)
{
  Vector2D DesiredVelocity = Normalize(TargetPos - currentPos)*MaxSpeed();

  return (DesiredVelocity - currentVelocity}

Simply when I press 2, it will change the target, 3 will change the target and so on...

Do I need to make the currentVelocity the old Desired velocity?

\$\endgroup\$
0
\$\begingroup\$

Yes, you could make currentVelocity equal to DesiredVelocity.

However, this is going to make your object suddenly change direction in an unrealistic manner. If you want your object to travel more realistically, you should try interpolating from currentVelocity to DesiredVelocity, and set that to currentVelocity.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

You do not need to make the currentVelocity equal the old desiredVelocity.

desiredVelocity changes every frame, as the actor changes position in relation to it's target.

Here's an overview of the situation: seek/flee vectors

You could use the resulting steeringDirection to calculate an incremental change to the current velocity (as per Steering Behaviors For Autonomous Characters):

steeringDirection = seek (targetPos);

// this limits the steering force to a maximum steering force
// to avoid unnatural quick 180 turns
steeringForce = truncate (steeringDirection, MaxForce);

// we calculate an acceleration using the actor's mass
// so bigger things can be slow and have "more inertia"
// while small seekers can be fast and more maneuverable
acceleration = steeringForce / mass;

// and add it to the current velocity, which we then clamp to
// the maximum speed of the actor
currentVelocity = truncate (currentVelocity + acceleration, MaxSpeed);

// and we update the position
position = position + currentVelocity;

Hope I could help shed some light on the situation :)

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.