I'm trying to build a simple top-down game in P5js where the player is centred on the screen and the map is rotated around the player. To test it , I've drawn a square to see how the map is moving. The square isn't rotating around the player, though, its rotating around the translated centre point.
I've tried to move around where the axes are rotated and translated however, this messes up the player movement, locking it to the rotated axes. I've also tried drawing the cube in relation to the players (x,y) coordinates, thinking this would make sure the origin isn't moved, however it still has the same problem.
draw function
let lookAngle = 0;
let mouseSensitivity = 0.001;
function draw() {
translate(width/2, height/2);
scale(1, -1);
background(180);
player.update();
lookAngle += movedX * mouseSensitivity;
push();
translate(player.position);
rotate(lookAngle);
rect(100, 100, 50, 50);
pop();
}
player update
update() {
let acceleration = 3;
let maxSpeed = 20;
let xSpeed = 0;
let ySpeed = 0;
if (keyIsDown(65)) {
xSpeed += acceleration;
} if (keyIsDown(68)) {
xSpeed -= acceleration;
} if (keyIsDown(87)) {
ySpeed -= acceleration;
} if (keyIsDown(83)) {
ySpeed += acceleration;
}
if (xSpeed > maxSpeed) {
xSpeed = maxSpeed;
} if (xSpeed < -maxSpeed) {
xSpeed = -maxSpeed;
} if (ySpeed > maxSpeed) {
ySpeed = maxSpeed;
} if (ySpeed < -maxSpeed) {
ySpeed = -maxSpeed;
}
this.xPosition += xSpeed;
this.yPosition += ySpeed;
this.position.set(this.xPosition, this.yPosition);
fill(100);
rect(0, 10, 5, 15);
fill(255);
circle(0, 0, 20);
}