【问题标题】:Creating a circular Orbit in libgdx在 libgdx 中创建圆形轨道
【发布时间】:2017-05-04 14:21:38
【问题描述】:

我正在尝试在我的游戏中实现重力。 我目前有一个应该绕行星运行的月球(在我的游戏中都使用了 Cell 类)。

使用以下代码(缩短):

在单元格中:

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;
}

在 GravityField 类中:

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 应用到拉出的单元格上。

在 Cell 中,Cell 向重力方向移动

 pos.add(drg);

并朝着自己的motionVector vel与

pos.add(vel);

在我的 orbit() 方法中,我试图找到并设置 vel 以形成圆形轨道。

我的问题是我无法获得完美的轨道。月球仍然是轻微的,并且随着距离的缩小,向地球的距离越来越快。

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

写出我用过的:

v= sqrt((M*m)/r) 我的 OrbitVector 速度 f = M*m/r^2 向地球拉动的力量 a= f/m 向地球加速

感谢您的帮助!

【问题讨论】:

    标签: java libgdx gravity orbit


    【解决方案1】:

    这很简单。您将需要 2 种纹理,您的轨道和月亮。你所要做的就是让月球在轨道上继续旋转。

    如果你正在使用精灵,你可以这样做:

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

    但这里要注意的一个非常重要的事情是,两个图像应该具有相同的分辨率。

    我最近做了同样的事情,我什至在 stackoverflow 上问了一个问题,这是链接:

    Collision detection for an arc of a circle

    【讨论】:

    • 这不是我想要的。我正在尝试实现一个物理系统。轨道应该是动态的,并支持多个影响恒星体..
    猜你喜欢
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多