2016-10-25 10 views
2

私は弾丸衝突のプロジェクトで学校に通っています。 行が真っ直ぐだがオプションがbulletspreadの場合は問題ありません。 したがって、弾丸が与えられる直線の点が与えられますが、弾丸の衝突で角度と距離からランダムな点を作成する方法はありますか? ここは弾丸衝突のコードです。距離と角度からランダムな点を得るには?

private void setEndPoint() 
{ 
    endPoint = new Point(beginPoint.x, beginPoint.y); 

    switch (direction) 
    { 
     default: 
      break; 
     case UP: 
      while (endPoint.y > 0) 
      { 
       if (tileEmptyCheck(endPoint.x, endPoint.y)) 
       { 
        endPoint.y -= 1; 
       } 
       else 
       { 
        fire(); 
        break; 
       } 
      } 
      break; 
     case LEFT: 
      while (endPoint.x > 0) 
      { 
       if (tileEmptyCheck(endPoint.x, endPoint.y)) 
       { 
        endPoint.x -= 1; 
       } 
       else 
       { 
        fire(); 
        break; 
       } 
      } 
      break; 
     case DOWN: 
      while (endPoint.y < map.getHeightInTiles() * GameState.TILESIZE) 
      { 
       if (tileEmptyCheck(endPoint.x, endPoint.y)) 
       { 
        endPoint.y += 1; 
       } 
       else 
       { 
        fire(); 
        break; 
       } 
      } 
      break; 
     case RIGHT: 
      while (endPoint.x < map.getWidthInTiles() * GameState.TILESIZE) 
      { 
       if (tileEmptyCheck(endPoint.x, endPoint.y)) 
       { 
        endPoint.x += 1; 
       } 
       else 
       { 
        fire(); 
        break; 
       } 
      } 
      break; 
     case RIGHT_UP: 
      while (endPoint.y > 0 && endPoint.x < map.getWidthInTiles() * GameState.TILESIZE) 
      { 
       if (tileEmptyCheck(endPoint.x, endPoint.y)) 
       { 
        endPoint.y -= 1; 
        endPoint.x += 1; 
       } 
       else 
       { 
        fire(); 
        break; 
       } 
      } 
      break; 
     case RIGHT_DOWN: 
      while (endPoint.y < map.getHeightInTiles() * GameState.TILESIZE && 
        endPoint.x < map.getWidthInTiles() * GameState.TILESIZE) 
      { 
       if (tileEmptyCheck(endPoint.x, endPoint.y)) 
       { 
        endPoint.y += 1; 
        endPoint.x += 1; 
       } 
       else 
       { 
        fire(); 
        break; 
       } 
      } 
      break; 
     case LEFT_DOWN: 
      while (endPoint.y < map.getHeightInTiles() * GameState.TILESIZE && 
        endPoint.x > 0) 
      { 
       if (tileEmptyCheck(endPoint.x, endPoint.y)) 
       { 
        endPoint.y += 1; 
        endPoint.x -= 1; 
       } 
       else 
       { 
        fire(); 
        break; 
       } 
      } 
      break; 
     case LEFT_UP: 
      while (endPoint.y > 0 && 
         endPoint.x > 0) 
      { 
       if (tileEmptyCheck(endPoint.x, endPoint.y)) 
       { 
        endPoint.y -= 1; 
        endPoint.x -= 1; 
       } 
       else 
       { 
        fire(); 
        break; 
       } 
      } 
      break; 
    } 
} 

答えて

1

これは単なる三角法です:極座標変換とユークリッド座標の変換です。

考えるdistance d、およびspread angle s(底角の範囲/エラー)(あなたが/撮影行くしようとしている方向)は、式があれば、例えばので

double rand = random.nextDouble(); 
int x = d * sin (b - s/2 + s * (rand)); 
int y = d * cos (b - s/2 + s * (rand)); 

あるbase angle b底角は、次いで、PI/2(90度)が、スプレッドは3~5 * PI/8(67.5度)から行くことができる* PI/8(112.5度)、

B = PI/2

ました

s =(5-3)* Pi/8 = 2 * Pi/8 = Pi/4

+0

底角と広がり角の違いは何ですか? –

+0

@TiesTheunissen私は自分の答えを更新しました – ControlAltDel

関連する問題