Jump to content

Rotating walls around the player.


CodedGames
 Share

Recommended Posts

Hello, so I'm trying to make it so that the player can 'rotate' the camera around them. For example when they press E and Q the world rotates and things like walls get repositioned and their angle changes. Here is the code I have written:

function rotateWorld(angle) {
    for (var i = 0; i < walls.getAll().length; i++) {
        var wall = walls.getAll()[i];
        wall.angle += angle;
        var delta_x = wall.x - player.x;
        var delta_y = wall.y - player.y;
        var thetaAngle = Math.atan2(delta_y, delta_x) * 180 / Math.PI;
        wall.x = player.x + lengthdir_x(distance(wall.x, player.x, wall.y, player.y), thetaAngle + angle);
        wall.y = player.y + lengthdir_y(distance(wall.x, player.x, wall.y, player.y), thetaAngle + angle);
    }
}

function lengthdir_x(length, direction) {
    return Math.cos(direction * Math.PI / 180) * length;
}

function lengthdir_y(length, direction) {
    return Math.sin(direction * Math.PI / 180) * length;
}

function distance(x1, x2, y1, y2) {
    return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}

So far this sort of works but objects lose proper positioning after rotating a bit. It seems like they are not rotating around a perfect circle. 

Any help would really be appreciated and if there is a way to do this that's easier please let me know. Thanks!

Link to comment
Share on other sites

Ok thanks guys I got things fixed. The problem was that I calculated the distance between two points, adjusted the x position, then calculated the distance again when adjusting the y position. This caused things to have their distance from the pivot point changed when it shouldn't.

 

The only thing left is I need to get collisions to work with rotated sprites. Originally I was using the arcade physics but I heard I need to switch over to P2 for rotation to work.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...