2017-05-04 40 views
1

我想在我的游戏中实现Gravity。 我目前有一个月球应该绕行星运行(两者都使用我的游戏中的Cell类)。在libgdx中创建一个圆形轨道

下面的代码被使用(缩短):

在类细胞:

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; 
} 
与我的重力场

,所以我基本上施加dragVector到被拉的细胞。

细胞,细胞是朝graviator移动与

pos.add(drg); 

,并朝与

pos.add(vel); 

在我的轨道上()方法,我试图找到并设置VEL自己的movementVector VEL做一个圆形轨道。

我的问题是,我莫名其妙地无法完成轨道。月亮仍然略微缩小,距离越来越远,对地球的影响越来越大。

我不知道为什么它不工作。我是否使用正确的公式?

写出我用:

v= sqrt((M*m)/r)我OrbitVector速度 f = M*m/r^2用于向地球 a= f/m为加速向地球

谢谢您的帮助蒲凌力!

回答

0

这很简单。有2个纹理你将需要,你的轨道和月球。你必须做的是继续在轨道上旋转月球。

如果您使用的是精灵,你可以这样做:

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

不过这里要注意一个非常重要的事情是,无论是图像应具有相同的分辨率。

我最近做了同样的事情,我甚至问了一个问题关于它的计算器,这里是链接:

Collision detection for an arc of a circle

+0

好that's不是我想要什么。 我想实现一个物理系统。轨道应该是动态的并且支持多重影响星体。 – Superwutz