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));
}
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();
}
}
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;
}
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");
}
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;
}
}
Aggregations