Example usage for com.badlogic.gdx.math Vector2 cpy

List of usage examples for com.badlogic.gdx.math Vector2 cpy

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Vector2 cpy.

Prototype

@Override
    public Vector2 cpy() 

Source Link

Usage

From source file:com.dongbat.invasion.util.MovementUtil.java

public static void moveTo(Entity entity, Vector2 destination) {
    Physics physicsComponent = EntityUtil.getComponent(entity, Physics.class);
    Body body = physicsComponent.getBody();
    float mass = body.getMass();
    Vector2 currentVelocity = body.getLinearVelocity();
    Vector2 position = body.getPosition();

    Vector2 desiredVelocity = destination.cpy().sub(position).nor().scl(physicsComponent.getMaxSpeed());

    Vector2 impulse = desiredVelocity.sub(currentVelocity).scl(mass);

    PhysicsUtil.applyImpulse(entity, impulse);
}

From source file:com.dongbat.invasion.util.RenderCameraUtil.java

public static Vector2 physicsToRenderCoords(Vector2 physicsCoords) {
    return physicsCoords.cpy().scl(PhysicsCameraUtil.getRatio());
}

From source file:com.github.unluckyninja.defenseofhuman.model.entity.Hook.java

License:Open Source License

public Hook(GameWorld gameWorld, Player.HookShooter shooter, Vector2 direction, float launchSpeed) {
    this.gameWorld = gameWorld;
    this.shooter = shooter;

    Vector2 dir = direction.cpy();

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(shooter.getPlayer().getLaunchPosition());
    bodyDef.angle = (float) Math.toRadians(dir.angle());
    bodyDef.bullet = true;/*from w  ww  .  ja va  2 s  .  co  m*/
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.linearVelocity.set(dir.nor().scl(launchSpeed));

    body = shooter.getPlayer().getWorld().createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(0.15f, 0.1f, new Vector2(0.15f, 0), 0);

    body.createFixture(shape, 0.01f).setSensor(true);

    shape.dispose();

    body.setUserData(this);

    // maybe we can move all the definitions to fields?
}

From source file:com.github.unluckyninja.defenseofhuman.model.entity.Player.java

License:Open Source License

private void createBody(GameWorld gameWorld, Vector2 position) {
    Vector2 temp = new Vector2(position);
    if (body != null) {
        this.world.destroyBody(body);
    }//from w  w w  . j  ava  2 s  . c  o  m
    this.world = gameWorld.getWorld();

    BodyDef def = new BodyDef();
    def.type = BodyDef.BodyType.DynamicBody;
    def.fixedRotation = true;
    def.bullet = true;
    def.position.set(temp);
    body = world.createBody(def);

    body.setUserData(this);

    CircleShape cir = new CircleShape();
    cir.setRadius(0.4f);
    cir.setPosition(temp.add(0, 0.4f));
    circle = body.createFixture(cir, 0);
    circle.setRestitution(0f);
    cir.dispose();

    PolygonShape poly = new PolygonShape();
    poly.setAsBox(0.4f, 0.8f, temp.add(0, 0.8f), 0);
    box = body.createFixture(poly, 1);

    poly.setAsBox(0.2f, 0.2f, temp.add(0, -0.4f), 0);
    localLaunchPosition = temp.cpy();

    FixtureDef fixDef = new FixtureDef();
    fixDef.isSensor = true;
    fixDef.density = 0.01f;
    fixDef.shape = poly;

    shootSensor = body.createFixture(fixDef);

    poly.dispose();
}

From source file:com.hajnar.GravityShip.GameObjects.Terrain.java

License:Apache License

public void generateMeshes() {
    ArrayList<Fixture> terrainFixtures = objectBody.getFixtureList();
    Vector2 boxVertex = new Vector2();
    meshes = new ArrayList<Mesh>();
    boundingBoxes = new ArrayList<BoundingBox>();
    EarClippingTriangulator ear = new EarClippingTriangulator();

    for (Fixture terrainFixture : terrainFixtures) {
        PolygonShape shape = (PolygonShape) terrainFixture.getShape();
        boxVertex = new Vector2();
        int vc = shape.getVertexCount();
        ArrayList<Vector2> boxVertices = new ArrayList<Vector2>();
        ArrayList<Vector2> triaBoxVertices = new ArrayList<Vector2>();

        for (int i = 0; i < vc; i++) {
            shape.getVertex(i, boxVertex);
            boxVertex = objectBody.getWorldPoint(boxVertex).mul(Helper.BOX_TO_WORLD);
            boxVertices.add(boxVertex.cpy());
        }//ww  w .  j ava2 s . c o  m
        triaBoxVertices = (ArrayList<Vector2>) ear.computeTriangles(boxVertices);
        float[] meshVertices = new float[triaBoxVertices.size() * 4];
        short[] meshIndices = new short[triaBoxVertices.size()];
        for (int i = 0; i < triaBoxVertices.size(); i++) {
            meshVertices[i * 4] = triaBoxVertices.get(i).x;
            meshVertices[i * 4 + 1] = triaBoxVertices.get(i).y;
            meshVertices[i * 4 + 2] = triaBoxVertices.get(i).x * TEXTURE_SCALE;
            meshVertices[i * 4 + 3] = triaBoxVertices.get(i).y * TEXTURE_SCALE;
            meshIndices[i] = (short) i;
        }

        Mesh mesh = new Mesh(true, triaBoxVertices.size(), triaBoxVertices.size(),
                new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE),
                new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
        mesh.setVertices(meshVertices);
        mesh.setIndices(meshIndices);
        meshes.add(mesh);
        boundingBoxes.add(mesh.calculateBoundingBox());
    }
}

