1

I'm programming a movement class for a game and I'm having some trouble getting an object to approach a destination along a curve. I've read multiple other stackoverflow answers to no success, so I thought I'd open a question of my own.

Here's the method inside my movement class where I'm trying to approach a destination Vector2 from a current position Vector2:

public Vector2 CurvedMove()
{
    // Where DestinationPosition and CurrentPosition are Vector2's stored in 
    // the movement class. 
    float xDistance = DestinationPosition.X - CurrentPosition.X;
    float yDistance = DestinationPosition.Y - CurrentPosition.Y;

    // Set the center of the circle once (-1 is what _circleCenter is initialized to)
    if (_circleCenter.X == -1)
    {
        // Gets the center point of our circle
        _circleCenter.X = CurrentPosition.X + (xDistance / 2.0f);
        _circleCenter.Y = CurrentPosition.Y + (yDistance / 2.0f);
    }

    float diameter = (float)Math.Sqrt((xDistance * xDistance) + (yDistance * yDistance));
    float radius = diameter / 2.0f;

    float circumference = (float)Math.PI * diameter;

    // Speed represents the number of pixels an object can move (along a straight 
    // line) in a tick
    float factor = ((float)Speed) / circumference;
    _elapsedAngle += 2.0f * (float)Math.PI * factor;

    // Math based off of this answer: http://stackoverflow.com/questions/14096138/find-the-point-on-a-circle-with-given-center-point-radius-and-degree
    _currentPosition.X = (_circleCenter.X + (float)(radius * Math.Sin(_elapsedAngle)));
    _currentPosition.Y = (_circleCenter.Y - (float)(radius * Math.Cos(_elapsedAngle)));

    if (_currentPosition == _destinationPosition)
        _reachedDestination = true;

    return CurrentPosition;
}

This is probably simple geometry or a complete misunderstanding of basic math, but I just can't figure it out. Any ideas?

tots_o_tater
  • 529
  • 1
  • 5
  • 12

0 Answers0