Search in sources :

Example 36 with Vector2

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

the class BodyUpdateAction method act.

/**
	 * Action.
	 * 
	 */
@Override
public boolean act(float delta) {
    // Centre body.
    Vector2 pos = body.getPosition();
    if (centred) {
        // Adjust the actor to centre
        getActor().setX((pos.x * pixelsPerMetre) - getActor().getWidth() / 2);
        getActor().setY((pos.y * pixelsPerMetre) - getActor().getHeight() / 2);
    } else {
        getActor().setX(pos.x * pixelsPerMetre);
        getActor().setY(pos.y * pixelsPerMetre);
    }
    float angleDeg = body.getAngle() * MathUtils.radiansToDegrees;
    getActor().setRotation(angleDeg);
    return false;
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Example 37 with Vector2

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

the class Box2DTest method render.

@Override
public void render() {
    if (Gdx.input.isTouched()) {
        float x = Gdx.input.getX();
        float y = Gdx.input.getY();
        if (!console.hitsConsole(x, y)) {
            Vector3 worldVector = c.unproject(new Vector3(x, y, 0));
            createExplosion(worldVector.x, worldVector.y, 2000);
            console.log(String.format("Created touch explosion at %.2f, %.2f!", worldVector.x, worldVector.y), LogLevel.SUCCESS);
        }
    }
    if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE))
        Gdx.app.exit();
    world.step(1 / 60f, 6, 2);
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    for (int i = 0; i < bodies.length; i++) {
        Vector2 pos = bodies[i].getPosition();
        sprites[i].setPosition(pos.x - sprites[i].getWidth() / 2, pos.y - sprites[i].getHeight() / 2);
        sprites[i].setRotation(MathUtils.radiansToDegrees * bodies[i].getAngle());
        sprites[i].draw(batch);
    }
    batch.end();
    debugRenderer.render(world, c.combined);
    console.draw();
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) Vector3(com.badlogic.gdx.math.Vector3)

Example 38 with Vector2

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

the class Box2DTest method createExplosion.

/**
	 * Creates an explosion that applies forces to the bodies relative to their
	 * position and the given x and y values.
	 * 
	 * @param maxForce
	 *            The maximum force to be applied to the bodies (diminishes as
	 *            distance from touch increases).
	 */
private void createExplosion(float x, float y, float maxForce) {
    float force;
    Vector2 touch = new Vector2(x, y);
    for (int i = 0; i < bodies.length; i++) {
        Body b = bodies[i];
        Vector2 v = b.getPosition();
        float dist = v.dst2(touch);
        if (dist == 0)
            force = maxForce;
        else
            force = MathUtils.clamp(maxForce / dist, 0, maxForce);
        float angle = v.cpy().sub(touch).angle();
        float xForce = force * MathUtils.cosDeg(angle);
        float yForce = force * MathUtils.sinDeg(angle);
        Vector3 touch3, v3, boundMin, boundMax, intersection;
        touch3 = new Vector3(touch.x, touch.y, 0);
        v3 = new Vector3(v.x, v.y, 0);
        boundMin = new Vector3(v.x - 1, v.y - 1, 0);
        boundMax = new Vector3(v.x + 1, v.y + 1, 0);
        intersection = Vector3.Zero;
        Intersector.intersectRayBounds(new Ray(touch3, v3), new BoundingBox(boundMin, boundMax), intersection);
        b.applyForce(new Vector2(xForce, yForce), new Vector2(intersection.x, intersection.y), true);
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) BoundingBox(com.badlogic.gdx.math.collision.BoundingBox) Vector3(com.badlogic.gdx.math.Vector3) Ray(com.badlogic.gdx.math.collision.Ray) Body(com.badlogic.gdx.physics.box2d.Body)

Example 39 with Vector2

use of com.badlogic.gdx.math.Vector2 in project nhglib by VoidZombie.

the class Main method onMouseInput.

@Override
public void onMouseInput(NhgInput input) {
    Vector2 pointerVector = (Vector2) input.getInputSource().getValue();
    if (pointerVector != null) {
        float horizontalAxis = pointerVector.x;
        float verticalAxis = pointerVector.y;
        cameraNode.rotate(verticalAxis, horizontalAxis, 0);
        cameraNode.applyTransforms();
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Example 40 with Vector2

use of com.badlogic.gdx.math.Vector2 in project AmazingMaze by TheVirtualMachine.

the class FishMiniGame method touchDown.

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    if (!paused && button == Input.Buttons.LEFT) {
        Vector2 current = new Vector2(screenX, screenY);
        canvas.drawDot(current);
        previous = current;
        leftDown = true;
        return true;
    }
    return false;
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

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