Vector2 Maths Problem (Acceleration/Deceleration towards a Point)

Reply

Join Date: Apr 2009
Posts: 5
Reputation: tnind is an unknown quantity at this point 
Solved Threads: 1
tnind tnind is offline Offline
Newbie Poster

Vector2 Maths Problem (Acceleration/Deceleration towards a Point)

 
0
  #1
Apr 27th, 2009
Hi, I am writing a game in which an object at a PointF travelling at a current velocity (Vector2) is trying to reach a destination PointF (which can change between movement steps)

At each step of movement I need to decide the acceleration vector that will get to the point the fastest.

So the result should be the ship adjusts its current velocity so that it always moves towards the target point at stead acceleration unless doing so would mean there isn't enough time to slow down before reaching it in which case it should begin decelerating.

This has been doing my head in a bit and the code I have at the moment seems to just make the ships orbit the point:

  1. Vector2 v_target = new Vector2(target.X,target.Y);
  2. Vector2 v_currentLocation = new Vector2(this.Location.X,this.Location.Y);
  3. Vector2 v_currentVelocity = new Vector2(f_velocityX, f_velocityY);
  4.  
  5. //calculate vector in direction of target of length f_accelleration
  6. //this line here is fundamentally flawed it can't just acclerate towards the target it has to take into account the current velocity direction somehow (which might not be in the direction of the current target)
  7. Vector2 v_acceleration = new Vector2(target.X - Location.X, target.Y - Location.Y);
  8. v_acceleration.Normalize();
  9. v_acceleration.Multiply(f_acceleration);
  10.  
  11. //if we keep decelerating from current velocity
  12. int i_shouldDecelerate = VectorShouldDecelerate(v_target, v_currentLocation, v_currentVelocity, v_acceleration);
  13.  
  14. if (i_shouldDecelerate ==-1)
  15. {
  16. f_velocityX += v_acceleration.X;
  17. f_velocityY += v_acceleration.Y;
  18. }
  19. else
  20. if (i_shouldDecelerate == 1)
  21. {
  22. f_velocityX -= v_acceleration.X;
  23. f_velocityY -= v_acceleration.Y;
  24. }
  25.  
  26. this.Location = new PointF(this.Location.X + f_velocityX, this.Location.Y + f_velocityY);
  27. }
  28.  
  29. private int VectorShouldDecelerate(Vector2 in_target, Vector2 in_location, Vector2 in_currentVelocity, Vector2 in_acceleration)
  30. {
  31. if (in_location.Equals(in_target))
  32. return 0;
  33.  
  34. //if we start decelerating now, will we overshoot
  35. Vector2 velocityWhenDecelerating = in_currentVelocity;
  36. Vector2 stoppingPoint = in_location;
  37. float f_previousLength = velocityWhenDecelerating.Length();
  38. while (velocityWhenDecelerating.Length() < in_acceleration.Length())
  39. {
  40.  
  41. //reduce velocity
  42. velocityWhenDecelerating.Subtract(in_acceleration);
  43. //calculate stopping point
  44. stoppingPoint.Add(velocityWhenDecelerating);
  45. f_previousLength = velocityWhenDecelerating.Length();
  46. }
  47.  
  48. //if we have arrived
  49. stoppingPoint.Subtract(in_target);
  50. if (stoppingPoint.Length() < in_acceleration.Length())
  51. return 1;
  52.  
  53. else return -1;
  54.  
  55. }
Last edited by tnind; Apr 27th, 2009 at 7:21 am. Reason: elaboration
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: tnind is an unknown quantity at this point 
Solved Threads: 1
tnind tnind is offline Offline
Newbie Poster

Re: Vector2 Maths Problem (Acceleration/Deceleration towards a Point)

 
0
  #2
Apr 28th, 2009
Well I've eventually worked out the following (after talking to a friend of mine), It does a good job at flying from any fixed point to the target using appropriate accelleration/deceleration and is able to compensate fairlly when the you wave the target point around and they can usually get back to the point with only a couple of tight concentric orbits.

Feel free to use the code in your own programs if you ever need to:

  1.  
  2. //get targets location
  3. PointF target = Squad.Formation.ApplyFormation(i_unitPositionInSquad, this.Squad.Location);
  4. //get the velocity we are currently travelling at
  5. Vector2 v_currentVelocity = new Vector2(f_velocityX, f_velocityY);
  6. //this is a vector directly to the target, it will be the direction we would idealy be going
  7. Vector2 v_idealVelocity = new Vector2(target.X - Location.X, target.Y - Location.Y);
  8.  
  9. //dont bother moving if its less than 1 pixel awat to prevent dithering
  10. if (v_idealVelocity.Length() <= 1)
  11. return;
  12.  
  13. //because our ideal vector goes all the way to the point we don't leave any time to slow down so
  14. //we will run a loop to calculate a new better idealVelocity vector
  15.  
  16. //this is the velocity we would be travelling at if we slowed down next step
  17. Vector2 v_iterationVelocity = v_idealVelocity;
  18. v_iterationVelocity.Normalize();
  19. v_iterationVelocity.Multiply(v_currentVelocity.Length());
  20.  
  21. //this is the vector that represents 1 slowdown of length f_accelleration down the ideal vector
  22. Vector2 v_slowdownEachStep = v_idealVelocity;
  23. v_slowdownEachStep.Normalize();
  24. v_slowdownEachStep.Multiply(f_acceleration);
  25.  
  26. //while we have not reached the subsequent step where we would be stopped
  27. while (v_iterationVelocity.Length() > 1)
  28. {
  29. //reduce this steps velocity by the deceleration vector
  30. v_iterationVelocity.Subtract(v_slowdownEachStep);
  31.  
  32. //move our ACTUAL ideal velocty vector closer to us by a distance equal to this slowdown step,
  33. //This will adjust our ideal vector which originally went all the way to the target closer so as
  34. //to allow time to slow down
  35. v_idealVelocity.Subtract(v_iterationVelocity);
  36. }
  37.  
  38. //to find our new acceleration vector that we will be applying get the difference
  39. //between our ideal vector and our current vector and accelerate down it.
  40. Vector2 v_stefansVector = v_idealVelocity;
  41. v_stefansVector.Subtract(v_currentVelocity);
  42. v_stefansVector.Normalize();
  43. v_stefansVector.Multiply(f_acceleration);
  44.  
  45. f_velocityX += v_stefansVector.X;
  46. f_velocityY += v_stefansVector.Y;
  47.  
  48. //adjust our coordinates
  49. this.Location = new PointF(this.Location.X + f_velocityX, this.Location.Y + f_velocityY);
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum


Views: 934 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC