Skip to main content
gamedev.net
🔒 Locked

WayPoint Function, Please help with small problem.

Started by Oldfella 4 replies 1.2k views
Original Post
Oldfella
Oldfella
Hi, Hope you are all well. I am using the code below to move a ball on a pre-determined path at a constant speed. It works fine until it gets to the endpoint. At this stage the ball starts shaking. By looking at the normalized value of d, as it arives at the endpoint, I see it jumping from -1 to 1, therefor, causing the problem. The value is stable befor reaching the endpoint. How should I fix this so the Ball stops dead at the endpoint? A = BallPosition B = EndWayPoint d = B-A Then Normalized A = A + d * Time * Speed Any help much appreciated. :) Cheers.
FReY
FReY
The problem is that your ball is overshooting the mark, and then overshooting back again...

Calculate B-A. Before you normalize, you have the length of B-A, right? So if the distance from the ball to the target is less than the amount you're going to step, just assign the ball to the target position and set it's speed to zero.
ie.
A = BallPositionB = EndWayPoint;d = B-A;fMoveDistance = Time * Speed;fLengthD = VectorLength(d);if (fLengthD < fMoveDistance){  // stop the ball  A = B;  Speed = 0;}else{  // move the ball closer to the target  // (no need to do normalization, because we can simply divide by the length here rather)  A = A + (d * fMoveDistance) / fLengthD;}


Hope that helped :)

edit: fixed some spelling :)
do unto others... and then run like hell.
Oldfella
Oldfella
Hi FReY,
Thanks for your prompt reply.
Yes I do Calculate B - A befor Normalizing the result.

Just one question, what do you mean by VectorLength(d);?
as I have not seen this function befor.
Or is this just the normalized result of d ?

Thanks.
FReY
FReY
When normalizing your vector, you divide it by the length of the vector so that the length of the vector becomes 1. The function VectorLength(D) calculates the length of your vector. In pseudocode:

float VectorLength(float *pfVector){  return sqrtf(pfVector[0]*pfVector[0] + pfVector[1]*pfVector[1]+pfVector[2]*pfVector[2]);}// or if you have a struct for your vector (instead of an array)float VectorLength(Vector *psVector){  return sqrtf(psVector->x*psVector->x + psVector->y*psVector->y + psVector->z*psVector->z);}


You probably have a similar function around? What structure are you using to store your 'A' and 'B'?
do unto others... and then run like hell.
Oldfella
Oldfella
Thanks FReY,
Yes I use that often, but have been calling it Magnitude,probably the wrong name for it :)

I looked up and temp substituted D3DXVec3Length, it worked.
Your formula works great :)
Your help is much appreciated.

Cheers.
FReY
FReY
Yah, okay, 'Magnitude' is a good term for it too. Now you know it's the same thing :)
do unto others... and then run like hell.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.