【发布时间】:2017-06-04 11:27:55
【问题描述】:
我有一个多人 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
}
}
};
【问题讨论】:
-
您想创建具有相对于玩家位置和玩家方向的初始位置的子弹?你似乎知道三角学(sin 和 cos)。您的具体问题是什么?
标签: javascript node.js math trigonometry calculus