From source file:com.jmstudios.pointandhit.TargetManager.java

License:Open Source License

public int hit(Vector2 position) {
    Vector2 targetPosition = target.getCenterPosition();
    if (targetPosition.cpy().sub(position).len() < target.getRadius() + userPointer.getRadius() && !target.dying
            && !loseLifeEffect.isActive()) {
        int extraScore = getHitScore((float) (target.getRadius()) / (float) (target.getStartRadius()));
        score += extraScore;//from w w  w .j  a v  a 2s .c  o  m
        currentTheme = getTheme(score);
        target.explode(position);
        return extraScore;
    } else {
        return 0;
    }
}

From source file:com.kotcrab.vis.editor.module.physicseditor.PRenderer.java

License:Apache License

public void drawBallThrowPath(Vector2 p1, Vector2 p2) {
    if (p1 == null || p2 == null)
        return;/*from   w  w  w .  j av  a2s .  c o  m*/

    Gdx.gl.glLineWidth(3);
    Gdx.gl.glEnable(GL20.GL_BLEND);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

    float w = 0.03f * camera.getZoom();

    drawer.setProjectionMatrix(camera.getCombinedMatrix());
    drawer.begin(ShapeRenderer.ShapeType.Line);
    drawer.setColor(BALLTHROWPATH_COLOR);
    drawer.line(p1.x, p1.y, p2.x, p2.y);
    drawer.end();

    drawer.setProjectionMatrix(camera.getCombinedMatrix());
    drawer.begin(ShapeType.Filled);
    drawer.setColor(BALLTHROWPATH_COLOR);
    drawer.rect(p2.cpy().sub(w / 2, w / 2).x, p2.cpy().sub(w / 2, w / 2).y, w, w);
    drawer.end();
}

From source file:com.kotcrab.vis.editor.module.physicseditor.PRenderer.java

License:Apache License

private void drawShapes(Array<ShapeModel> shapes, Vector2 nextPoint) {
    Gdx.gl.glLineWidth(2);/*w w w .  j av  a 2 s  .  c  om*/
    Gdx.gl.glEnable(GL20.GL_BLEND);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

    for (ShapeModel shape : shapes) {
        Array<Vector2> vs = shape.getVertices();
        if (vs.size == 0)
            continue;

        switch (shape.getType()) {
        case POLYGON:
            drawer.begin(ShapeRenderer.ShapeType.Line);
            drawer.setColor(SHAPE_COLOR);

            for (int i = 1; i < vs.size; i++)
                drawer.line(vs.get(i).x, vs.get(i).y, vs.get(i - 1).x, vs.get(i - 1).y);

            if (shape.isClosed()) {
                drawer.setColor(SHAPE_COLOR);
                drawer.line(vs.get(0).x, vs.get(0).y, vs.get(vs.size - 1).x, vs.get(vs.size - 1).y);
            } else {
                drawer.setColor(SHAPE_LASTLINE_COLOR);
                drawer.line(vs.get(vs.size - 1).x, vs.get(vs.size - 1).y, nextPoint.x, nextPoint.y);
            }

            drawer.end();
            break;

        case CIRCLE:
            if (shape.isClosed()) {
                Vector2 center = shape.getVertices().get(0);
                float radius = shape.getVertices().get(1).cpy().sub(center).len();
                if (radius > 0.0001f) {
                    drawer.begin(ShapeType.Line);
                    drawer.setColor(SHAPE_COLOR);
                    drawer.circle(center.x, center.y, radius, 20);
                    drawer.end();
                }
            } else {
                Vector2 center = shape.getVertices().get(0);
                float radius = nextPoint.cpy().sub(center).len();
                if (radius > 0.0001f) {
                    drawer.begin(ShapeType.Line);
                    drawer.setColor(SHAPE_LASTLINE_COLOR);
                    drawer.circle(center.x, center.y, radius, 20);
                    drawer.end();
                }
            }
            break;
        }
    }
}

From source file:com.kotcrab.vis.editor.module.physicseditor.PRenderer.java

License:Apache License

private void drawPoints(Array<ShapeModel> shapes, List<Vector2> selectedPoints, Vector2 nearestPoint,
        Vector2 nextPoint) {/*from  ww  w  .ja v a  2s.co  m*/
    Gdx.gl.glLineWidth(2);
    Gdx.gl.glEnable(GL20.GL_BLEND);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

    float w = 0.025f * camera.getZoom();

    for (ShapeModel shape : shapes) {
        for (Vector2 p : shape.getVertices()) {
            if (p == nearestPoint || (selectedPoints != null && selectedPoints.contains(p))) {
                drawer.begin(ShapeType.Filled);
                drawer.setColor(SHAPE_COLOR);
                drawer.rect(p.cpy().sub(w / 2, w / 2).x, p.cpy().sub(w / 2, w / 2).y, w, w);
                drawer.end();
            } else {
                drawer.begin(ShapeType.Line);
                drawer.setColor(SHAPE_COLOR);
                drawer.rect(p.cpy().sub(w / 2, w / 2).x, p.cpy().sub(w / 2, w / 2).y, w, w);
                drawer.end();
            }
        }
    }

    if (nextPoint != null) {
        drawer.begin(ShapeType.Line);
        drawer.setColor(SHAPE_LASTLINE_COLOR);
        drawer.rect(nextPoint.cpy().sub(w / 2, w / 2).x, nextPoint.cpy().sub(w / 2, w / 2).y, w, w);
        drawer.end();
    }
}

From source file:com.kotcrab.vis.editor.module.physicseditor.util.PolygonUtils.java

License:Apache License

private static float distanceSquared(Vector2 a, Vector2 b) {
    Vector2 c = a.cpy().sub(b);
    return c.dot(c);
}