libgdx / gdx-ai

Artificial Intelligence framework for games based on libGDX or not. Features: Steering Behaviors, Formation Motion, Pathfinding, Behavior Trees and Finite State Machines
Apache License 2.0
1.2k stars 242 forks source link

Physics incoherence in examples #36

Closed Chulepa closed 9 years ago

Chulepa commented 9 years ago

Hi,

I was looking the examples on steering behavior, and I found ans issue. I'm not 100% sure I got it right, but let me explain. On the Scene2D example, the steeringOutput is calculated, and scaled with time and added to the velocity.

private void applySteering (SteeringAcceleration<Vector2> steering, float time) {
        // Update position and linear velocity. Velocity is trimmed to maximum speed
        position.mulAdd(linearVelocity, time);
        linearVelocity.mulAdd(steering.linear, time).limit(getMaxLinearSpeed());
}

So the steering linear is treated as a force. On the other hand, on the example with box2D, the steeringOutup is calculated, then scaled to deltaTime, and finaly applyed as a force to the body:

protected void applySteering (SteeringAcceleration<Vector2> steering, float deltaTime) {
        boolean anyAccelerations = false;

        // Update position and linear velocity.
        if (!steeringOutput.linear.isZero()) {
            Vector2 force = steeringOutput.linear.scl(deltaTime);
            body.applyForceToCenter(force, true);
            anyAccelerations = true;
}

In this case the steeringOutput.linear is scaled to delta before is used as a force. Then the box2D will scale again with dalta before applying to velocity. The result is a much weaker force.

As I sad, I am not sure if I've wrong understood it, or it's a mistake on the examples. And if so, which of the to cases should we use in our code?

Thanks, StefanW

davebaol commented 9 years ago

I'm quite sure scene2d sample is correct. If box2d internally scales the acceleration by delta once again (which sounds reasonable, of course) then box2d sample is wrong. I'll look into it more deeply in the next days. Thanks for the report :)

davebaol commented 9 years ago

@Chulepa According to this box2d thread you're right. The applyForce method takes delta time into account already. The same goes for applyTorque, I guess.

Of course, after fixing the applySteering method, steering parameters in all box2d tests will need some tweaking.

Also, bullet tests are likely affected by a similar issue. :worried:

davebaol commented 9 years ago

Fixed box2d tests, see commit c63f3c085c9c61870c20fd13a184e08fa744e5f4 Bullet tests not fixed yet