Search in sources :

Example 6 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class ShapeRenderer method rectLine.

/** Draws a line using a rotated rectangle, where with one edge is centered at x1, y1 and the opposite edge centered at x2, y2. */
public void rectLine(float x1, float y1, float x2, float y2, float width) {
    check(ShapeType.Line, ShapeType.Filled, 8);
    float colorBits = color.toFloatBits();
    Vector2 t = tmp.set(y2 - y1, x1 - x2).nor();
    width *= 0.5f;
    float tx = t.x * width;
    float ty = t.y * width;
    if (shapeType == ShapeType.Line) {
        renderer.color(colorBits);
        renderer.vertex(x1 + tx, y1 + ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x1 - tx, y1 - ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x2 + tx, y2 + ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x2 - tx, y2 - ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x2 + tx, y2 + ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x1 + tx, y1 + ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x2 - tx, y2 - ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x1 - tx, y1 - ty, 0);
    } else {
        renderer.color(colorBits);
        renderer.vertex(x1 + tx, y1 + ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x1 - tx, y1 - ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x2 + tx, y2 + ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x2 - tx, y2 - ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x2 + tx, y2 + ty, 0);
        renderer.color(colorBits);
        renderer.vertex(x1 - tx, y1 - ty, 0);
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Example 7 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class Box2DCharacterControllerTest method isPlayerGrounded.

private boolean isPlayerGrounded(float deltaTime) {
    groundedPlatform = null;
    Array<Contact> contactList = world.getContactList();
    for (int i = 0; i < contactList.size; i++) {
        Contact contact = contactList.get(i);
        if (contact.isTouching() && (contact.getFixtureA() == playerSensorFixture || contact.getFixtureB() == playerSensorFixture)) {
            Vector2 pos = player.getPosition();
            WorldManifold manifold = contact.getWorldManifold();
            boolean below = true;
            for (int j = 0; j < manifold.getNumberOfContactPoints(); j++) {
                below &= (manifold.getPoints()[j].y < pos.y - 1.5f);
            }
            if (below) {
                if (contact.getFixtureA().getUserData() != null && contact.getFixtureA().getUserData().equals("p")) {
                    groundedPlatform = (Platform) contact.getFixtureA().getBody().getUserData();
                }
                if (contact.getFixtureB().getUserData() != null && contact.getFixtureB().getUserData().equals("p")) {
                    groundedPlatform = (Platform) contact.getFixtureB().getBody().getUserData();
                }
                return true;
            }
            return false;
        }
    }
    return false;
}
Also used : WorldManifold(com.badlogic.gdx.physics.box2d.WorldManifold) Vector2(com.badlogic.gdx.math.Vector2) Contact(com.badlogic.gdx.physics.box2d.Contact)

Example 8 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class Box2DCharacterControllerTest method create.

@Override
public void create() {
    world = new World(new Vector2(0, -40), true);
    renderer = new Box2DDebugRenderer();
    cam = new OrthographicCamera(28, 20);
    createWorld();
    Gdx.input.setInputProcessor(this);
    batch = new SpriteBatch();
    font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false);
}
Also used : Box2DDebugRenderer(com.badlogic.gdx.physics.box2d.Box2DDebugRenderer) Vector2(com.badlogic.gdx.math.Vector2) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) World(com.badlogic.gdx.physics.box2d.World) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Example 9 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class Box2DCharacterControllerTest method touchDown.

