Search in sources :

Example 11 with Vector2

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

the class KinematicBodyTest method create.

public void create() {
    cam = new OrthographicCamera(48, 32);
    cam.position.set(0, 15, 0);
    renderer = new Box2DDebugRenderer();
    world = new World(new Vector2(0, -10), true);
    Body body = world.createBody(new BodyDef());
    CircleShape shape = new CircleShape();
    shape.setRadius(1f);
    MassData mass = new MassData();
    mass.mass = 1f;
    body.setMassData(mass);
    body.setFixedRotation(true);
    body.setType(BodyType.KinematicBody);
    body.createFixture(shape, 1);
    body.setBullet(true);
    body.setTransform(new Vector2(0, 0), body.getAngle());
    body.setLinearVelocity(new Vector2(50f, 0));
}
Also used : Box2DDebugRenderer(com.badlogic.gdx.physics.box2d.Box2DDebugRenderer) Vector2(com.badlogic.gdx.math.Vector2) CircleShape(com.badlogic.gdx.physics.box2d.CircleShape) MassData(com.badlogic.gdx.physics.box2d.MassData) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) World(com.badlogic.gdx.physics.box2d.World) Body(com.badlogic.gdx.physics.box2d.Body) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef)

Example 12 with Vector2

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

the class ReflectionTest method create.

