Coordinates from angle implementation not working

270
February 25, 2017, at 10:05 AM

I have the following implementation of code to get coordinates in my multiplayer game based on rotation of the player which I cannot get to work:

GameServer.prototype.movePlayer = function(cell1, client) {
    if (client.socket.isConnected === false || client.frozen) 
    {
        console.log("SOMETHING WAS BAD!");
        return;
    }
    // TODO: use vector for distance(s)
    // get distance
    var angle = client.angle;
    var distance = 180;

    var targetX = cell1.position.x + Math.cos(angle) * distance;
    var targetY = cell1.position.y - Math.sin(angle) * distance;

    var dx = ~~(targetX - cell1.position.x);
    var dy = ~~(targetY - cell1.position.y);
    // console.log("dx was " + dx);
    // console.log("dy was " + dy);
    var squared = dx * dx + dy * dy;
    if (squared < 1 || isNaN(dx) || isNaN(dy)) {
        return;
    }
    // get movement speed
    var d = Math.sqrt(squared);
    var speed = cell1.getSpeed(d);
    if (!speed) return; // avoid shaking
    // move player cells
    cell1.position.x += dx / d * speed;
    cell1.position.y += dy / d * speed;
};

I am unable to get it to work, I get strange movement, and gets stuck. A lot of the times it does not move at all. also, if you input in a value like say 180, it moves in a diagonal direction.

Here is a working implementation which I used as an example, where the player moves towards their mouse position:

GameServer.prototype.movePlayerDefault = function(cell1, client) {
    if (client.socket.isConnected == false || client.frozen) 
    {
        return;
    }
    // TODO: use vector for distance(s)
    // get distance
    var dx = ~~(client.mouse.x - cell1.position.x);
    var dy = ~~(client.mouse.y - cell1.position.y);
    // console.log("dx was " + dx);
    // console.log("dy was " + dy);
    var squared = dx * dx + dy * dy;
    if (squared < 1 || isNaN(dx) || isNaN(dy)) {
        return;
    }
    // get movement speed
    var d = Math.sqrt(squared);
    var speed = cell1.getSpeed(d);
    if (!speed) return; // avoid shaking
    // move player cells
    cell1.position.x += dx / d * speed;
    cell1.position.y += dy / d * speed;
};
Rent Charter Buses Company
READ ALSO
How to hide the pagination bar if the number of pages is less than the number of items per page in angular

How to hide the pagination bar if the number of pages is less than the number of items per page in angular

I'm trying to hide the pagination bar when the number of pages to render is smaller than the number of items per pageI tried with ng-show but it didn't work

301
Calling A Javascript Function in a Parent Window from a link inside an iFrame

Calling A Javascript Function in a Parent Window from a link inside an iFrame

I've got a parent page and a child page issue involving JavaScript functions back and forthThe basic idea (this is for a menu system on a mobile site for thousands of pages of content) is that there is a NAV on the parent page which is hidden using CSS (z-index)...

439
running Js test getting error window is defined

running Js test getting error window is defined

running JS tests using karma + jasmine

369
Semantic UI dropdown user addition only allow email address

Semantic UI dropdown user addition only allow email address

The dropdown is being used to allow the visitor to search for users/email addresses or enter in a custom email address

798