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.