Example usage for com.badlogic.gdx.math MathUtils FLOAT_ROUNDING_ERROR

List of usage examples for com.badlogic.gdx.math MathUtils FLOAT_ROUNDING_ERROR

Introduction

In this page you can find the example usage for com.badlogic.gdx.math MathUtils FLOAT_ROUNDING_ERROR.

Prototype

float FLOAT_ROUNDING_ERROR

To view the source code for com.badlogic.gdx.math MathUtils FLOAT_ROUNDING_ERROR.

Click Source Link

Usage

From source file:coder5560.gdxai.SteeringActor.java

License:Apache License

private void applySteering(SteeringAcceleration<Vector2> steering, float time) {
    // Update position and linear velocity. Velocity is trimmed to maximum
    // speed/*from   w w w .j  a  va  2s .c  o  m*/
    position.mulAdd(linearVelocity, time);
    linearVelocity.mulAdd(steering.linear, time).limit(getMaxLinearSpeed());

    // Update orientation and angular velocity
    if (independentFacing) {
        setRotation(getRotation() + (angularVelocity * time) * MathUtils.radiansToDegrees);
        angularVelocity += steering.angular * time;
    } else {
        // If we haven't got any velocity, then we can do nothing.
        if (!linearVelocity.isZero(MathUtils.FLOAT_ROUNDING_ERROR)) {
            float newOrientation = vectorToAngle(linearVelocity);
            angularVelocity = (newOrientation - getRotation() * MathUtils.degreesToRadians) * time; // this is superfluous if independentFacing is
                                                                                                    // always true
            setRotation(newOrientation * MathUtils.radiansToDegrees);
        }
    }
}

From source file:com.badlogic.gdx.ai.tests.steer.box2d.Box2dRaycastCollisionDetector.java

License:Apache License

@Override
public boolean findCollision(Collision<Vector2> outputCollision, Ray<Vector2> inputRay) {
    callback.collided = false;//from   w  ww .  j av a  2s  .  co  m
    if (!inputRay.start.epsilonEquals(inputRay.end, MathUtils.FLOAT_ROUNDING_ERROR)) {
        callback.outputCollision = outputCollision;
        world.rayCast(callback, inputRay.start, inputRay.end);
    }
    return callback.collided;
}

From source file:com.badlogic.gdx.ai.tests.steer.box2d.Box2dSteeringEntity.java

License:Apache License

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;//from   w  ww.j  a  v  a2s . c  om
    }

    // Update orientation and angular velocity
    if (isIndependentFacing()) {
        if (steeringOutput.angular != 0) {
            body.applyTorque(steeringOutput.angular * deltaTime, true);
            anyAccelerations = true;
        }
    } else {
        // If we haven't got any velocity, then we can do nothing.
        Vector2 linVel = getLinearVelocity();
        if (!linVel.isZero(MathUtils.FLOAT_ROUNDING_ERROR)) {
            float newOrientation = vectorToAngle(linVel);
            body.setAngularVelocity((newOrientation - getAngularVelocity()) * deltaTime); // this is superfluous if independentFacing is always true
            body.setTransform(body.getPosition(), newOrientation);
        }
    }

    if (anyAccelerations) {
        // body.activate();

        // TODO:
        // Looks like truncating speeds here after applying forces doesn't work as expected.
        // We should likely cap speeds form inside an InternalTickCallback, see
        // http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Simulation_Tick_Callbacks

        // Cap the linear speed
        Vector2 velocity = body.getLinearVelocity();
        float currentSpeedSquare = velocity.len2();
        float maxLinearSpeed = getMaxLinearSpeed();
        if (currentSpeedSquare > maxLinearSpeed * maxLinearSpeed) {
            body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float) Math.sqrt(currentSpeedSquare)));
        }

        // Cap the angular speed
        float maxAngVelocity = getMaxAngularSpeed();
        if (body.getAngularVelocity() > maxAngVelocity) {
            body.setAngularVelocity(maxAngVelocity);
        }
    }
}

From source file:com.badlogic.gdx.ai.tests.steer.bullet.SteeringBulletEntity.java

