Hi all, I am trying to implement a movement by time concept vs moving x amount every tick. The end goal is to pass in a function move sprite x, to location x, from location q, you started moving at time n, you will end there are time z.
I am so close, but there appears to be an error in my math somewhere, the sprite gets close to it's destination but always overshoots by one (x or y) and undershoots the other. Please let me know if you a better way of doing this
Calling my function:
int TimeToEndMoving = moveStart + TimeToMove;
float PercentTimeLeftForMove = (agk::GetMilliseconds() - moveStart) / (float)TimeToMove;
float MovePoint = PercentTimeLeftForMove * CurrentDistance;
if (PercentTimeLeftForMove < 1.0 && TimeToEndMoving > 0.0) {
//moveSprite(50);
//MoveToward(MovePoint);
MoveFleet(CurrentLoc, MoveToLoc, agk::GetMilliseconds(), moveStart, TimeToMove, Sprite_ID);
AddScreenDebug("moveStart: " + std::string(agk::Str(PercentTimeLeftForMove)) + " " + std::string(agk::Str(TimeToEndMoving)) + " " + std::string(agk::Str(MovePoint)) + " " + std::string(agk::Str(agk::GetSpriteAngle(Sprite_ID))));
}
Distance Function:
float DistanceBetweenTwoPoints(Coordinate Start, Coordinate End) {
float distance;
float SX, SY;
//d= sqrt((x2 - x1)^2 + (y2 - y1)^2)
SX = std::pow((End.GetX() - Start.GetX())*1.0, 2.0);
SY = std::pow((End.GetY() - Start.GetY())*1.0, 2.0);
distance=sqrt(SX+SY);
//LogToFile("Start:" + PosIntToString(StartX,StartY) + Chr(9))
LogToFile("Distance: " + std::string(agk::Str(distance)));
return distance;
}
Movement Function:
void Fleet::MoveFleet(Coordinate Start, Coordinate End, int CurrentTime, int StartMovingTime, int TimeToMove, int Sprite) {
//Coordinate holds two values, the world location of an X and Y
float tempAngle = agk::GetSpriteAngle(Sprite);
int Quad = 1;
float CurrentDistance = DistanceBetweenTwoPoints(Coordinate(agk::GetSpriteX(Sprite), agk::GetSpriteY(Sprite)), End);
int TimeToEndMoving = StartMovingTime + TimeToMove;
float PercentTimeLeftForMove = (CurrentTime - StartMovingTime) / (float)TimeToMove;
//Gets the full Distance to move, the Hypotenuse of the triangle
float TotalDistance = DistanceBetweenTwoPoints(Start, End);
if (PercentTimeLeftForMove > 1.0) {
PercentTimeLeftForMove = 1.0;
}
//This movement//
//This should be 0 - the the move amount (full length of the Hypotenuse
float ThisMovement = TotalDistance * PercentTimeLeftForMove;
//Get Quadrient//
//Q1: going up and right, Q2: Going Down and Right, Q3: Going Down and Left, Q4: Going Up and Left
while(tempAngle > 90) {
Quad++;
tempAngle = tempAngle - 90;
}
//Converting to Radians
//tempAngle = (tempAngle / 360.0) * (2.0 * PI);
//Not needed now using AGK sin / cos
float y;
float x;
//As the direction of the ship rotates the Hypotenuse of the triangle switchs.
//Also when going up or left need to move by a negative number
if(Quad == 4) {
y = ((agk::Sin(tempAngle))*ThisMovement) * -1;
x = ((agk::Cos(tempAngle))*ThisMovement) * -1;
} else if(Quad == 3) {
x = ((agk::Sin(tempAngle))*ThisMovement) * -1;
y = ((agk::Cos(tempAngle))*ThisMovement);
} else if(Quad == 2) {
y = ((agk::Sin(tempAngle))*ThisMovement);
x = ((agk::Cos(tempAngle))*ThisMovement);
} else if(Quad == 1) {
x = ((agk::Sin(tempAngle))*ThisMovement);
y = ((agk::Cos(tempAngle))*ThisMovement) * -1;
}
//Adds movement to the orginal location
y = y + Start.GetY();
x = x + Start.GetX();
//Sets current position
agk::SetSpritePosition(Sprite, x, y);
}