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

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

Introduction

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

Prototype

public Vector2(float x, float y) 

Source Link

Document

Constructs a vector with the given components

Usage

From source file:Tabox2D.java

License:Open Source License

private Tabox2D(Vector2 gravity) {
    width = Gdx.graphics.getWidth();/*from   w w w.  j  a  v a  2  s .c  o  m*/
    height = Gdx.graphics.getHeight();
    meterSize = 100;// 1 metter = 100px, default.
    polyInfo = new HashMap<String, Float>();
    rawForces = false;

    // Sides by polygon:
    polyInfo.put("triangle", 3f);
    polyInfo.put("square", 4f);
    polyInfo.put("pentagon", 5f);
    polyInfo.put("hexagon", 6f);
    polyInfo.put("heptagon", 7f);
    polyInfo.put("octagon", 8f);

    // Angle of the sides:
    polyInfo.put("triangle_angle", 120f);
    polyInfo.put("square_angle", 90f);
    polyInfo.put("pentagon_angle", 72f);
    polyInfo.put("hexagon_angle", 60f);
    polyInfo.put("heptagon_angle", 51.428f);
    polyInfo.put("octagon_angle", 45f);

    filterMin = "linear";
    filterMag = "linear";

    renderer = new Box2DDebugRenderer();
    sr = new ShapeRenderer();
    spriteBath = new SpriteBatch();
    adjustCamera();
    tabodies = new ArrayList<Tabody>();
    world = new World(new Vector2(gravity.x, gravity.y), true);

    sr = new ShapeRenderer();
}

From source file:Tabox2D.java

License:Open Source License

/**
 * Returns and instance of Tabox2D with its own world
 * @return The new instance/*from   ww  w  .  j  ava  2  s  .  c  o  m*/
 */
public static Tabox2D getInstance() {
    return getInstance(new Vector2(0, -9.8f));
}

From source file:Tabox2D.java

License:Open Source License

private Tabody generateRegularPoly(String name, String type, float x, float y, float rad) {
    // Scale proportions:
    x /= meterSize;/*from  ww w.  j a  va  2 s.  c  om*/
    y /= meterSize;
    rad /= meterSize;

    PolygonShape polygonShape;
    BodyDef defPoly = new BodyDef();

    setType(defPoly, type);

    // Generate points:
    List<Vector2> pts = new ArrayList<Vector2>();
    Vector2 p0 = new Vector2(0, rad);

    float conv = MathUtils.degreesToRadians;
    float angleInDeg = polyInfo.get(name + "_angle");
    float cos = MathUtils.cos(conv * angleInDeg);
    float sin = MathUtils.sin(conv * angleInDeg);

    for (int i = 0; i < polyInfo.get(name); i++) {
        pts.add(new Vector2(p0.x, p0.y));
        p0.set(p0.x, p0.y);

        float newX = p0.x * cos - p0.y * sin;
        float newY = p0.x * sin + p0.y * cos;

        p0.x = newX;
        p0.y = newY;
    }

    // Get bounding box:

    float[] rawPoints = new float[pts.size() * 2];
    int pointIndex = 0;
    for (int i = 0; i < rawPoints.length - 1; i += 2) {
        rawPoints[i] = pts.get(pointIndex).x;
        rawPoints[i + 1] = pts.get(pointIndex).y;
        pointIndex++;
    }

    Polygon polyForBox = new Polygon();
    polyForBox.setVertices(rawPoints);

    Rectangle boundingRect = polyForBox.getBoundingRectangle();
    float boxX = boundingRect.x;
    float boxY = boundingRect.y;
    float boxW = boundingRect.getWidth();
    float boxH = boundingRect.getHeight();

    Vector2 aabbCenter = new Vector2(boxX + boxW / 2, boxY + boxH / 2);
    defPoly.position.set(x, y);

    Tabody regularPoly = new Tabody();
    regularPoly.body = world.createBody(defPoly);
    //regularPoly.body.setFixedRotation(true);
    polygonShape = new PolygonShape();

    //polygonShape.setAsBox(w / 2, h / 2);
    for (int i = 0; i < rawPoints.length - 1; i += 2) {
        rawPoints[i] -= aabbCenter.x;
        rawPoints[i + 1] -= aabbCenter.y;
    }
    //rawPoints[0] += 0.5;
    polygonShape.set(rawPoints);

    FixtureDef fixtureBox = new FixtureDef();
    fixtureBox.shape = polygonShape;
    fixtureBox.density = 1;
    fixtureBox.friction = 1;
    fixtureBox.restitution = 0;

    ////////////////////////////////////////
    regularPoly.w = boxW * meterSize;//radius * 2 * meterSize;
    regularPoly.h = boxH * meterSize;//radius * 2 * meterSize;
    regularPoly.fixture = fixtureBox;
    regularPoly.bodyType = "poly";
    ////////////////////////////////////////

    regularPoly.body.createFixture(fixtureBox);
    polygonShape.dispose();
    tabodies.add(regularPoly);
    return regularPoly;
}

