"Walking" along a rotating surface in LimeJS
- by Dave Lancea
I'm trying to have a character walk along a plank (a long, thin rectangle) that works like a seesaw, being rotated around a central point by box2d physics (falling objects). I want the left and right arrow keys to move the player up and down the plank, regardless of it's slope, and I don't want to use real physics for the player movement.
My idea for achieving this was to compute the coordinate based on the rotation of the plank and the current location "up" or "down" the board.
My math is derived from here:
http://math.stackexchange.com/questions/143932/calculate-point-given-x-y-angle-and-distance
Here's the code I have so far:
movement = 0;
if(keys[37]){ // Left
movement = -3;
}
if(keys[39]){ // Right
movement = 3;
}
// this.plank is a LimeJS sprite.
// getRotation() Should return an angle in degrees
var rotation = this.plank.getRotation();
// this.current_plank_location is initialized as 0
this.current_plank_location += movement;
var x_difference = this.current_plank_location * Math.cos(rotation);
var y_difference = this.current_plank_location * Math.sin(rotation);
this.setPosition(seesaw.PLANK_CENTER_X + x_difference, seesaw.PLANK_CENTER_Y + y_difference);
This code causes the player to swing around in a circle when they are out of the center of the plank given a slight change in rotation of the plank.
Any ideas on how I can get the player position to follow the board position?