License:Apache License

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

    // Update position and linear velocity
    if (!steeringOutput.linear.isZero()) {
        body.applyCentralForce(steeringOutput.linear.scl(deltaTime));
        anyAccelerations = true;/*w  ww  . ja v  a  2  s .co  m*/
    }

    // Update orientation and angular velocity
    if (isIndependentFacing()) {
        if (steeringOutput.angular != 0) {
            body.applyTorque(tmpVector3.set(0, steeringOutput.angular * deltaTime, 0));
            anyAccelerations = true;
        }
    } else {
        // If we haven't got any velocity, then we can do nothing.
        Vector3 linVel = getLinearVelocity();
        if (!linVel.isZero(MathUtils.FLOAT_ROUNDING_ERROR)) {
            // 
            // TODO: Commented out!!!
            // Looks like the code below creates troubles in combination with the applyCentralForce above
            // Maybe we should be more consistent by only applying forces or setting velocities.
            //
            //            float newOrientation = vectorToAngle(linVel);
            //            Vector3 angVel = body.getAngularVelocity();
            //            angVel.y = (newOrientation - oldOrientation) % MathUtils.PI2;
            //            if (angVel.y > MathUtils.PI) angVel.y -= MathUtils.PI2;
            //            angVel.y /= deltaTime;
            //            body.setAngularVelocity(angVel);
            //            anyAccelerations = true;
            //            oldOrientation = newOrientation;
        }
    }
    if (anyAccelerations) {
        body.activate();

        // TODO:
        // Looks like truncating speeds here after applying forces doesn't work as expected.
        // We should likely cap speeds form inside an InternalTickCallback, see
        // http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Simulation_Tick_Callbacks

        // Cap the linear speed
        Vector3 velocity = body.getLinearVelocity();
        float currentSpeedSquare = velocity.len2();
        float maxLinearSpeed = getMaxLinearSpeed();
        if (currentSpeedSquare > maxLinearSpeed * maxLinearSpeed) {
            body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float) Math.sqrt(currentSpeedSquare)));
        }

        // Cap the angular speed
        Vector3 angVelocity = body.getAngularVelocity();
        if (angVelocity.y > getMaxAngularSpeed()) {
            angVelocity.y = getMaxAngularSpeed();
            body.setAngularVelocity(angVelocity);
        }
    }
}

From source file:com.badlogic.gdx.ai.tests.steer.scene2d.SteeringActor.java

License:Apache License

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());

    // Update orientation and angular velocity
    if (independentFacing) {
        setRotation(getRotation() + (angularVelocity * time) * MathUtils.radiansToDegrees);
        angularVelocity += steering.angular * time;
    } else {/*  www  .  ja va  2 s  . co  m*/
        // If we haven't got any velocity, then we can do nothing.
        if (!linearVelocity.isZero(MathUtils.FLOAT_ROUNDING_ERROR)) {
            float newOrientation = vectorToAngle(linearVelocity);
            angularVelocity = (newOrientation - getRotation() * MathUtils.degreesToRadians) * time; // this is superfluous if independentFacing is always true
            setRotation(newOrientation * MathUtils.radiansToDegrees);
        }
    }
}

From source file:toniarts.openkeeper.world.creature.steering.CreatureRayCastCollisionDetector.java

License:Open Source License

