Search in sources :

Example 81 with Vector2

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

the class GwtTest method render.

@Override
public void render() {
    Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    texture.bind(0);
    shader.begin();
    shader.setUniformMatrix("u_projView", matrix);
    shader.setUniformi("u_texture", 0);
    mesh.render(shader, GL20.GL_TRIANGLES);
    shader.end();
    batch.begin();
    batch.draw(atlas.findRegion("font"), 0, 100);
    sprite.rotate(Gdx.graphics.getDeltaTime() * 45);
    for (Vector2 position : positions) {
        sprite.setPosition(position.x, position.y);
        sprite.draw(batch);
    }
    font.draw(batch, "fps:" + Gdx.graphics.getFramesPerSecond() + ", delta: " + Gdx.graphics.getDeltaTime() + ", #sprites: " + numSprites, 0, 30);
    cache.setPosition(200, 200);
    cache.draw(batch);
    batch.end();
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Example 82 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, Color c1, Color c2) {
    check(ShapeType.Line, ShapeType.Filled, 8);
    float col1Bits = c1.toFloatBits();
    float col2Bits = c2.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(col1Bits);
        renderer.vertex(x1 + tx, y1 + ty, 0);
        renderer.color(col1Bits);
        renderer.vertex(x1 - tx, y1 - ty, 0);
        renderer.color(col2Bits);
        renderer.vertex(x2 + tx, y2 + ty, 0);
        renderer.color(col2Bits);
        renderer.vertex(x2 - tx, y2 - ty, 0);
        renderer.color(col2Bits);
        renderer.vertex(x2 + tx, y2 + ty, 0);
        renderer.color(col1Bits);
        renderer.vertex(x1 + tx, y1 + ty, 0);
        renderer.color(col2Bits);
        renderer.vertex(x2 - tx, y2 - ty, 0);
        renderer.color(col1Bits);
        renderer.vertex(x1 - tx, y1 - ty, 0);
    } else {
        renderer.color(col1Bits);
        renderer.vertex(x1 + tx, y1 + ty, 0);
        renderer.color(col1Bits);
        renderer.vertex(x1 - tx, y1 - ty, 0);
        renderer.color(col2Bits);
        renderer.vertex(x2 + tx, y2 + ty, 0);
        renderer.color(col2Bits);
        renderer.vertex(x2 - tx, y2 - ty, 0);
        renderer.color(col2Bits);
        renderer.vertex(x2 + tx, y2 + ty, 0);
        renderer.color(col1Bits);
        renderer.vertex(x1 - tx, y1 - ty, 0);
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Example 83 with Vector2

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

the class SimpleAnimationTest method create.

@Override
public void create() {
    Gdx.input.setInputProcessor(this);
    texture = new Texture(Gdx.files.internal("data/animation.png"));
    TextureRegion[][] regions = TextureRegion.split(texture, 32, 48);
    TextureRegion[] downWalkReg = regions[0];
    TextureRegion[] leftWalkReg = regions[1];
    TextureRegion[] rightWalkReg = regions[2];
    TextureRegion[] upWalkReg = regions[3];
    downWalk = new Animation<TextureRegion>(ANIMATION_SPEED, downWalkReg);
    leftWalk = new Animation<TextureRegion>(ANIMATION_SPEED, leftWalkReg);
    rightWalk = new Animation<TextureRegion>(ANIMATION_SPEED, rightWalkReg);
    upWalk = new Animation<TextureRegion>(ANIMATION_SPEED, upWalkReg);
    currentWalk = leftWalk;
    currentFrameTime = 0.0f;
    spriteBatch = new SpriteBatch();
    position = new Vector2();
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Vector2(com.badlogic.gdx.math.Vector2) Texture(com.badlogic.gdx.graphics.Texture) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Example 84 with Vector2

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

the class ApplyForce method createWorld.

@Override
protected void createWorld(World world) {
    world.setGravity(new Vector2(0, 0));
    float k_restitution = 0.4f;
    Body ground;
    {
        BodyDef bd = new BodyDef();
        bd.position.set(0, 20);
        ground = world.createBody(bd);
        EdgeShape shape = new EdgeShape();
        FixtureDef sd = new FixtureDef();
        sd.shape = shape;
        sd.density = 0;
        sd.restitution = k_restitution;
        shape.set(new Vector2(-20, -20), new Vector2(-20, 20));
        ground.createFixture(sd);
        shape.set(new Vector2(20, -20), new Vector2(20, 20));
        ground.createFixture(sd);
        shape.set(new Vector2(-20, 20), new Vector2(20, 20));
        ground.createFixture(sd);
        shape.set(new Vector2(-20, -20), new Vector2(20, -20));
        ground.createFixture(sd);
        shape.dispose();
    }
    {
        Transform xf1 = new Transform(new Vector2(), 0.3524f * (float) Math.PI);
        xf1.setPosition(xf1.mul(new Vector2(1, 0)));
        Vector2[] vertices = new Vector2[3];
        vertices[0] = xf1.mul(new Vector2(-1, 0));
        vertices[1] = xf1.mul(new Vector2(1, 0));
        vertices[2] = xf1.mul(new Vector2(0, 0.5f));
        PolygonShape poly1 = new PolygonShape();
        poly1.set(vertices);
        FixtureDef sd1 = new FixtureDef();
        sd1.shape = poly1;
        sd1.density = 4.0f;
        Transform xf2 = new Transform(new Vector2(), -0.3524f * (float) Math.PI);
        xf2.setPosition(xf2.mul(new Vector2(-1, 0)));
        vertices[0] = xf2.mul(new Vector2(-1, 0));
        vertices[1] = xf2.mul(new Vector2(1, 0));
        vertices[2] = xf2.mul(new Vector2(0, 0.5f));
        PolygonShape poly2 = new PolygonShape();
        poly2.set(vertices);
        FixtureDef sd2 = new FixtureDef();
        sd2.shape = poly2;
        sd2.density = 2.0f;
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.angularDamping = 5.0f;
        bd.linearDamping = 0.1f;
        bd.position.set(0, 2);
        bd.angle = (float) Math.PI;
        bd.allowSleep = false;
        m_body = world.createBody(bd);
        m_body.createFixture(sd1);
        m_body.createFixture(sd2);
        poly1.dispose();
        poly2.dispose();
    }
    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 0.5f);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 1.0f;
        fd.friction = 0.3f;
        for (int i = 0; i < 10; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            bd.position.set(0, 5 + 1.54f * i);
            Body body = world.createBody(bd);
            body.createFixture(fd);
            float gravity = 10.0f;
            float I = body.getInertia();
            float mass = body.getMass();
            float radius = (float) Math.sqrt(2 * I / mass);
            FrictionJointDef jd = new FrictionJointDef();
            jd.localAnchorA.set(0, 0);
            jd.localAnchorB.set(0, 0);
            jd.bodyA = ground;
            jd.bodyB = body;
            jd.collideConnected = true;
            jd.maxForce = mass * gravity;
            jd.maxTorque = mass * radius * gravity;
            world.createJoint(jd);
        }
        shape.dispose();
    }
}
Also used : EdgeShape(com.badlogic.gdx.physics.box2d.EdgeShape) PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) Vector2(com.badlogic.gdx.math.Vector2) FrictionJointDef(com.badlogic.gdx.physics.box2d.joints.FrictionJointDef) Transform(com.badlogic.gdx.physics.box2d.Transform) Body(com.badlogic.gdx.physics.box2d.Body) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef)

Example 85 with Vector2

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

the class ApplyForce method keyDown.

public boolean keyDown(int keyCode) {
    if (keyCode == Keys.W) {
        Vector2 f = m_body.getWorldVector(tmp.set(0, -200));
        Vector2 p = m_body.getWorldPoint(tmp.set(0, 2));
        m_body.applyForce(f, p, true);
    }
    if (keyCode == Keys.A)
        m_body.applyTorque(50, true);
    if (keyCode == Keys.D)
        m_body.applyTorque(-50, 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