2017-05-04 7 views
1

私はゲームで重力を実装しようとしています。 私は現在、惑星を周回するはずの月を持っています(どちらも私のゲームでクラスCellを使用しています)。libgdxで円形軌道を作成する

次のコードが使用されている(短縮):クラスのセルに

:クラスGravityFieldで

private Cell graviator; 
private Vector2 pos; 
private Vector2 vel; 
private Vector2 drg; 
private int mass; 
private float drgSpeed; 
private GravityField grav=new GravityField(this); 

public void move(){ 
    calculateDrag(); 
    if(orbiting){ 
     orbit(); 
    } 
    pos.add(vel); 
    pos.add(drg); 
} 

private void calculateDrag() { 
    drg.scl(drgSpeed); 
} 

private void orbit(){ 
    Vector2 temp=new Vector2(drg.x,drg.y); 
    temp.rotate90(0); 
    temp.nor(); 
    speed=(float)Math.sqrt((mass+graviator.getMass())/getGraviatorDistance()); // v= sqrt((M*m)/r) 
    temp.scl(speed); 
    vel=temp; 
} 

private Cell cell; 

public void computeGravity(Cell cell2) { 
    float force = getForce(cell2); 
    if (cell != cell2.getGraviator()) { 
     if (cell2.getMass() < cell.getMass()) { 
      if (force > cell2.getLargestForce()) { 
       cell2.setGraviator(cell); 
       cell2.setLargestForce(force); 
       cell2.setDrg(getDragVector(cell2)); 
       cell2.setDrgAcc(getAcceleration(cell2)); 
      } 
     } 
    } else { 
     cell2.setLargestForce(force); 
     cell2.setDrg(getDragVector(cell2)); 
     cell2.setDrgAcc(getAcceleration(cell2)); 
    } 
} 

public Vector2 getDragVector(Cell cell2) { 
    Vector2 temp = new Vector2(cell.getPos().x - cell2.getPos().x, cell.getPos().y - cell2.getPos().y); 
    temp.nor(); 
    return temp; 
} 

public Vector2 getDirection(Cell cell2) { 
    return new Vector2(cell.getPos().x - cell2.getPos().x, cell.getPos().y - cell2.getPos().y); 
} 

public float getDistance(Vector2 direction) { 
    return direction.len(); 
} 

public float getForce(Cell cell2) { 
    Vector2 temp = new Vector2(cell.getPos().x - cell2.getPos().x, cell.getPos().y - cell2.getPos().y); 
    return cell.getMass() * cell2.getMass()/(temp.len() * temp.len()); //f = M*m/r^2 
} 

public float getAcceleration(Cell cell2) { 
    Vector2 temp = new Vector2(cell.getPos().x - cell2.getPos().x, cell.getPos().y - cell2.getPos().y); 
    float force = cell.getMass() * cell2.getMass()/(temp.len() * temp.len()); 
    float acceleration = force/cell2.getMass(); // a= f/m 
    return acceleration; 
} 

だから私のGravityFieldで、私は基本的にdragVectorを適用しています引っ張られた細胞。

セルには、セルが

pos.add(drg); 

とし、

pos.add(vel); 
私の軌道上で

()メソッド私が見つけることを試みていると、設定されたVELを持つ独自のmovementVectorのVELに向けてgraviatorに向かって移動します円軌道を作る。

私の問題は、どうにかして完全な軌道を達成できないということです。月はやや僅かであり、ますます惑星に向かってますます加速していきます。

なぜ動作しないのかわかりません。適切な数式を使用しますか?私が使用して書かれた

:私のOrbitVector速度に対する

v= sqrt((M*m)/r) f = M*m/r^2を地球に向けて加速するために惑星 a= f/mに向けて

をpuling力のためには、あなたの助けをありがとう!

答えて

0

かなりシンプルです。あなたが必要とする2つのテクスチャ、あなたの軌道と月があります。あなたがしなければならないことは、月を軌道上で回転させ続けることです。

あなたがスプライトを使用している場合は、あなたがそうすることができます:ここで注意すべき

sprite.setRotation(rotationVaue); 
rotationValue+=1; 

しかし、非常に重要なことは、画像の両方が同じ解像度を持つべきであるということです。私は望んでいない、まさに

Collision detection for an arc of a circle

+0

まあthat's:

が、私は最近、同じ事をした、私もstackoverflowの上でそれについて質問をし、ここにリンクです。 私は物理システムを実装しようとしています。軌道は動的で、多様な恒星の身体を支えるものでなければなりません。 – Superwutz

関連する問題