java - 在 libgdx 中创建圆形轨道

标签 java libgdx gravity orbit

我正在尝试在我的游戏中实现重力。 我目前有一个应该绕行星运行的月亮(在我的游戏中都使用 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;
}

在重力场类中:

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 中,Cell 朝重力器移动

 pos.add(drg);

并朝着它自己的运动 vector 速度

pos.add(vel);

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

我的问题是我无法实现完美的轨道。月球仍然稍微靠近地球,并且随着距离的缩短,它越来越加速地接近地球。

我不明白为什么它不起作用。我使用了正确的公式吗?

写出我使用的:

v= sqrt((M*m)/r) 表示我的 OrbitVector 速度 f = M*m/r^2 用于拉向行星的力 a= f/m 表示朝向行星的加速度

感谢您的帮助!

最佳答案

这非常简单。您需要 2 个纹理:轨道和月亮。你所要做的就是继续让月球在轨道上旋转。

如果您使用 sprite ,您可以这样做:

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

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

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

Collision detection for an arc of a circle

关于java - 在 libgdx 中创建圆形轨道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43785527/

相关文章:

android - 发布使用 Libgdx 创建的 apk

java - 如何在没有 box2d libgdx 的情况下对对象施加脉冲

java - 使用 IntelliJ 自动替换为 var

iOS 游戏本地化与 lproj 目录不起作用

Java:找出哪个观察者正在监听

java - 'Tween Engine' 类的 Eclipse dalvik 链接在 android 上失败但在桌面上工作

android - 如何将对话框定位到底部

java - 快速多体重力算法?

java - 如何在hibernate环境中查询RevisionEntity

java - Eclipse QuickFix 功能