我有一個多人遊戲的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)。你的具體問題是什麼? – Yunnosch
可能的重複:https://stackoverflow.com/questions/44330819/how-to-calculate-coordinates-of-the-gunpoint/ – MBo