2017-06-04 23 views
0

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

+1

あなたはプレーヤーの位置とプレイヤーの向きに初期の相対位置で箇条書きを作成したいのですか?あなたは三角法(sinとcos)を知っているようです。あなたの特定の問題は何ですか? – Yunnosch

+0

可能な重複:https://stackoverflow.com/questions/44330819/how-to-calculate-coordinates-of-the-gunpoint/ – MBo

答えて

1

プレーヤー(円)がそれ自身のローカル起源であると仮定すると、ガンの位置はプレイヤーの起源に相対的です。座標系はキャンバスの座標系で、x軸に沿って左から右に、時計回りに90deg(プレーヤーの左)でY軸が下に行くと仮定します。

enter image description hereイメージ:CCから前方赤色の矢印に沿ってとローカル円原点(0,0)あるGxをGyのから銃のローカル座標であります円の中心C。左上はキャンバス座標(ワールド)システムの原点を示しています。以下のコードでは、playerの位置はその世界起源に相対的です。最後の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; 
0

X軸とY軸の違いは? 0はどこですか?angleangleは時計回りか反時計回りですか?それでも基本的な考え方は同じです。添付された画像のようにX軸が右に、Y軸が下にあり、右上に(1、-1)を追加すると仮定しましょう。また、X軸に対してangle = 0、時計方向にangleがあり、Y軸=下向きの正の方向に揃えられていると仮定する。銃が指し示されたとき、すなわちangle = -Pi/2の出発点は、であり、距離はsqrt(2)であり、さらに銃の向きに対応するPi/4に回転される。これはあなたが知る必要があるすべてです。

var angle = this.toRad(client.rotation); // rotation of the player 
    var gunStartAngle = angle + Math.PI/4; 
    var sqrt2 = Math.sqrt(2); 

    // cell.position is the players position 
    var pos = new Vec2(
     cell.position.x + cell._size * sqrt2 * Math.cos(gunStartAngle), 
     cell.position.y + cell._size * sqrt2 * Math.sin(gunStartAngle) 
    ); 

明らかにレイアウトが異なる場合は、数学の詳細を修正する必要がありますが、アイデアは変わりません。

関連する問題