@Override
public boolean touchDown(int x, int y, int pointerId, int button) {
    cam.unproject(point.set(x, y, 0));
    if (button == Input.Buttons.LEFT) {
        if (last == null) {
            last = new Vector2(point.x, point.y);
        } else {
            createEdge(BodyType.StaticBody, last.x, last.y, point.x, point.y, 0);
            last.set(point.x, point.y);
        }
    } else {
        last = null;
    }
    return false;
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Example 10 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class Box2DCharacterControllerTest method render.

@Override
public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    cam.position.set(player.getPosition().x, player.getPosition().y, 0);
    cam.update();
    renderer.render(world, cam.combined);
    Vector2 vel = player.getLinearVelocity();
    Vector2 pos = player.getPosition();
    boolean grounded = isPlayerGrounded(Gdx.graphics.getDeltaTime());
    if (grounded) {
        lastGroundTime = TimeUtils.nanoTime();
    } else {
        if (TimeUtils.nanoTime() - lastGroundTime < 100000000) {
            grounded = true;
        }
    }
    // cap max velocity on x
    if (Math.abs(vel.x) > MAX_VELOCITY) {
        vel.x = Math.signum(vel.x) * MAX_VELOCITY;
        player.setLinearVelocity(vel.x, vel.y);
    }
    // calculate stilltime & damp
    if (!Gdx.input.isKeyPressed(Keys.A) && !Gdx.input.isKeyPressed(Keys.D)) {
        stillTime += Gdx.graphics.getDeltaTime();
        player.setLinearVelocity(vel.x * 0.9f, vel.y);
    } else {
        stillTime = 0;
    }
    // disable friction while jumping
    if (!grounded) {
        playerPhysicsFixture.setFriction(0f);
        playerSensorFixture.setFriction(0f);
    } else {
        if (!Gdx.input.isKeyPressed(Keys.A) && !Gdx.input.isKeyPressed(Keys.D) && stillTime > 0.2) {
            playerPhysicsFixture.setFriction(1000f);
            playerSensorFixture.setFriction(1000f);
        } else {
            playerPhysicsFixture.setFriction(0.2f);
            playerSensorFixture.setFriction(0.2f);
        }
        // character hops :)
        if (groundedPlatform != null && groundedPlatform instanceof MovingPlatform && ((MovingPlatform) groundedPlatform).dist == 0) {
            player.applyLinearImpulse(0, -24, pos.x, pos.y, true);
        }
    }
    // since Box2D 2.2 we need to reset the friction of any existing contacts
    Array<Contact> contacts = world.getContactList();
    for (int i = 0; i < world.getContactCount(); i++) {
        Contact contact = contacts.get(i);
        contact.resetFriction();
    }
    // apply left impulse, but only if max velocity is not reached yet
    if (Gdx.input.isKeyPressed(Keys.A) && vel.x > -MAX_VELOCITY) {
        player.applyLinearImpulse(-2f, 0, pos.x, pos.y, true);
    }
    // apply right impulse, but only if max velocity is not reached yet
    if (Gdx.input.isKeyPressed(Keys.D) && vel.x < MAX_VELOCITY) {
        player.applyLinearImpulse(2f, 0, pos.x, pos.y, true);
    }
    // jump, but only when grounded
    if (jump) {
        jump = false;
        if (grounded) {
            player.setLinearVelocity(vel.x, 0);
            System.out.println("jump before: " + player.getLinearVelocity());
            player.setTransform(pos.x, pos.y + 0.01f, 0);
            player.applyLinearImpulse(0, 40, pos.x, pos.y, true);
            System.out.println("jump, " + player.getLinearVelocity());
        }
    }
    // update platforms
    for (int i = 0; i < platforms.size; i++) {
        Platform platform = platforms.get(i);
        platform.update(Math.max(1 / 30.0f, Gdx.graphics.getDeltaTime()));
    }
    // le step...
    world.step(Gdx.graphics.getDeltaTime(), 4, 4);
    // accum += Gdx.graphics.getDeltaTime();
    // while(accum > TICK) {
    // accum -= TICK;
    // world.step(TICK, 4, 4);
    // }
    player.setAwake(true);
    cam.project(point.set(pos.x, pos.y, 0));
    batch.begin();
    font.draw(batch, "friction: " + playerPhysicsFixture.getFriction() + "\ngrounded: " + grounded, point.x + 20, point.y);
    batch.end();
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) Contact(com.badlogic.gdx.physics.box2d.Contact)

Aggregations

Vector2 (com.badlogic.gdx.math.Vector2)103 Body (com.badlogic.gdx.physics.box2d.Body)21 BodyDef (com.badlogic.gdx.physics.box2d.BodyDef)14 FixtureDef (com.badlogic.gdx.physics.box2d.FixtureDef)10 PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)10 Texture (com.badlogic.gdx.graphics.Texture)7 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)7 EdgeShape (com.badlogic.gdx.physics.box2d.EdgeShape)7 Vector3 (com.badlogic.gdx.math.Vector3)6 Fixture (com.badlogic.gdx.physics.box2d.Fixture)6 World (com.badlogic.gdx.physics.box2d.World)6 GameEntity (ilargia.egdx.logicbricks.gen.game.GameEntity)6 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)5 CircleShape (com.badlogic.gdx.physics.box2d.CircleShape)5 Test (org.junit.Test)5 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)4 Box2DDebugRenderer (com.badlogic.gdx.physics.box2d.Box2DDebugRenderer)4 Contact (com.badlogic.gdx.physics.box2d.Contact)4 WorldManifold (com.badlogic.gdx.physics.box2d.WorldManifold)4