From source file:Tabox2D.java

License:Open Source License

/**
 * Creates a Polygons with the given points
 * @param type "dynamic" or "static"/*  ww w .j  a v  a 2  s .  co  m*/
 * @param pts points for the polygon
 * @return A new Tabody instance
 */
public Tabody newPoly(String type, float[] pts) {
    // Scale proportions:
    for (int i = 0; i < pts.length; i++) {
        pts[i] /= meterSize;
    }

    PolygonShape polygonShape;
    BodyDef defPoly = new BodyDef();

    setType(defPoly, type);

    // Get bounding box:

    Polygon polyForBox = new Polygon();
    polyForBox.setVertices(pts);

    //polyForBox.translate(center.x, center.y);

    Rectangle boundingRect = boundingBoxOf(polyForBox.getVertices());
    Vector2 aabbCenter = new Vector2(boundingRect.x + boundingRect.width / 2,
            boundingRect.y + boundingRect.height / 2);
    defPoly.position.set(aabbCenter.x, aabbCenter.y);

    Tabody regularPoly = new Tabody();
    regularPoly.body = world.createBody(defPoly);
    polygonShape = new PolygonShape();

    for (int i = 0; i < pts.length - 1; i += 2) {
        pts[i] -= aabbCenter.x;
        pts[i + 1] -= aabbCenter.y;
    }
    polygonShape.set(pts);

    FixtureDef fixtureBox = new FixtureDef();
    fixtureBox.shape = polygonShape;
    fixtureBox.density = 1;
    fixtureBox.friction = 1;
    fixtureBox.restitution = 0;

    ////////////////////////////////////////
    regularPoly.w = boundingRect.width * meterSize;//radius * 2 * meterSize;
    regularPoly.h = boundingRect.height * meterSize;//radius * 2 * meterSize;
    regularPoly.fixture = fixtureBox;
    regularPoly.bodyType = "poly";
    ////////////////////////////////////////

    regularPoly.body.createFixture(fixtureBox);
    polygonShape.dispose();
    tabodies.add(regularPoly);
    return regularPoly;
}

From source file:Tabox2D.java

License:Open Source License

/**
 * Combines different tabodies in a single one.<br/>
 * This is useful to have a body with different fixtures in an easy way
 * @param tabodyArray Array of tabodies to combine
 * @return A new Tabody/*from   ww  w. j a v  a  2  s . c  o m*/
 */
