I have an entity in 2D space moving around, lerping between waypoints. I would like to make the entity rotate around its own origin to face the current direction that it is going, I.E. towards the next waypoint in the list.
In case the waypoint movement code is needed, I'm using the following (this is contained inside of the entity that is following the waypoints) :
private void GoToWaypoint() {
if (waypointList.Count > 0) {
Vector2 origin = helper.GetOrigin(position, width, height);
int nodeWidth = 50;
int nodeHeight = 50;
if (moveToPosition != waypointList[0]) {
moveToPosition = waypointList[0];
moveToPositionOrigin = helper.GetOrigin(moveToPosition, nodeWidth, nodeHeight);
distanceToPosition = moveToPositionOrigin - origin;
}
if ((Math.Ceiling(distanceToPosition.X) < 0)) {
position.X -= moveSpeed;
distanceToPosition.X = (int)Math.Round(distanceToPosition.X + moveSpeed);
}
else if ((Math.Floor(distanceToPosition.X) > 0)) {
position.X += moveSpeed;
distanceToPosition.X = (int)Math.Round(distanceToPosition.X - moveSpeed);
}
if (Math.Ceiling(distanceToPosition.Y) < 0) {
// Check if the entity moves below the top boundary
if (position.Y > 0) {
position.Y -= moveSpeed;
distanceToPosition.Y = (int)Math.Round(distanceToPosition.Y + moveSpeed);
}
else {
distanceToPosition.Y = 0;
}
}
else if (Math.Floor(distanceToPosition.Y) > 0) {
// Check if the entity moves below the bottom boundary
if (position.Y < 550) {
position.Y += moveSpeed;
distanceToPosition.Y = (int)Math.Round(distanceToPosition.Y - moveSpeed);
}
else {
distanceToPosition.Y = 0;
}
}
// If the entity gets in the waypoint, or the distance to the waypoint is 0 on both axis (which can occur due to screen boundaries)
if ((helper.InNode(position, moveToPosition, width, height, nodeWidth, nodeHeight)) || ((distanceToPosition.X == 0) && (distanceToPosition.Y == 0))) {
waypointList.RemoveAt(0);
}
}
}
I know this may have been asked elsewhere, and the bit of searching that I did on this type of problem, didn't really help me. I'm slightly math retarded, so I don't understand the explanations that don't really explain what the math is exactly achieving. So any help that breaks down the math and functions needed to achieve this would be greatly appreciated.
Thanks.