1

I am making a minigame in which a player can jump on to a spinning propeller. I am having two, possible, related bugs that I cannot track down. When not on the propeller, movement is simply done by adjusting the players velocity vector and adding that to the change in position vector

velocityVector.x += moveSpeed;
x += velocityVector.x; 

When on the propeller the player has to move in a circular motion.

I do this by float radius = vectorBetweenPlayerandPropeller.len();

if (propeller.getRotation() != 0)
{
    velocityVector.x = (float) (propeller.anchorPoint.x + radius * Math.cos(Math.toRadians(propeller.getTotalRotation() ))) - positionVector.x;
    velocityVector.y = (float) (propeller.anchorPoint.y   + radius * Math.sin(Math.toRadians(propeller.getTotalRotation() ))) - positionVector.y;
}
else
{
    velocityVector.x = 0;
    velocityVector.y = 0;
}       

Problem 1: When I land on the rotating propeller (non-rotating works fine) I am instantly repositioned to the far end of the propeller. There must be something wrong with how I calculate the velocity. Probably, I cannot find the velocity by calculating the new x value according to circular motion and then withdraw the current position (positionVector.x). Also, maybe, the

Math.toRadians(propeller.getTotalRotation() )

is buggy.The rotation is currently clockwise, as this works nicely when drawing the rotating propeller. However, the world of radians is counter-clockwise so maybe the line should be something like

Math.toRadians(360 - propeller.getTotalRotation() )

When I do this, however, the player does not follow the rotating propeller anymore.

Problem 2: The player also has to be able to move up and down the rotating propeller. I do this by somewhat messy code:

//360-rotation due to clockwise/counter-clockwise difference between radians and degrees. 
double adjustedRotationRad = Math.toRadians(360-propeller.getTotalRotation());

if(left) {
    //How is the propeller incline? Should the velocityVector.y be positive? (first case, hence we move downwards since position 0,0 is top left)
    // or should we move upwards? (negative velocityVector.y)
    velocityVector.x -= moveSpeed*Math.abs(Math.cos(adjustedRotationRad));
    if (((90 > (propeller.getTotalRotation()) && (propeller.getTotalRotation()) > 0)) ||
    ((360 > (propeller.getTotalRotation()) && (propeller.getTotalRotation()) > 270)))
    velocityVector.y += moveSpeed*Math.sin(adjustedRotationRad);

    else if ((270 > (propeller.getTotalRotation()) && (propeller.getTotalRotation()) > 90))
    velocityVector.y -= moveSpeed*Math.sin(adjustedRotationRad);
}

The bug is a bit strange: when moving on the propeller I cannot pass the middle of the propeller, the player is simply stuck.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user2913053
  • 93
  • 1
  • 9
  • how do you determine which direction to go in when you are on the propeller? Is it simply the player's "normal" velocity (what it would be when not attached) projected on to the propeller's span vector? – meowgoesthedog Aug 12 '17 at 14:02
  • I calculate the distance between the player and the propeller anchorpoint, termed radius. I then initially calculate x and y. Shown for x: velocityVector.x = (float) (propeller.anchorPoint.x + radius * Math.cos(Math.toRadians(propeller.getTotalRotation() ))) which gets the new position. To turn this into the velocity.x I substract the position of the player. – user2913053 Aug 13 '17 at 18:37
  • That's not what I meant; but now I've seen your solution for problem 2, the important mistake seems to be the use of `Math.abs`. This is causing an effective reversal in velocity when you cross the center. – meowgoesthedog Aug 13 '17 at 20:09

0 Answers0