Rotating walls around the player.

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!

Share this post

Link to post
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.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.