use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.
the class EnvironmentScriptingAccessor method physics2dCreateBodyWithShape.
public Physics2dBody physics2dCreateBodyWithShape(boolean fixed, ArrayRealVector position, List verticesPoints, float linearDamping, float angularDamping, float mass, float restitution, float friction) {
PolygonShape shape = new PolygonShape();
Vec2[] vertices = convertVerticesFromArrayRealVectorGenericList(verticesPoints);
shape.set(vertices, vertices.length);
// shape.m_centroid.set(bodyDef.position);
BodyDef bodyDefinition = new BodyDef();
bodyDefinition.linearDamping = linearDamping;
bodyDefinition.angularDamping = angularDamping;
if (fixed) {
bodyDefinition.type = BodyType.STATIC;
} else {
bodyDefinition.type = BodyType.DYNAMIC;
}
bodyDefinition.position.set((float) position.getDataRef()[0], (float) position.getDataRef()[1]);
Body body = environment.physicsWorld2d.createBody(bodyDefinition);
Fixture fixture = body.createFixture(shape, mass);
fixture.setRestitution(restitution);
fixture.setFriction(friction);
return new Physics2dBody(body);
}
use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.
the class EnvironmentScriptingAccessor method physics2dCreateBody.
public Physics2dBody physics2dCreateBody(boolean fixed, String shapeType, ArrayRealVector position, ArrayRealVector size, float radius, float density, float friction) {
BodyDef bodyDefinition = new BodyDef();
bodyDefinition.position.set(new Vec2((float) position.getDataRef()[0], (float) position.getDataRef()[1]));
if (fixed) {
bodyDefinition.type = BodyType.STATIC;
} else {
bodyDefinition.type = BodyType.DYNAMIC;
}
Body body = environment.physicsWorld2d.createBody(bodyDefinition);
PolygonShape polygonShape = new PolygonShape();
if (shapeType.equals("BOX")) {
polygonShape.setAsBox((float) size.getDataRef()[0] * 0.5f, (float) size.getDataRef()[1] * 0.5f);
} else {
throw new InternalError();
}
if (fixed) {
body.createFixture(polygonShape, 0.0f);
} else {
FixtureDef fixture = new FixtureDef();
fixture.shape = polygonShape;
fixture.density = density;
fixture.friction = friction;
body.createFixture(fixture);
}
return new Physics2dBody(body);
}
use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.
the class TheoJansen method createLeg.
void createLeg(float s, v2 wheelAnchor) {
v2 p1 = new v2(5.4f * s, -6.1f * abs(s));
v2 p2 = new v2(7.2f * s, -1.2f * abs(s));
v2 p3 = new v2(4.3f * s, -1.9f * abs(s));
v2 p4 = new v2(3.1f * s, 0.8f * abs(s));
v2 p5 = new v2(6.0f * s, 1.5f * abs(s));
v2 p6 = new v2(2.5f * s, 3.7f * abs(s));
FixtureDef fd1 = new FixtureDef();
FixtureDef fd2 = new FixtureDef();
fd1.filter.groupIndex = -1;
fd2.filter.groupIndex = -1;
fd1.density = 1.0f;
fd2.density = 1.0f;
PolygonShape poly1 = new PolygonShape();
PolygonShape poly2 = new PolygonShape();
if (s > 0.0f) {
v2[] vertices = new v2[3];
vertices[0] = p1;
vertices[1] = p2;
vertices[2] = p3;
poly1.set(vertices, 3);
vertices[0] = new v2();
vertices[1] = p5.sub(p4);
vertices[2] = p6.sub(p4);
poly2.set(vertices, 3);
} else {
v2[] vertices = new v2[3];
vertices[0] = p1;
vertices[1] = p3;
vertices[2] = p2;
poly1.set(vertices, 3);
vertices[0] = new v2();
vertices[1] = p6.sub(p4);
vertices[2] = p5.sub(p4);
poly2.set(vertices, 3);
}
fd1.shape = poly1;
fd2.shape = poly2;
BodyDef bd1 = new BodyDef(), bd2 = new BodyDef();
bd1.type = BodyType.DYNAMIC;
bd2.type = BodyType.DYNAMIC;
bd1.position = center;
bd2.position = p4.add(center);
bd1.angularDamping = 10.0f;
bd2.angularDamping = 10.0f;
Body2D body1 = world.addBody(bd1);
Body2D body2 = world.addBody(bd2);
body1.addFixture(fd1);
body2.addFixture(fd2);
DistanceJointDef djd = new DistanceJointDef();
// Using a soft distance constraint can reduce some jitter.
// It also makes the structure seem a bit more fluid by
// acting like a suspension system.
djd.dampingRatio = 0.5f;
djd.frequencyHz = 10.0f;
djd.initialize(body1, body2, p2.add(center), p5.add(center));
world.addJoint(djd);
djd.initialize(body1, body2, p3.add(center), p4.add(center));
world.addJoint(djd);
djd.initialize(body1, wheel, p3.add(center), wheelAnchor.add(center));
world.addJoint(djd);
djd.initialize(body2, wheel, p6.add(center), wheelAnchor.add(center));
world.addJoint(djd);
RevoluteJointDef rjd = new RevoluteJointDef();
rjd.initialize(body2, chassis, p4.add(center));
world.addJoint(rjd);
}
use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.
the class TheoJansenTest method init.
// @Override
// public Long getTag(Body2D argBody) {
// if (argBody2D == m_chassis) {
// return CHASSIS_TAG;
// } else if (argBody2D == m_wheel) {
// return WHEEL_TAG;
// }
// return null;
// }
//
// @Override
// public Long getTag(Joint argJoint) {
// if (argJoint == m_motorJoint) {
// return MOTOR_TAG;
// }
// return null;
// }
// @Override
// public void processBody(Body2D argBody, Long argTag) {
// if (argTag == CHASSIS_TAG) {
// m_chassis = argBody;
// } else if (argTag == WHEEL_TAG) {
// m_wheel = argBody;
// }
// }
//
// @Override
// public void processJoint(Joint argJoint, Long argTag) {
// if (argTag == MOTOR_TAG) {
// m_motorJoint = (RevoluteJoint) argJoint;
// m_motorOn = m_motorJoint.isMotorEnabled();
// }
// }
@Override
public void init(Dynamics2D w) {
this.w = w;
m_offset.set(0.0f, 8.0f);
m_motorSpeed = 2.0f;
m_motorOn = true;
v2 pivot = new v2(0.0f, 0.8f);
// Ground
{
BodyDef bd = new BodyDef();
Body2D ground = w.addBody(bd);
EdgeShape shape = new EdgeShape();
shape.set(new v2(-50.0f, 0.0f), new v2(50.0f, 0.0f));
ground.addFixture(shape, 0.0f);
shape.set(new v2(-50.0f, 0.0f), new v2(-50.0f, 10.0f));
ground.addFixture(shape, 0.0f);
shape.set(new v2(50.0f, 0.0f), new v2(50.0f, 10.0f));
ground.addFixture(shape, 0.0f);
}
// Balls
for (int i = 0; i < 40; ++i) {
CircleShape shape = new CircleShape();
shape.radius = 0.25f;
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(-40.0f + 2.0f * i, 0.5f);
Body2D body = w.addBody(bd);
body.addFixture(shape, 1.0f);
}
// Chassis
{
PolygonShape shape = new PolygonShape();
shape.setAsBox(2.5f, 1.0f);
FixtureDef sd = new FixtureDef();
sd.density = 1.0f;
sd.shape = shape;
sd.filter.groupIndex = -1;
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(pivot).added(m_offset);
m_chassis = w.addBody(bd);
m_chassis.addFixture(sd);
}
{
CircleShape shape = new CircleShape();
shape.radius = 1.6f;
FixtureDef sd = new FixtureDef();
sd.density = 1.0f;
sd.shape = shape;
sd.filter.groupIndex = -1;
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(pivot).added(m_offset);
m_wheel = w.addBody(bd);
m_wheel.addFixture(sd);
}
{
RevoluteJointDef jd = new RevoluteJointDef();
jd.initialize(m_wheel, m_chassis, pivot.add(m_offset));
jd.collideConnected = false;
jd.motorSpeed = m_motorSpeed;
jd.maxMotorTorque = 400.0f;
jd.enableMotor = m_motorOn;
m_motorJoint = (RevoluteJoint) w.addJoint(jd);
}
v2 wheelAnchor;
wheelAnchor = pivot.add(new v2(0.0f, -0.8f));
createLeg(-1.0f, wheelAnchor);
createLeg(1.0f, wheelAnchor);
m_wheel.setTransform(m_wheel.getPosition(), 120.0f * MathUtils.PI / 180.0f);
createLeg(-1.0f, wheelAnchor);
createLeg(1.0f, wheelAnchor);
m_wheel.setTransform(m_wheel.getPosition(), -120.0f * MathUtils.PI / 180.0f);
createLeg(-1.0f, wheelAnchor);
createLeg(1.0f, wheelAnchor);
}
use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.
the class Box2DTests method drawBody.
private void drawBody(Body2D body) {
if (body.getType() == BodyType.DYNAMIC) {
g.setColor(Color.LIGHT_GRAY);
} else {
g.setColor(Color.GRAY);
}
Tuple2f v = new Vec2();
MyList<PolygonFixture> generalPolygons = new MyList<>();
for (Fixture f = body.fixtures; f != null; f = f.next) {
PolygonFixture pg = f.polygon;
if (pg != null) {
if (!generalPolygons.contains(pg)) {
generalPolygons.add(pg);
}
} else {
Shape shape = f.shape();
switch(shape.m_type) {
case POLYGON:
PolygonShape poly = (PolygonShape) shape;
for (int i = 0; i < poly.vertices; ++i) {
body.getWorldPointToOut(poly.vertex[i], v);
Point p = getPoint(v);
x[i] = p.x;
y[i] = p.y;
}
g.fillPolygon(x, y, poly.vertices);
break;
case CIRCLE:
CircleShape circle = (CircleShape) shape;
float r = circle.radius;
body.getWorldPointToOut(circle.center, v);
Point p = getPoint(v);
int wr = (int) (r * zoom);
g.fillOval(p.x - wr, p.y - wr, wr * 2, wr * 2);
break;
case EDGE:
EdgeShape edge = (EdgeShape) shape;
Tuple2f v1 = edge.m_vertex1;
Tuple2f v2 = edge.m_vertex2;
Point p1 = getPoint(v1);
Point p2 = getPoint(v2);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
break;
}
}
}
if (generalPolygons.size() != 0) {
PolygonFixture[] polygonArray = generalPolygons.toArray(new PolygonFixture[generalPolygons.size()]);
for (PolygonFixture poly : polygonArray) {
int n = poly.size();
int[] x = new int[n];
int[] y = new int[n];
for (int i = 0; i < n; ++i) {
body.getWorldPointToOut(poly.get(i), v);
Point p = getPoint(v);
x[i] = p.x;
y[i] = p.y;
}
g.fillPolygon(x, y, n);
}
}
}
Aggregations