use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.
the class Fracture method smash.
/**
* Rozbije objekt. Upravi objekt world tak, ze vymaze triesteny objekt
* a nahradi ho fragmentami na zaklade nastaveneho materialu a clenskych
* premennych.
*
* @param dt casova dlzka framu
*/
public void smash(Smasher smasher, float dt) {
Shape s = f1.shape;
if (s == null)
return;
if (contact == null) {
// riesi sa staticky prvok, ktory ma priliz maly obsah
b1.setType(BodyType.DYNAMIC);
return;
}
Dynamics2D w = b1.W;
Polygon p = f1.polygon;
if (p == null) {
switch(s.m_type) {
case POLYGON:
PolygonShape ps = (PolygonShape) s;
Tuple2f[] vertices = ps.vertex;
int n = ps.vertices;
p = new Polygon(n);
for (int i = 0; i < n; ++i) {
p.add(vertices[n - i - 1]);
}
break;
case CIRCLE:
CircleShape cs = (CircleShape) s;
p = new Polygon(CIRCLEVERTICES);
float radius = cs.radius;
double u = Math.PI * 2 / CIRCLEVERTICES;
// upravim radius tak, aby bola zachovana velkost obsahu
radius = (float) Math.sqrt(u / Math.sin(u)) * radius;
Tuple2f center = cs.center;
for (int i = 0; i < CIRCLEVERTICES; ++i) {
// uhol
double j = u * i;
float sin = (float) Math.sin(j);
float cos = (float) Math.cos(j);
Tuple2f v = new v2(sin, cos).scaled(radius).added(center);
p.add(v);
}
break;
default:
throw new RuntimeException("Dany typ tvaru nepodporuje stiepenie");
}
}
// sila v zavislosti na pevnosti telesa
float mConst = f1.material.m_rigidity / normalImpulse;
// true, ak f2 je v objekte contact ako m_fixtureA
boolean fixA = f1 == contact.aFixture;
float oldAngularVelocity = fixA ? contact.m_angularVelocity_bodyA : contact.m_angularVelocity_bodyB;
Tuple2f oldLinearVelocity = fixA ? contact.m_linearVelocity_bodyA : contact.m_linearVelocity_bodyB;
b1.setAngularVelocity((b1.velAngular - oldAngularVelocity) * mConst + oldAngularVelocity);
b1.setLinearVelocity(b1.vel.sub(oldLinearVelocity).scaled(mConst).added(oldLinearVelocity));
if (!w.isFractured(f2) && b2.type == BodyType.DYNAMIC && !b2.m_fractureTransformUpdate) {
// ak sa druhy objekt nerozbija, tak sa jej nahodia povodne hodnoty (TREBA MODIFIKOVAT POHYB OBJEKTU, KTORY SPOSOBUJE ROZPAD)
oldAngularVelocity = !fixA ? contact.m_angularVelocity_bodyA : contact.m_angularVelocity_bodyB;
oldLinearVelocity = !fixA ? contact.m_linearVelocity_bodyA : contact.m_linearVelocity_bodyB;
b2.setAngularVelocity((b2.velAngular - oldAngularVelocity) * mConst + oldAngularVelocity);
b2.setLinearVelocity(b2.vel.sub(oldLinearVelocity).scaled(mConst).added(oldLinearVelocity));
b2.setTransform(b2.transformPrev.pos.add(b2.vel.scale(dt)), b2.transformPrev.angle());
// osetruje jbox2d od posuvania telesa pri rieseni kolizie
b2.m_fractureTransformUpdate = true;
}
Tuple2f localPoint = Transform.mulTrans(b1, point);
Tuple2f b1Vec = b1.getLinearVelocityFromWorldPoint(point);
Tuple2f b2Vec = b2.getLinearVelocityFromWorldPoint(point);
Tuple2f localVelocity = b2Vec.subbed(b1Vec);
localVelocity.scaled(dt);
// rodeli to
Polygon[] fragment = m.split(smasher, p, localPoint, localVelocity, normalImpulse);
if (fragment.length <= 1) {
// nerozbilo to na ziadne fragmenty
return;
}
// definuje tela fragmentov - tie maju vsetky rovnaku definiciu (preberaju parametre z povodneho objektu)
BodyDef bodyDef = new BodyDef();
// pozicia
bodyDef.position.set(b1.pos);
// otocenie
bodyDef.angle = b1.angle();
bodyDef.fixedRotation = b1.isFixedRotation();
bodyDef.angularDamping = b1.m_angularDamping;
bodyDef.allowSleep = b1.isSleepingAllowed();
FixtureDef fd = new FixtureDef();
// trenie
fd.friction = f1.friction;
// odrazivost
fd.restitution = f1.restitution;
fd.isSensor = f1.isSensor;
fd.density = f1.density;
// odstrani fragmentacne predmety/cele teleso
List<Fixture> fixtures = new FasterList<>();
if (f1.polygon != null) {
for (Fixture f = b1.fixtures; f != null; f = f.next) {
if (f.polygon == f1.polygon) {
fixtures.add(f);
}
}
} else {
fixtures.add(f1);
}
for (Fixture f : fixtures) {
b1.removeFixture(f);
}
if (b1.fixtureCount == 0) {
w.removeBody(b1);
}
// prida fragmenty do simulacie
MyList<Body2D> newbodies = new MyList<>();
for (Polygon pg : fragment) {
// vytvori tela, prida fixtury, poriesi konvexnu dekompoziciu
if (pg.isCorrect()) {
if (pg instanceof Fragment) {
Polygon[] convex = pg.convexDecomposition();
bodyDef.type = BodyType.DYNAMIC;
for (Polygon pgx : convex) {
Body2D f_body = w.addBody(bodyDef);
pgx.flip();
PolygonShape ps = new PolygonShape();
ps.set(pgx.getArray(), pgx.size());
fd.shape = ps;
fd.polygon = null;
fd.material = f1.material;
// .m_fragments; //rekurzivne stiepenie
f_body.addFixture(fd);
f_body.setAngularVelocity(b1.velAngular);
f_body.setLinearVelocity(b1.getLinearVelocityFromLocalPoint(f_body.getLocalCenter()));
newbodies.add(f_body);
}
} else {
fd.material = // .m_fragments; //rekurzivne stiepenie
f1.material;
bodyDef.type = b1.getType();
Body2D f_body = w.addBody(bodyDef);
PolygonFixture pf = new PolygonFixture(pg);
f_body.addFixture(pf, fd);
f_body.setLinearVelocity(b1.getLinearVelocityFromLocalPoint(f_body.getLocalCenter()));
f_body.setAngularVelocity(b1.velAngular);
newbodies.add(f_body);
}
}
}
// zavola sa funkcia z fraction listeneru (pokial je nadefinovany)
FractureListener fl = w.getContactManager().m_fractureListener;
if (fl != null) {
fl.action(m, normalImpulse, newbodies);
}
}
use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.
the class Body2D method addFixture.
/**
* Vytvori lubovolny simple konkavny objekt s lubovolnym poctom vrcholov.
* funkcia urobi konvexnu dekompoziciu polygonu a aplikuje na ne jednotlive
* konvexne fixtures, ktore budu okrem ineho zachovavat limit
* Settings.maxPolygonVertices. Funkcia je pocas callbacku zamknuta.
* FixtudeDef prepise 2 svoje premenne - tie sa definuju algoritmom, ostatne
* sa prenesu na novovzniknute Fixtury.
*
* @param polygon
* @param def
*/
public final void addFixture(PolygonFixture polygon, FixtureDef def) {
Polygon[] convex = polygon.convexDecomposition();
def.polygon = convex.length > 1 ? polygon : null;
for (Polygon p : convex) {
p.flip();
PolygonShape ps = new PolygonShape();
ps.set(p.getArray(), p.size());
def.shape = ps;
polygon.fixtureList.add(addFixture(def));
}
}
use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.
the class PhyWall method drawBody.
private void drawBody(Body2D body, GL2 gl) {
if (body.data() instanceof PhyWindow.WallBody) {
// its rendered already via its Surface
return;
}
if (body instanceof Consumer) {
// HACK make better custom enderer interface
((Consumer) body).accept(gl);
return;
}
// boolean active = body.isActive();
boolean awake = body.isAwake();
gl.glColor4f(0.5f, 0.5f, 0.5f, awake ? 0.75f : 0.65f);
// List<PolygonFixture> generalPolygons = new FasterList<>();
for (Fixture f = body.fixtures; f != null; f = f.next) {
PolygonFixture pg = f.polygon;
if (pg != null) {
// generalPolygons.add(pg);
} else {
Shape shape = f.shape();
switch(shape.m_type) {
case POLYGON:
Draw.poly(body, gl, (PolygonShape) shape);
break;
case CIRCLE:
CircleShape circle = (CircleShape) shape;
float r = circle.radius;
v2 v = new v2();
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);
Draw.circle(gl, v, true, r, 9);
break;
case EDGE:
EdgeShape edge = (EdgeShape) shape;
Tuple2f p1 = edge.m_vertex1;
Tuple2f p2 = edge.m_vertex2;
gl.glLineWidth(4f);
Draw.line(gl, 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);
// }
}
use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.
the class BlobTest4 method init.
@Override
public void init(Dynamics2D w) {
Body2D ground = null;
{
PolygonShape sd = new PolygonShape();
sd.setAsBox(50.0f, 0.4f);
BodyDef bd = new BodyDef();
bd.position.set(0.0f, 0.0f);
ground = w.addBody(bd);
ground.addFixture(sd, 0f);
sd.setAsBox(0.4f, 50.0f, new v2(-10.0f, 0.0f), 0.0f);
ground.addFixture(sd, 0f);
sd.setAsBox(0.4f, 50.0f, new v2(10.0f, 0.0f), 0.0f);
ground.addFixture(sd, 0f);
}
ConstantVolumeJointDef cvjd = new ConstantVolumeJointDef();
float cx = 0.0f;
float cy = 10.0f;
float rx = 5.0f;
float ry = 5.0f;
int nBodies = 40;
float bodyRadius = 0.25f;
for (int i = 0; i < nBodies; ++i) {
float angle = MathUtils.map(i, 0, nBodies, 0, 2 * 3.1415f);
BodyDef bd = new BodyDef();
// bd.isBullet = true;
bd.fixedRotation = true;
float x = cx + rx * (float) Math.sin(angle);
float y = cy + ry * (float) Math.cos(angle);
bd.position.set(new v2(x, y));
bd.type = BodyType.DYNAMIC;
Body2D body = w.addBody(bd);
FixtureDef fd = new FixtureDef();
CircleShape cd = new CircleShape();
cd.radius = bodyRadius;
fd.shape = cd;
fd.density = 1.0f;
body.addFixture(fd);
cvjd.addBody(body);
}
cvjd.frequencyHz = 10.0f;
cvjd.dampingRatio = 0.9f;
cvjd.collideConnected = false;
w.addJoint(cvjd);
BodyDef bd2 = new BodyDef();
bd2.type = BodyType.DYNAMIC;
PolygonShape psd = new PolygonShape();
psd.setAsBox(3.0f, 1.5f, new v2(cx, cy + 15.0f), 0.0f);
bd2.position = new v2(cx, cy + 15.0f);
Body2D fallingBox = w.addBody(bd2);
fallingBox.addFixture(psd, 1.0f);
}
use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.
the class TheoJansenTest method createLeg.
void createLeg(float s, v2 wheelAnchor) {
v2 p1 = new v2(5.4f * s, -6.1f);
v2 p2 = new v2(7.2f * s, -1.2f);
v2 p3 = new v2(4.3f * s, -1.9f);
v2 p4 = new v2(3.1f * s, 0.8f);
v2 p5 = new v2(6.0f * s, 1.5f);
v2 p6 = new v2(2.5f * s, 3.7f);
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 = m_offset;
bd2.position = p4.add(m_offset);
bd1.angularDamping = 10.0f;
bd2.angularDamping = 10.0f;
Body2D body1 = w.addBody(bd1);
Body2D body2 = w.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(m_offset), p5.add(m_offset));
w.addJoint(djd);
djd.initialize(body1, body2, p3.add(m_offset), p4.add(m_offset));
w.addJoint(djd);
djd.initialize(body1, m_wheel, p3.add(m_offset), wheelAnchor.add(m_offset));
w.addJoint(djd);
djd.initialize(body2, m_wheel, p6.add(m_offset), wheelAnchor.add(m_offset));
w.addJoint(djd);
RevoluteJointDef rjd = new RevoluteJointDef();
rjd.initialize(body2, m_chassis, p4.add(m_offset));
w.addJoint(rjd);
}
Aggregations