0

The title describes my problem, as you can see here my character is spawning at a 45 degree angle. The character always spawns at (0, 0)

I have created some code for animations that uses the character's position and desired position to calculate at what angle the character should be at. This angle is determined by using the percentage of distance the character has travelled to its destination (these destinations are only Y values. X is increased by 1 every tick). I have made an equation to determine the angle based on the percentage travelled. The equation: -(9/500)X^2 + (9/5)X. This is a parabola that intersects point (0, 0), (50, 45) ← 45 degree angle and (100, 0).

The code I made to use this equation is as follows:

float GetRotationDegrees(int Current, int Target, int LaneWidth) {
    int numerator = Current - LaneWidth;
    int denominator = Target - LaneWidth;
    float X;
    if (denominator == 0) X = 0;
    else X = abs(numerator / denominator * 100);
    return -(9/500)*pow(X,2)+(9/5)*X; // function: -(9/500)X^2 + (9/5)X.
}

The LaneWidth is always 400, Current is the character's current Y coordinate and Target is the target Y coordinate. At Y = 0 the equation should be -400 / -400 * 100 = 100. And when you put 100 in the equation you get 0, this value of 0 is then used like this:

SetActorLocation(FVector(1, sin(angle * PI / 180), 0).Rotation());

I am really confused right now because whenever I manually try this it won't work.

It is probably something stupid I did, I tend to look over things a lot of the time and am not that good at maths (I am also new to C++, so the mistake might be there too). If someone could help me I would really appreciate it!

(By the way, I know that the way that GetRotationDegrees decides X the angle will only return positive angles, I want to get this to work first)

timgfx
  • 169
  • 2
  • 11

1 Answers1

0

Fixed it with this:

float GetRotationDegrees(int Current, int Target, int LaneWidth) {
    if (Current == Target) return 0;
    int MovingFrom;
    if (Target != 0) MovingFrom = 0;
    else if (Target > Current) MovingFrom = -LaneWidth;
    else MovingFrom = LaneWidth;

    float Percentage = float(Current - MovingFrom) / float(Target - MovingFrom) * 100;
    if (Target < Current) Percentage = -Percentage;
    bool isNegative = Percentage < 0;
    if (isNegative) return -(-9 * pow(abs(Percentage), 2) / 500 + 9 * abs(Percentage) / 5);
    return -9 * pow(Percentage, 2) / 500 + 9 * Percentage / 5;
}
timgfx
  • 169
  • 2
  • 11