@Override
public void create() {
    font = new BitmapFont();
    batch = new SpriteBatch();
    try {
        Vector2 fromDefaultConstructor = ClassReflection.newInstance(Vector2.class);
        println("From default constructor: " + fromDefaultConstructor);
        Method mSet = ClassReflection.getMethod(Vector2.class, "set", float.class, float.class);
        mSet.invoke(fromDefaultConstructor, 10, 11);
        println("Set to 10/11: " + fromDefaultConstructor);
        Constructor copyConstroctor = ClassReflection.getConstructor(Vector2.class, Vector2.class);
        Vector2 fromCopyConstructor = (Vector2) copyConstroctor.newInstance(fromDefaultConstructor);
        println("From copy constructor: " + fromCopyConstructor);
        Method mMul = ClassReflection.getMethod(Vector2.class, "scl", float.class);
        println("Multiplied by 2; " + mMul.invoke(fromCopyConstructor, 2));
        Method mNor = ClassReflection.getMethod(Vector2.class, "nor");
        println("Normalized: " + mNor.invoke(fromCopyConstructor));
        Vector2 fieldCopy = new Vector2();
        Field fx = ClassReflection.getField(Vector2.class, "x");
        Field fy = ClassReflection.getField(Vector2.class, "y");
        fx.set(fieldCopy, fx.get(fromCopyConstructor));
        fy.set(fieldCopy, fy.get(fromCopyConstructor));
        println("Copied field by field: " + fieldCopy);
        Json json = new Json();
        String jsonString = json.toJson(fromCopyConstructor);
        Vector2 fromJson = json.fromJson(Vector2.class, jsonString);
        println("JSON serialized: " + jsonString);
        println("JSON deserialized: " + fromJson);
        fromJson.x += 1;
        fromJson.y += 1;
        println("JSON deserialized + 1/1: " + fromJson);
        Object array = ArrayReflection.newInstance(int.class, 5);
        ArrayReflection.set(array, 0, 42);
        println("Array int: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
        array = ArrayReflection.newInstance(String.class, 5);
        ArrayReflection.set(array, 0, "test string");
        println("Array String: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
    } catch (Exception e) {
        message = "FAILED: " + e.getMessage() + "\n";
        message += e.getClass();
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) Vector2(com.badlogic.gdx.math.Vector2) Constructor(com.badlogic.gdx.utils.reflect.Constructor) Method(com.badlogic.gdx.utils.reflect.Method) Json(com.badlogic.gdx.utils.Json) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Example 13 with Vector2

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

the class SelectTest method createDummies.

public static Array<Dummy> createDummies(int n) {
    float variance = 20;
    Array<Dummy> dummies = new Array<Dummy>();
    for (int i = 0; i < n; i++) {
        Dummy d = new Dummy();
        dummies.add(d);
        d.pos = new Vector2();
        d.id = nextID++;
    }
    return dummies;
}
Also used : Array(com.badlogic.gdx.utils.Array) Vector2(com.badlogic.gdx.math.Vector2)

Example 14 with Vector2

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

the class TiledMapPacker method packTilesets.

/** Traverse the specified tilesets, optionally lookup the used ids and pass every tile image to the {@link TexturePacker},
	 * optionally ignoring unused tile ids */
private void packTilesets(FileHandle inputDirHandle, Settings texturePackerSettings) throws IOException {
    BufferedImage tile;
    Vector2 tileLocation;
    Graphics g;
    packer = new TexturePacker(texturePackerSettings);
    for (TiledMapTileSet set : tilesetsToPack.values()) {
        String tilesetName = set.getName();
        System.out.println("Processing tileset " + tilesetName);
        IntArray usedIds = this.settings.stripUnusedTiles ? getUsedIdsBucket(tilesetName, -1) : null;
        int tileWidth = set.getProperties().get("tilewidth", Integer.class);
        int tileHeight = set.getProperties().get("tileheight", Integer.class);
        int firstgid = set.getProperties().get("firstgid", Integer.class);
        String imageName = set.getProperties().get("imagesource", String.class);
        TileSetLayout layout = new TileSetLayout(firstgid, set, inputDirHandle);
        for (int gid = layout.firstgid, i = 0; i < layout.numTiles; gid++, i++) {
            boolean verbose = this.settings.verbose;
            if (usedIds != null && !usedIds.contains(gid)) {
                if (verbose) {
                    System.out.println("Stripped id #" + gid + " from tileset \"" + tilesetName + "\"");
                }
                continue;
            }
            tileLocation = layout.getLocation(gid);
            tile = new BufferedImage(tileWidth, tileHeight, BufferedImage.TYPE_4BYTE_ABGR);
            g = tile.createGraphics();
            g.drawImage(layout.image, 0, 0, tileWidth, tileHeight, (int) tileLocation.x, (int) tileLocation.y, (int) tileLocation.x + tileWidth, (int) tileLocation.y + tileHeight, null);
            if (verbose) {
                System.out.println("Adding " + tileWidth + "x" + tileHeight + " (" + (int) tileLocation.x + ", " + (int) tileLocation.y + ")");
            }
            // AtlasTmxMapLoader expects every tileset's index to begin at zero for the first tile in every tileset.
            // so the region's adjusted gid is (gid - layout.firstgid). firstgid will be added back in AtlasTmxMapLoader on load
            int adjustedGid = gid - layout.firstgid;
            final String separator = "_";
            String regionName = tilesetName + separator + adjustedGid;
            packer.addImage(tile, regionName);
        }
    }
    String tilesetOutputDir = outputDir.toString() + "/" + this.settings.tilesetOutputDirectory;
    File relativeTilesetOutputDir = new File(tilesetOutputDir);
    File outputDirTilesets = new File(relativeTilesetOutputDir.getCanonicalPath());
    outputDirTilesets.mkdirs();
    packer.pack(outputDirTilesets, this.settings.atlasOutputName + ".atlas");
}
Also used : Graphics(java.awt.Graphics) IntArray(com.badlogic.gdx.utils.IntArray) Vector2(com.badlogic.gdx.math.Vector2) TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) TexturePacker(com.badlogic.gdx.tools.texturepacker.TexturePacker) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 15 with Vector2

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

the class Mesh method transformUV.

/** Method to transform the texture coordinates (UV) in the float array. This is a potentially slow operation, use with care.
	 * @param matrix the transformation matrix
	 * @param vertices the float array
	 * @param vertexSize the number of floats in each vertex
	 * @param offset the offset within a vertex to the texture location
	 * @param start the vertex to start with
	 * @param count the amount of vertices to transform */
public static void transformUV(final Matrix3 matrix, final float[] vertices, int vertexSize, int offset, int start, int count) {
    if (start < 0 || count < 1 || ((start + count) * vertexSize) > vertices.length)
        throw new IndexOutOfBoundsException("start = " + start + ", count = " + count + ", vertexSize = " + vertexSize + ", length = " + vertices.length);
    final Vector2 tmp = new Vector2();
    int idx = offset + (start * vertexSize);
    for (int i = 0; i < count; i++) {
        tmp.set(vertices[idx], vertices[idx + 1]).mul(matrix);
        vertices[idx] = tmp.x;
        vertices[idx + 1] = tmp.y;
        idx += vertexSize;
    }
}
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