朝向目的地的轨迹

时间:2015-09-08 10:28:34

标签: java android libgdx game-physics

我正在使用libgdx进行小型安卓游戏,而且我已经在物理上奋斗了几天。

想法只是以抛物线轨迹在指定位置射球。 我发现关于我的问题的主题很少,我得到了这个等式。

https://en.wikipedia.org/wiki/Trajectory_of_a_projectile#Angle_required_to_hit_coordinate_.28x.2Cy.29

修改

我想出了这段代码:

public void update(float deltaTime) {
    if (!freeze) {
        position.add(velocity.x * deltaTime, velocity.y * deltaTime, velocity.z * deltaTime);
        velocity.scl(1 - (GameWorld.GRAVITY * deltaTime));
        if (position.y < 1) {
            velocity.y = (- velocity.y/2)+1;
        } else {
            velocity.y += GameWorld.GRAVITY;
        }
    }
}

public void shootToward(Vector3 destination, float v, boolean direct) {
    float distX = destination.x - position.x;
    //float distZ = destination.z - position.z;
    float distZ = 0;
    float altitude = destination.y - position.y;
    float distance = (float)(Math.sqrt(distX * distX + distZ * distZ));
    double sqrt = (v*v*v*v) - (GameWorld.GRAVITY * (GameWorld.GRAVITY * distance*distance + 2 * altitude * v*v));
    sqrt = Math.sqrt(sqrt);
    float angle;
    if (direct)
        angle = (float)Math.atan((v*v - sqrt) / (GameWorld.GRAVITY*distance));
    else
        angle = (float)Math.atan((v*v + sqrt) / (GameWorld.GRAVITY*distance));
    Gdx.app.log("Ball", "Degrees: " + Math.toDegrees(angle));
    float newX = v * (float)Math.cos(angle);
    float newY = v * (float)Math.sin(angle);
    velocity.set(newX, newY, 0);
}

这对大多数人来说似乎有用,但在我看来,积极解决方案的角度始终是89' 否定也不起作用。 试图调整值,但它仍然失败。

此外,重力应该是正值还是负值?

我很感激帮助

修改

示例镜头:

v = 50 direct=false
Ball: Position: 0.0, 50.0
Ball: Destination: 100.0, 0.0
Ball: Degrees: -88.8654739323932
Ball: Velocity: 0.9899961, -49.990196
Ball: Landed: 0.8182346, -4.0932293

v = 100 direct=false
Ball: Position: 0.0, 50.0
Ball: Destination: 100.0, 0.0
Ball: Degrees: -89.71855772953862
Ball: Velocity: 0.49120748, -99.998795
Ball: Landed: 0.2182493, 1.7965958

v = 50 direct=true
Ball: Position: 0.0, 50.0
Ball: Destination: 100.0, 0.0
Ball: Degrees: -27.699576046892837
Ball: Velocity: 44.269855, -23.241776
Ball: Landed: 59.603405, 1.6399858

v = 100 direct=true
Ball: Position: 0.0, 50.0
Ball: Destination: 100.0, 0.0
Ball: Degrees: -26.846403457288204
Ball: Velocity: 89.22204, -45.160027
Ball: Landed: 73.34829, 0.033889055

0 个答案:

没有答案