javascript - 根据旋转 Angular 计算新的圆偏移?

标签 javascript node.js math trigonometry calculus

我有一个多人 Javascript 游戏,其中玩家是一个圆圈,并且能够沿着玩家旋转的方向射击/“弹出”圆圈子弹。我的代码工作完美,除了它从播放器的中间射击。我希望圆圈是从玩家的右上角位置(枪所在的位置)射击的。问题是,当球员轮换发生变化时,您不能简单地将 (1, -1) 添加到球员的位置。

这是我的代码:

GameServer.prototype.ejectMass = function(client) {

    for (var i = 0; i < client.cells.length; i++) {
        var cell = client.cells[i]; // the player


        var angle = this.toRad(client.rotation); // rotation of the player

        var d = new Vec2(Math.sin(angle), Math.cos(-angle)).scale(-180);

        var sq = (~~d.sqDist(d));

        d.x = sq > 1 ? d.x / sq : 1;
        d.y = sq > 1 ? d.y / sq : 0;

        // cell.position is the players position
        var pos = new Vec2(
            cell.position.x + d.x * cell._size,
            cell.position.y + d.y * cell._size
        );

        var ejected = 0;

        // Create cell and add it to node list

        ejected = new Entity.EjectedMass(this, null, pos, this.config.ejectSize * cell.ejectSize); // the bullet being shot

        ejected.setBoostDefault(-this.config.ejectVelocity * cell.ejectSpeed, angle);

        ejected.ejectedOwner = cell; // set the person who shot the bullet 

        this.addNode(ejected); // add the bullet into the game

        if (typeof ejected !== 'undefined') {
            setTimeout(this.removeNode.bind(this, ejected), 1000); // remove the ejected bullet after 1 second
        }
    }
};

这是当前工作方式的说明: eject illustration

最佳答案

假设玩家(圆圈)位于其自己的本地原点,那么枪的位置是相对于玩家的原点的。假设坐标系为 Canvas 坐标系,从左向右沿x轴向前,顺时针90度(玩家左侧)为Y轴向下。

enter image description here 图片:C 是本地圆原点 (0,0),从 C 沿红色箭头向前,< strong>Gx和Gy是枪距圆心C的局部坐标。左上角显示 Canvas 坐标(世界)系统原点。在下面的代码中,玩家位置是相对于世界原点的。最终的gunPos也是相对于世界坐标给出的。 B vec 是子弹 bullet.delta 向量

const bulletSpeed = 10;
var gunPos = {x : 10, Y : 10} // ten units forward ten units left of circle center
var player = {rotation : ?, x : ?, y : ?} // unknown player position and rotation



// get the unit vector of the rotated x axis. Along player forward
var xAx = Math.cos(player.rotation);
var xAy = Math.sin(player.rotation);

// transform the gunpos to absolute position (world coordinates) of rotated player 
var rotatedGunPos = {};
rotatedGunPos.x = gunPos.x * xAx - gunPos.y * xAy + player.x;
rotatedGunPos.y = gunPos.x * xAy + gunPos.y * xAx + player.y;

// and fire the bullet from 
var bullet = {}
bullet.x = rotatedGunPos.x;
bullet.y = rotatedGunPos.y;

// bullet vector is
bullet.deltaX = xAx * BULLET_SPEED;
bullet.deltaY = xAy * BULLET_SPEED;

关于javascript - 根据旋转 Angular 计算新的圆偏移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44353557/

相关文章:

Javascript 函数总是返回 null

javascript - 在 gridview 中选择日期时间并更改 gridview 中日期选择的下一个文本框值

node.js - 运行 `ng serve`后出现 `ng update @angular/cli @angular/core`错误,无法为Angular项目提供服务

node.js - 将 .npmrc 文件添加到 Angular 项目

python - 为什么 node.js 需要 python

javascript - 在 Jasmine 中模拟假按键

javascript - 如何检查变量是类还是 id(jQuery)

php - 查找 "best fit"方程

c - 考虑稀疏矩阵获取转置矩阵(可能与排序有关)

algorithm - 分析序列的算法