@Override
public boolean findCollision(Collision<Vector2> outputCollision, Ray<Vector2> inputRay) {

    if (!inputRay.start.epsilonEquals(inputRay.end, MathUtils.FLOAT_ROUNDING_ERROR)) {

        // Don't do a real ray cast, 2D this
        Point collisionPoint = getCollisionWall((int) Math.floor(inputRay.start.x),
                (int) Math.floor(inputRay.start.y), (int) Math.floor(inputRay.end.x + 1),
                (int) Math.floor(inputRay.end.y + 1));

        if (collisionPoint != null) {

            Line line = new Line(new Vector3f(inputRay.start.x, 0.25f, inputRay.start.y),
                    new Vector3f(inputRay.end.x, 0.25f, inputRay.end.y));
            line.setLineWidth(2);/*ww  w  .j av  a 2  s. c o  m*/
            Geometry geometry = new Geometry("Bullet", line);
            Material orange = new Material(worldState.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
            orange.setColor("Color", ColorRGBA.Red);
            orange.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
            geometry.setCullHint(Spatial.CullHint.Never);
            geometry.setMaterial(orange);
            worldState.getWorld().attachChild(geometry);

            if (outputCollision != null) {

                // FIXME: we could somehow 2D this, or 3D physics this
                com.jme3.math.Ray r = new com.jme3.math.Ray();
                r.setOrigin(new Vector3f(inputRay.start.x, 0.5f, inputRay.start.y));
                Vector2 v = inputRay.end.sub(inputRay.start);
                r.setDirection(new Vector3f(v.x, 0, v.y));
                CollisionResults result = new CollisionResults();

                Box b = new Box(new Vector3f(collisionPoint.x - 0.5f, 0.5f, collisionPoint.y - 0.5f), 0.5f,
                        0.5f, 0.5f);
                geometry = new Geometry("Bullet", b);
                TangentBinormalGenerator.generate(b);
                geometry.collideWith(r, result);
                //                    r.collideWith(geometry, result);

                //                    outputCollision.point = new Vector2(result.getClosestCollision().getContactPoint().x, result.getClosestCollision().getContactPoint().z);
                //                    outputCollision.normal = new Vector2(result.getClosestCollision().getContactNormal().x, result.getClosestCollision().getContactNormal().z);
                //                    line = new Line(new Vector3f(outputCollision.point.x, 1.25f, outputCollision.point.y), new Vector3f(outputCollision.point.x + 1, 1.25f, outputCollision.point.y));
                //                    line = new Line(new Vector3f(collisionPoint.x1, 1.05f, collisionPoint.y1), new Vector3f(collisionPoint.x2, 1.05f, collisionPoint.y2));
                //                    line.setLineWidth(2);
                if (result.getClosestCollision() != null) {
                    outputCollision.point = new Vector2(result.getClosestCollision().getContactPoint().x,
                            result.getClosestCollision().getContactPoint().z);
                    outputCollision.point = new Vector2(result.getClosestCollision().getContactNormal().x,
                            result.getClosestCollision().getContactNormal().z);

                    b = new Box(new Vector3f(result.getClosestCollision().getContactPoint().x, 1.5f,
                            result.getClosestCollision().getContactPoint().z), 0.1f, 0.1f, 0.1f);
                    geometry = new Geometry("Bullet", b);

                    orange = new Material(worldState.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
                    orange.setColor("Color", ColorRGBA.Red);
                    orange.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
                    geometry.setCullHint(Spatial.CullHint.Never);
                    geometry.setMaterial(orange);
                    worldState.getWorld().attachChild(geometry);
                }
            }

            return true;
        } else {
            Line line = new Line(new Vector3f(inputRay.start.x, 0.25f, inputRay.start.y),
                    new Vector3f(inputRay.end.x, 0.25f, inputRay.end.y));
            line.setLineWidth(2);
            Geometry geometry = new Geometry("Bullet", line);
            Material orange = new Material(worldState.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
            orange.setColor("Color", ColorRGBA.Green);
            orange.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
            geometry.setCullHint(Spatial.CullHint.Never);
            geometry.setMaterial(orange);
            //                world.getWorld().attachChild(geometry);
        }
    }

    //            Vector3f start = new Vector3f(inputRay.start.x, 0.25f, inputRay.start.y);
    //            com.jme3.math.Ray ray = new com.jme3.math.Ray(start, start.subtract(inputRay.end.x, 0.25f, inputRay.end.y).normalizeLocal());
    //            ray.setLimit(0.05f);
    //            CollisionResults results = new CollisionResults();
    //            ((Node) world.getWorld().getChild("Map")).getChild("Terrain").collideWith(ray, results);
    //            if (results.size() > 0) {
    //                CollisionResult collission = results.getClosestCollision();
    //                if (collission.getDistance() > ray.getLimit()) {
    //                    return false;
    //                }
    //                if (outputCollision != null) {
    //                    outputCollision.normal = new Vector2(collission.getContactNormal().x, collission.getContactNormal().z);
    //                    outputCollision.point = new Vector2(collission.getContactPoint().x, collission.getContactPoint().z);
    //                }
    //            }
    //            return (results.size() != 0);
    return false;
}