Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

LibGDX: Box2d hovering, rotating body

$
0
0

Hello I am working on a hovering triangle (equilateral triangle). The triangle should allways rotate at a constant speed and hover over the ground. The problem is if I setAngularVelocity it does rise and fall to the ground (repeatingly).

I have been following this tutorial for the hovering.
I rely have bad knowlege about RayCast and box2D (basics).

private float targetHeight = 7;
private float springConst = 100;
private float distanceAboveGround;
private HoverTest callback;
private Vector2 start = new Vector2(),
                end = new Vector2(),
                vec = new Vector2(0, -targetHeight);

private void physics(float delta) {

    float frameTime = Math.min(delta, 0.25f);
    accumulator += frameTime;
    while (accumulator >= Const.TIME_STEP) {
        playerBody.setAngularVelocity(1);

        start = playerBody.getPosition();
        end = playerBody.getWorldPoint(vec);
        world.rayCast(callback, start, end);

        if (callback.isHit()) {
            distanceAboveGround = start.sub(callback.getPoint()).len();
        }

        if (distanceAboveGround < targetHeight) {
            distanceAboveGround += 0.25f * playerBody.getLinearVelocity().y;

            float distanceAwayFromTargetHeight = targetHeight - distanceAboveGround;
            playerBody.applyForceToCenter(0, springConst*distanceAwayFromTargetHeight, true);
            playerBody.applyForceToCenter(0, playerBody.getMass() * -world.getGravity().y, true);
        }

        world.step(Const.TIME_STEP, 8, 3);
        accumulator -= Const.TIME_STEP;
    }
}

EDIT: I found that if i flip the triangle on a other side it falls and then does nothing. I think that has to dowith the end vector (is no longer pointing down).
How could I get it allways point down?


Viewing all articles
Browse latest Browse all 434

Trending Articles