public Tabody combine(String type, Tabody... tabodyArray) {
    if (tabodyArray.length > 0) {

        Tabody newTabody = new Tabody();

        BodyDef bodyDef = new BodyDef();
        setType(bodyDef, type);

        //List<Vector2> centers = new ArrayList<Vector2>();

        //AABB center of combines bodies:
        List<Vector2> ptsCombined = new ArrayList<Vector2>();
        for (int i = 0; i < tabodyArray.length; i++) {
            //centers.add(tabodyArray[i].body.getWorldCenter());
            Tabody t = tabodyArray[i];
            for (Fixture f : t.body.getFixtureList()) {
                if (f.getShape() instanceof CircleShape) {
                    CircleShape cS = (CircleShape) f.getShape();
                    // top-left and bottom-right of circle bounds:
                    ptsCombined.add(new Vector2(cS.getPosition().x - cS.getRadius(),
                            cS.getPosition().y + cS.getRadius()));
                    ptsCombined.add(new Vector2(cS.getPosition().x + cS.getRadius(),
                            cS.getPosition().y - cS.getRadius()));
                } else if (f.getShape() instanceof PolygonShape) {
                    PolygonShape pS = (PolygonShape) f.getShape();
                    for (int n = 0; n < pS.getVertexCount(); n++) {
                        Vector2 tmp = new Vector2();
                        pS.getVertex(n, tmp);
                        ptsCombined.add(
                                new Vector2(t.body.getPosition().x + tmp.x, t.body.getPosition().y + tmp.y));
                    }
                }
            }
        }

        Rectangle rectangle = boundingBoxOf(ptsCombined);

        bodyDef.position.set(rectangle.x + rectangle.width / 2, rectangle.y + rectangle.height / 2);

        newTabody.w = rectangle.width * meterSize;
        newTabody.h = rectangle.height * meterSize;
        newTabody.body = world.createBody(bodyDef);

        for (int i = 0; i < tabodyArray.length; i++) {

            Tabody t = tabodyArray[i];

            for (Fixture f : t.body.getFixtureList()) {
                FixtureDef fixtureDef = new FixtureDef();

                fixtureDef.density = f.getDensity();
                fixtureDef.friction = f.getFriction();
                fixtureDef.restitution = f.getRestitution();
                // Delta X and Y for translating the shapes:
                float dx = t.body.getWorldCenter().x - bodyDef.position.x;
                float dy = t.body.getWorldCenter().y - bodyDef.position.y;
                if (f.getShape() instanceof CircleShape) {

                    CircleShape circleShape = (CircleShape) f.getShape();
                    circleShape.setPosition(
                            new Vector2(circleShape.getPosition().x + dx, circleShape.getPosition().y + dy));
                    fixtureDef.shape = circleShape;

                } else if (f.getShape() instanceof PolygonShape) {

                    PolygonShape polygonShape = (PolygonShape) f.getShape();

                    float[] pts = new float[polygonShape.getVertexCount() * 2];
                    int vertexIndex = 0;
                    // delta X and delta Y respect to the main body:
                    dx = t.body.getPosition().x - bodyDef.position.x;
                    dy = t.body.getPosition().y - bodyDef.position.y;
                    for (int j = 0; j < pts.length - 1; j += 2) {
                        Vector2 tmp = new Vector2();
                        polygonShape.getVertex(vertexIndex, tmp);
                        pts[j] = tmp.x + dx;
                        pts[j + 1] = tmp.y + dy;
                        vertexIndex++;
                    }
                    polygonShape.set(pts);
                    fixtureDef.shape = polygonShape;

                } else if (f.getShape() instanceof EdgeShape) {
                    EdgeShape edgeShape = (EdgeShape) f.getShape();
                    fixtureDef.shape = edgeShape;
                }

                newTabody.body.createFixture(fixtureDef);
            }
        }

        // Destroy:
        for (int i = 0; i < tabodyArray.length; i++) {
            world.destroyBody(tabodyArray[i].body);
            tabodies.remove(tabodyArray[i]);
        }

        // Add new Tabody:
        tabodies.add(newTabody);
        return newTabody;
    } else {
        System.err.println("No tabodies specified in Tabox2D.combine()");
        return null;
    }
}

From source file:Tabox2D.java

License:Open Source License

/**
 * Get centroid of given polygon points/* www .j  a v  a  2 s.  c om*/
 * @param pts float[] array of points
 * @return a Vector2 instance with the center
 */
public Vector2 centroidOf(float[] pts) {
    float centroidX = 0, centroidY = 0;
    for (int i = 0; i < pts.length - 1; i += 2) {
        centroidX += pts[i];
        centroidY += pts[i + 1];
    }
    return new Vector2(centroidX / pts.length / 2, centroidY / pts.length / 2);
}

From source file:Tabox2D.java

License:Open Source License

/**
 * Get centroid of given polygon points/*from   ww w.  ja  va2  s  .co m*/
 * @param pts List<Vector2D> of points
 * @return a Vector2 instance with the center
 */
public Vector2 centroidOf(List<Vector2> pts) {
    float centroidX = 0, centroidY = 0;
    for (Vector2 vec : pts) {
        centroidX += vec.x;
        centroidY += vec.y;
    }
    return new Vector2(centroidX / pts.size(), centroidY / pts.size());
}

From source file:AITest.VectorUtils.java

public static Vector2 AngleToVector(float angle) {
    return new Vector2((float) Math.cos(angle), (float) Math.sin(angle));
}

From source file:app.badlogicgames.splitpong.multiplayer.World.java

License:Apache License

public World() {
    pad = new Pad(new Vector2(MainMenuScreen.camX / 2, (int) MainMenuScreen.camY / 4 + 4));
    ball = new Ball(new Vector2(MainMenuScreen.camX / 2, MainMenuScreen.camY / 2));

    paredIzq = new Rectangle(0, MainMenuScreen.camY / 5, grosorPared, 4 * MainMenuScreen.camY / 5);
    paredDer = new Rectangle(MainMenuScreen.camX - grosorPared, MainMenuScreen.camY / 5, grosorPared,
            4 * MainMenuScreen.camY / 5);
    paredBot = new Rectangle(grosorPared, MainMenuScreen.camY / 5, MainMenuScreen.camX - 2 * grosorPared,
            grosorPared);/*w  ww. java 2 s.co  m*/
    paredTop = new Rectangle(grosorPared, MainMenuScreen.camY - grosorPared,
            MainMenuScreen.camX - 2 * grosorPared, grosorPared);
}

From source file:app.badlogicgames.superjumper.GameObject.java

License:Apache License

public GameObject(float x, float y, float width, float height) {
    this.position = new Vector2(x, y);
    this.bounds = new Rectangle(x - width / 2, y - height / 2, width, height);
}