Body createBox (BodyType type, float width, float height, float density) { BodyDef def = new BodyDef(); def.type = type; Body box = world.createBody(def); PolygonShape poly = new PolygonShape(); poly.setAsBox(width, height); box.createFixture(poly, density); poly.dispose(); return box; }
Body createCircle (BodyType type, float radius, float density) { BodyDef def = new BodyDef(); def.type = type; Body box = world.createBody(def); CircleShape poly = new CircleShape(); poly.setRadius(radius); box.createFixture(poly, density); poly.dispose(); return box; }
private Body createEdge (BodyType type, float x1, float y1, float x2, float y2, float density) { BodyDef def = new BodyDef(); def.type = type; Body box = world.createBody(def); EdgeShape poly = new EdgeShape(); poly.set(new Vector2(0, 0), new Vector2(x2 - x1, y2 - y1)); box.createFixture(poly, density); box.setTransform(x1, y1, 0); poly.dispose(); return box; }
/** Returns the shape of this fixture */ public Shape getShape () { if (shape == null) { org.jbox2d.collision.shapes.Shape shape2 = fixture.getShape(); ShapeType type = shape2.getType(); if (type == ShapeType.CHAIN) shape = new ChainShape((org.jbox2d.collision.shapes.ChainShape)shape2); if (type == ShapeType.CIRCLE) shape = new CircleShape((org.jbox2d.collision.shapes.CircleShape)shape2); if (type == ShapeType.EDGE) shape = new EdgeShape((org.jbox2d.collision.shapes.EdgeShape)shape2); if (type == ShapeType.POLYGON) shape = new PolygonShape((org.jbox2d.collision.shapes.PolygonShape)shape2); } return shape; }
private void drawContact (Contact contact) { WorldManifold worldManifold = contact.getWorldManifold(); if (worldManifold.getNumberOfContactPoints() == 0) return; Vector2 point = worldManifold.getPoints()[0]; renderer.setColor(getColorByBody(contact.getFixtureA().getBody())); renderer.point(point.x, point.y, 0); }
@Override public boolean shouldCollide (Fixture fixtureA, Fixture fixtureB) { if ((fixtureA == m_platform && fixtureB == m_character) || (fixtureB == m_platform && fixtureA == m_character)) { Vector2 position = m_character.getBody().getPosition(); if (position.y < m_top + m_radius - 3.0f * 0.005f) return false; else return true; } else return true; }
/** Set the type of this body. This may alter the mass and velocity. */ public void setType (BodyType type) { jniSetType(addr, type.getValue()); }
/** Set the active state of the body. An inactive body is not simulated and cannot be collided with or woken up. If you pass a * flag of true, all fixtures will be added to the broad-phase. If you pass a flag of false, all fixtures will be removed from * the broad-phase and all contacts will be destroyed. Fixtures and joints are otherwise unaffected. You may continue to * create/destroy fixtures and joints on inactive bodies. Fixtures on an inactive body are implicitly inactive and will not * participate in collisions, ray-casts, or queries. Joints connected to an inactive body are implicitly inactive. An inactive * body is still owned by a b2World object and remains in the body list. */ public void setActive (boolean flag) { if (flag) { jniSetActive(addr, flag); } else { this.world.deactivateBody(this); } }
/** Create a loop. This automatically adjusts connectivity. * @param vertices an array of vertices, these are copied */ public void createLoop (Vector2[] vertices) { float[] verts = new float[vertices.length * 2]; for (int i = 0, j = 0; i < vertices.length * 2; i += 2, j++) { verts[i] = vertices[j].x; verts[i + 1] = vertices[j].y; } jniCreateLoop(addr, verts, 0, verts.length / 2); isLooped = true; }
/** Returns the vertex at the given position. * @param index the index of the vertex 0 <= index < getVertexCount( ) * @param vertex vertex */ public void getVertex (int index, Vector2 vertex) { jniGetVertex(addr, index, verts); vertex.x = verts[0]; vertex.y = verts[1]; }
public ManifoldType getType () { int type = jniGetType(addr); if (type == 0) return ManifoldType.Circle; if (type == 1) return ManifoldType.FaceA; if (type == 2) return ManifoldType.FaceB; return ManifoldType.Circle; }
/** Get the linear velocity of the center of mass. * Note that the same Vector2 instance is returned each time this method is called. */ public Vector2 getLinearVelocity () { jniGetLinearVelocity(addr, tmp); linearVelocity.x = tmp[0]; linearVelocity.y = tmp[1]; return linearVelocity; }
/** Apply an angular impulse. * @param impulse the angular impulse in units of kg*m*m/s */ public void applyAngularImpulse (float impulse, boolean wake) { jniApplyAngularImpulse(addr, impulse, wake); }
/** Get the rotational inertia of the body about the local origin. * @return the rotational inertia, usually in kg-m^2. */ public float getInertia () { return jniGetInertia(addr); }
/** Get the mass data of the body. * @return a struct containing the mass, inertia and center of the body. */ public MassData getMassData () { jniGetMassData(addr, tmp); massData.mass = tmp[0]; massData.center.x = tmp[1]; massData.center.y = tmp[2]; massData.I = tmp[3]; return massData; }
/** Get the world velocity of a local point. * Note that the same Vector2 instance is returned each time this method is called. * @param localPoint a point in local coordinates. * @return the world velocity of a point. */ public Vector2 getLinearVelocityFromLocalPoint (Vector2 localPoint) { jniGetLinearVelocityFromLocalPoint(addr, localPoint.x, localPoint.y, tmp); linVelLoc.x = tmp[0]; linVelLoc.y = tmp[1]; return linVelLoc; }
public Vector2 getAnchorB () { jniGetAnchorB(addr, tmp); anchorB.x = tmp[0]; anchorB.y = tmp[1]; return anchorB; }
public Vector2 getReactionForce (float inv_dt) { jniGetReactionForce(addr, inv_dt, tmp); reactionForce.x = tmp[0]; reactionForce.y = tmp[1]; return reactionForce; }