I am using Euler integration to update my position, velocity, and acceleration on a 2D plane. I move characters on screen by a point and click mechanic. I compute the vector the character travels along by taking the destinationVec - positionVec, normalize the resultant vector then mutiply that by some scalar that represents a characters acceleration value. The result is strange, on further distances traveled the character will completely miss the target and start circling around the point I clicked. When I remove the scalar multiplication it works fine with just the normalized vector. I must be rusty on my math or something? So I will post the code:
void navigateToDestination(CollidableObject &character)
{
sf::Vector2f destinationPx = character.getDestination();
sf::Vector2f positionPx = character.getPosition();
sf::Vector2f acceleration = character.getAcceleration();
sf::Vector2f direction = destinationPx - positionPx;
vecMath::normalize(direction);
....
if(character.getIsMoving())
{
acceleration.x = direction.x * character.getAccelerationValue(); <- Which is some float, 10.0f, in this instance
acceleration.y = direction.y * character.getAccelerationValue(); <- When this acceleration value is removed the path works
}
....
character.setAcceleration(acceleration);
}
..... Later called function
void updatePosition(double dt)
{
setCollisionRect();
m_position += sf::Vector2f(m_velocity.x * dt, m_velocity.y * dt);
m_velocity += sf::Vector2f(m_acceleration.x * dt, m_acceleration.y * dt);
updateCharacter(); // Just limits the acceleration/velocity based on character specific limits
}
I've limited the code to what I think might be the problem so I realize you may need to see more to catch something. Please feel free to ask me to post more. Thanks a lot!
Best,
DishSoap