私は、プレイヤーが円であるマルチプレイヤーの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