Search in sources :

Example 51 with Vector2

use of com.badlogic.gdx.math.Vector2 in project Entitas-Java by Rubentxu.

the class Box2DUtils method vertices.

/**
     * @return the vertices of the given Shape
     */
public static Vector2[] vertices(Shape shape, Vector2[] output) {
    if (shapeCache.containsKey(shape))
        return output = shapeCache.get(shape).vertices;
    else {
        switch(shape.getType()) {
            case Polygon:
                PolygonShape polygonShape = (PolygonShape) shape;
                output = new Vector2[polygonShape.getVertexCount()];
                for (int i = 0; i < output.length; i++) {
                    output[i] = new Vector2();
                    polygonShape.getVertex(i, output[i]);
                }
                break;
            case Edge:
                EdgeShape edgeShape = (EdgeShape) shape;
                edgeShape.getVertex1(tmpVec);
                edgeShape.getVertex2(tmpVec2);
                output = new Vector2[] { tmpVec, tmpVec2 };
                break;
            case Chain:
                ChainShape chainShape = (ChainShape) shape;
                output = new Vector2[chainShape.getVertexCount()];
                for (int i = 0; i < output.length; i++) {
                    output[i] = new Vector2();
                    chainShape.getVertex(i, output[i]);
                }
                break;
            case Circle:
                CircleShape circleShape = (CircleShape) shape;
                output = new Vector2[] { // top left
                new Vector2(circleShape.getPosition().x - circleShape.getRadius(), circleShape.getPosition().y + circleShape.getRadius()), // bottom left
                new Vector2(circleShape.getPosition().x - circleShape.getRadius(), circleShape.getPosition().y - circleShape.getRadius()), // bottom right
                new Vector2(circleShape.getPosition().x + circleShape.getRadius(), circleShape.getPosition().y - circleShape.getRadius()), // top right
                new Vector2(circleShape.getPosition().x + circleShape.getRadius(), circleShape.getPosition().y + circleShape.getRadius()) };
                break;
            default:
                throw new IllegalArgumentException("Shapes of the type '" + shape.getType().name() + "' are not supported");
        }
        if (autoShapeCache && shapeCache.size < autoShapeCacheMaxSize) {
            Vector2[] cachedOutput = new Vector2[output.length];
            System.arraycopy(output, 0, cachedOutput, 0, output.length);
            shapeCache.put(shape, new ShapeCache(cachedOutput, amplitude(filterX(cachedOutput)), amplitude(filterY(cachedOutput)), min(filterX(cachedOutput)), max(filterX(cachedOutput)), min(filterY(cachedOutput)), max(filterY(cachedOutput))));
        }
        return output;
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Example 52 with Vector2

use of com.badlogic.gdx.math.Vector2 in project Entitas-Java by Rubentxu.

the class Box2DUtils method vertices.

/**
     * @return the vertices of the given Shape
     */
public static Vector2[] vertices(Shape shape, Vector2[] output) {
    if (shapeCache.containsKey(shape))
        return output = shapeCache.get(shape).vertices;
    else {
        switch(shape.getType()) {
            case Polygon:
                PolygonShape polygonShape = (PolygonShape) shape;
                output = new Vector2[polygonShape.getVertexCount()];
                for (int i = 0; i < output.length; i++) {
                    output[i] = new Vector2();
                    polygonShape.getVertex(i, output[i]);
                }
                break;
            case Edge:
                EdgeShape edgeShape = (EdgeShape) shape;
                edgeShape.getVertex1(tmpVec);
                edgeShape.getVertex2(tmpVec2);
                output = new Vector2[] { tmpVec, tmpVec2 };
                break;
            case Chain:
                ChainShape chainShape = (ChainShape) shape;
                output = new Vector2[chainShape.getVertexCount()];
                for (int i = 0; i < output.length; i++) {
                    output[i] = new Vector2();
                    chainShape.getVertex(i, output[i]);
                }
                break;
            case Circle:
                CircleShape circleShape = (CircleShape) shape;
                output = new Vector2[] { // top left
                new Vector2(circleShape.getPosition().x - circleShape.getRadius(), circleShape.getPosition().y + circleShape.getRadius()), // bottom left
                new Vector2(circleShape.getPosition().x - circleShape.getRadius(), circleShape.getPosition().y - circleShape.getRadius()), // bottom right
                new Vector2(circleShape.getPosition().x + circleShape.getRadius(), circleShape.getPosition().y - circleShape.getRadius()), // top right
                new Vector2(circleShape.getPosition().x + circleShape.getRadius(), circleShape.getPosition().y + circleShape.getRadius()) };
                break;
            default:
                throw new IllegalArgumentException("Shapes of the type '" + shape.getType().name() + "' are not supported");
        }
        if (autoShapeCache && shapeCache.size < autoShapeCacheMaxSize) {
            Vector2[] cachedOutput = new Vector2[output.length];
            System.arraycopy(output, 0, cachedOutput, 0, output.length);
            shapeCache.put(shape, new ShapeCache(cachedOutput, amplitude(filterX(cachedOutput)), amplitude(filterY(cachedOutput)), min(filterX(cachedOutput)), max(filterX(cachedOutput)), min(filterY(cachedOutput)), max(filterY(cachedOutput))));
        }
        return output;
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Example 53 with Vector2

use of com.badlogic.gdx.math.Vector2 in project Entitas-Java by Rubentxu.

the class InputManagerGDX method touchUp.

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    int indexPointer = (mouse) ? button : pointer;
    //set the state of all touch state events
    if (indexPointer < inputStateData.pointerStates.length) {
        PointerState<Vector2, Vector3> t = inputStateData.pointerStates[indexPointer];
        t.worldCoordinates.set(screenX, screenY, 0);
        camera.unproject(t.worldCoordinates);
        t.down = false;
        t.released = true;
        t.clickTime = 0;
        t.coordinates.x = t.worldCoordinates.x;
        t.coordinates.y = t.worldCoordinates.y;
        if (joints[indexPointer] != null) {
            physics.destroyJoint(joints[indexPointer]);
            joints[indexPointer] = null;
        }
    }
    return false;
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) Vector3(com.badlogic.gdx.math.Vector3) MouseJoint(com.badlogic.gdx.physics.box2d.joints.MouseJoint)

Example 54 with Vector2

use of com.badlogic.gdx.math.Vector2 in project Entitas-Java by Rubentxu.

the class InputManagerGDX method touchDown.

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    int indexPointer = (mouse) ? button : pointer;
    //set the state of all touch state events
    if (indexPointer < inputStateData.pointerStates.length) {
        PointerState<Vector2, Vector3> t = inputStateData.pointerStates[indexPointer];
        t.down = true;
        t.pressed = true;
        //recording last position for displacement values
        t.lastPosition.x = t.coordinates.x;
        t.lastPosition.y = t.coordinates.y;
        t.worldCoordinates.set(screenX, screenY, 0);
        camera.unproject(t.worldCoordinates);
        //store the coordinates of this touch event
        t.coordinates.x = t.worldCoordinates.x;
        t.coordinates.y = t.worldCoordinates.y;
        physics.QueryAABB(queryCallback, t.coordinates.x, t.coordinates.y, t.coordinates.x, t.coordinates.y);
    }
    return false;
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) Vector3(com.badlogic.gdx.math.Vector3) MouseJoint(com.badlogic.gdx.physics.box2d.joints.MouseJoint)

Example 55 with Vector2

use of com.badlogic.gdx.math.Vector2 in project RubeLoader by tescott.

the class RubeLoaderTest method createPolySpatialsFromRubeFixtures.

/**
    * Creates an array of PolySpatials based on fixture information from the scene. Note that
    * fixtures create aligned textures.
    * 
    * @param scene
    */
private void createPolySpatialsFromRubeFixtures(RubeScene scene) {
    Array<Body> bodies = scene.getBodies();
    EarClippingTriangulator ect = new EarClippingTriangulator();
    if ((bodies != null) && (bodies.size > 0)) {
        polySpatials = new Array<PolySpatial>();
        Vector2 bodyPos = new Vector2();
        // for each body in the scene...
        for (int i = 0; i < bodies.size; i++) {
            Body body = bodies.get(i);
            bodyPos.set(body.getPosition());
            Array<Fixture> fixtures = body.getFixtureList();
            if ((fixtures != null) && (fixtures.size > 0)) {
                // for each fixture on the body...
                for (int j = 0; j < fixtures.size; j++) {
                    Fixture fixture = fixtures.get(j);
                    String textureName = (String) scene.getCustom(fixture, "TextureMask", null);
                    if (textureName != null) {
                        String textureFileName = "data/" + textureName;
                        Texture texture = textureMap.get(textureFileName);
                        TextureRegion textureRegion = null;
                        if (texture == null) {
                            texture = new Texture(textureFileName);
                            texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
                            textureMap.put(textureFileName, texture);
                            textureRegion = new TextureRegion(texture);
                            textureRegionMap.put(texture, textureRegion);
                        } else {
                            textureRegion = textureRegionMap.get(texture);
                        }
                        // only handle polygons at this point -- no chain, edge, or circle fixtures.
                        if (fixture.getType() == Shape.Type.Polygon) {
                            PolygonShape shape = (PolygonShape) fixture.getShape();
                            int vertexCount = shape.getVertexCount();
                            float[] vertices = new float[vertexCount * 2];
                            // static bodies are texture aligned and do not get drawn based off of the related body.
                            if (body.getType() == BodyType.StaticBody) {
                                for (int k = 0; k < vertexCount; k++) {
                                    shape.getVertex(k, mTmp);
                                    mTmp.rotate(body.getAngle() * MathUtils.radiansToDegrees);
                                    // convert local coordinates to world coordinates to that textures are
                                    mTmp.add(bodyPos);
                                    // aligned
                                    vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER;
                                    vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER;
                                }
                                short[] triangleIndices = ect.computeTriangles(vertices).toArray();
                                PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices);
                                PolySpatial spatial = new PolySpatial(region, Color.WHITE);
                                polySpatials.add(spatial);
                            } else {
                                // all other fixtures are aligned based on their associated body.
                                for (int k = 0; k < vertexCount; k++) {
                                    shape.getVertex(k, mTmp);
                                    vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER;
                                    vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER;
                                }
                                short[] triangleIndices = ect.computeTriangles(vertices).toArray();
                                PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices);
                                PolySpatial spatial = new PolySpatial(region, body, Color.WHITE);
                                polySpatials.add(spatial);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) EarClippingTriangulator(com.badlogic.gdx.math.EarClippingTriangulator) Texture(com.badlogic.gdx.graphics.Texture) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Vector2(com.badlogic.gdx.math.Vector2) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Body(com.badlogic.gdx.physics.box2d.Body) PolygonRegion(com.badlogic.gdx.graphics.g2d.PolygonRegion)

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