List of usage examples for com.badlogic.gdx.physics.box2d Fixture getFriction
public float getFriction()
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//w w w . j a v a 2 s .co 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:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DUtils.java
License:Apache License
/** @param fixture the fixture for which to setup a new {@link FixtureDef} * @return a new {@link FixtureDef} instance that can be used to clone the given fixture */ public static FixtureDef createDef(Fixture fixture) { FixtureDef fixtureDef = new FixtureDef(); fixtureDef.density = fixture.getDensity(); Filter filter = fixture.getFilterData(); fixtureDef.filter.categoryBits = filter.categoryBits; fixtureDef.filter.groupIndex = filter.groupIndex; fixtureDef.filter.maskBits = filter.maskBits; fixtureDef.friction = fixture.getFriction(); fixtureDef.isSensor = fixture.isSensor(); fixtureDef.restitution = fixture.getRestitution(); fixtureDef.shape = fixture.getShape(); return fixtureDef; }
From source file:edu.lehigh.cse.lol.Actor.java
License:Open Source License
/** * Change the size of an actor, and/or change its position * * @param x The new X coordinate of its bottom left corner * @param y The new Y coordinate of its bototm left corner * @param width The new width of the actor * @param height The new height of the actor *//*www .jav a 2s. c om*/ public void resize(float x, float y, float width, float height) { // To scale a polygon, we'll need a scaling factor, so we can // manually scale each point float xscale = height / mSize.y; float yscale = width / mSize.x; // set new height and width mSize.x = width; mSize.y = height; // read old body information Body oldBody = mBody; // make a new body if (mIsCircleBody) { Fixture oldFix = oldBody.getFixtureList().get(0); setCirclePhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(), oldBody.isBullet(), x, y, (width > height) ? width / 2 : height / 2); } else if (mIsBoxBody) { Fixture oldFix = oldBody.getFixtureList().get(0); setBoxPhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(), oldBody.isBullet(), x, y); } else if (mIsPolygonBody) { Fixture oldFix = oldBody.getFixtureList().get(0); // we need to manually scale all the vertices PolygonShape ps = (PolygonShape) oldFix.getShape(); float[] verts = new float[ps.getVertexCount() * 2]; for (int i = 0; i < ps.getVertexCount(); ++i) { ps.getVertex(i, mTmpVert); verts[2 * i] = mTmpVert.x * xscale; verts[2 * i + 1] = mTmpVert.y * yscale; } setPolygonPhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(), oldBody.isBullet(), x, y, verts); } // clone forces mBody.setAngularVelocity(oldBody.getAngularVelocity()); mBody.setTransform(mBody.getPosition(), oldBody.getAngle()); mBody.setGravityScale(oldBody.getGravityScale()); mBody.setLinearDamping(oldBody.getLinearDamping()); mBody.setLinearVelocity(oldBody.getLinearVelocity()); // disable the old body oldBody.setActive(false); }
From source file:net.dermetfan.utils.libgdx.box2d.Box2DUtils.java
License:Apache License
/** creates a deep copy of a {@link Fixture} * @param fixture the {@link Fixture} to copy * @param body the {@link Body} to create a copy of the given {@code fixture} on * @param shape if the {@link Fixture#getShape() shape} of the given {@code fixture} should be deep {@link #copy(Shape) copied} as well * @return the copied {@link Fixture} */ public static Fixture copy(Fixture fixture, Body body, boolean shape) { FixtureDef fixtureDef = new FixtureDef(); fixtureDef.density = fixture.getDensity(); Filter filter = fixture.getFilterData(); fixtureDef.filter.categoryBits = filter.categoryBits; fixtureDef.filter.groupIndex = filter.groupIndex; fixtureDef.filter.maskBits = filter.maskBits; fixtureDef.friction = fixture.getFriction(); fixtureDef.isSensor = fixture.isSensor(); fixtureDef.restitution = fixture.getRestitution(); fixtureDef.shape = shape ? copy(fixture.getShape()) : fixture.getShape(); Fixture copy = body.createFixture(fixtureDef); copy.setUserData(copy.getUserData()); return copy;/*from w ww . ja va2 s.c o m*/ }
From source file:org.ams.physics.things.def.DefParser.java
License:Open Source License
public static void thingWithBodyToDefinition(ThingWithBody thingWithBody, ThingWithBodyDef def) { Body body = thingWithBody.getBody(); Fixture firstFixture = body.getFixtureList().first(); thingToDefinition(thingWithBody, def); def.active = body.isActive();/*w ww. j a v a2s. c om*/ def.angle = body.getAngle(); def.angularDamping = body.getAngularDamping(); def.antiCollisionGroup = thingWithBody.getAntiCollisionGroup(); def.awake = body.isAwake(); def.bullet = body.isBullet(); def.categoryBits = firstFixture.getFilterData().categoryBits; def.density = firstFixture.getDensity(); def.fixedRotation = body.isFixedRotation(); def.friction = firstFixture.getFriction(); def.groupIndex = firstFixture.getFilterData().groupIndex; def.linearDamping = body.getLinearDamping(); def.maskBits = firstFixture.getFilterData().maskBits; def.position.set(body.getPosition()); def.restitution = firstFixture.getRestitution(); def.type = body.getType(); }