use of spacegraph.space2d.phys.collision.shapes.CircleShape 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.CircleShape 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.CircleShape 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.CircleShape in project narchy by automenta.
the class TensorGlow method main.
public static void main(String[] args) {
PhyWall p = SpaceGraph.wall(1200, 1000);
p.W.setGravity(new v2(0, -2.8f));
staticBox(p.W, -5, -8, +5, 2f, false, true, true, true);
for (int j = 0; j < 3; j++) {
BodyDef bodyDef2 = new BodyDef();
bodyDef2.type = BodyType.DYNAMIC;
// otocenie
bodyDef2.angle = -0.6f;
// smer pohybu
bodyDef2.linearVelocity = new v2(0.0f, 0.0f);
// rotacia (rychlost rotacie)
bodyDef2.angularVelocity = 0.0f;
Body2D newBody = p.W.addBody(bodyDef2);
PolygonShape shape2 = new PolygonShape();
shape2.setAsBox(0.25f, 0.25f);
Fixture f = newBody.addFixture(shape2, 1.0f);
// trenie
f.friction = 0.5f;
// odrazivost
f.restitution = 0.0f;
f.material = new Uniform();
f.material.m_rigidity = 1.0f;
}
// //ceiling rack
// addBox(p.W, -1, +0.4f, 0, +0.65f, false, true, true, true);
// new Pacman(p.W);
{
p.W.setContactListener(new Explosives.ExplosionContacts());
TheoJansen t = new TheoJansen(p.W, 0.35f);
PhyWall.PhyWindow pw = p.put(new Gridding(0.5f, new Port((float[] v) -> {
// System.out.println(v);
t.motorJoint.setMotorSpeed(v[0] * 2 - v[1] * 2);
t.motorJoint.setMaxMotorTorque(v[2]);
t.motorJoint.enableLimit(true);
t.motorJoint.setLimits((float) (-v[3] * Math.PI), (float) (+v[4] * Math.PI));
if (v[5] > 0.5f) {
t.gun.fire();
}
t.turretJoint.setLimits((float) (+Math.PI / 2 + v[6] * Math.PI - 0.1f), (float) (+Math.PI / 2 + v[6] * Math.PI + 0.1f));
})), 0.8f, 0.4f);
p.W.addJoint(new RevoluteJoint(p.W, new RevoluteJointDef(pw.body, t.chassis)));
}
{
p.W.setParticleRadius(0.05f);
p.W.setParticleDamping(0.1f);
CircleShape shape = new CircleShape();
shape.center.set(0, 10);
shape.radius = 2f;
ParticleGroupDef pd = new ParticleGroupDef();
pd.flags = ParticleType.b2_waterParticle;
// b2_viscousParticle;
// b2_elasticParticle;
// b2_springParticle;
// b2_powderParticle;
pd.color = new ParticleColor(0.7f, 0.1f, 0.1f, 0.8f);
pd.shape = shape;
p.W.addParticles(pd);
}
HaiQae q = new HaiQae(8, 2);
float[] in = new float[q.ae.inputs()];
final Tensor randomVector = Tensor.randomVectorGauss(in.length, 0, 1, rng);
final FloatRange lerpRate = new FloatRange(0.01f, 0, 1f);
final TensorLERP lerpVector = new TensorLERP(randomVector, lerpRate);
PhyWall.PhyWindow w = p.put(new Gridding(0.25f, new AutoUpdateMatrixView(lerpVector.data), new LabeledPane("lerp", new XYSlider().on((x, y) -> {
lerpRate.set(x);
})), new LabeledPane("out", new Port((x) -> {
}) {
@Override
public void prePaint(int dtMS) {
super.prePaint(dtMS);
out(lerpVector.data);
}
})), 0.5f, 0.5f);
p.put(new TogglePort(), 0.25f, 0.25f);
PhyWall.PhyWindow qw = p.put(new Gridding(new Label("HaiQ"), new AutoSurface<>(q), new LabeledPane("input", new Port((float[] i) -> {
System.arraycopy(i, 0, in, 0, i.length);
})), new Gridding(VERTICAL, new AutoUpdateMatrixView(in), new AutoUpdateMatrixView(q.ae.x), new AutoUpdateMatrixView(q.ae.W), new AutoUpdateMatrixView(q.ae.y)), new Gridding(VERTICAL, new AutoUpdateMatrixView(q.q), new AutoUpdateMatrixView(q.et))), 1, 1);
Loop.of(() -> {
lerpVector.update();
q.act((((float) Math.random()) - 0.5f) * 2, in);
}).runFPS(25);
}
use of spacegraph.space2d.phys.collision.shapes.CircleShape in project narchy by automenta.
the class VerletTest method init.
@Override
public void init(Dynamics2D w) {
// physics.addBehavior(new GravityBehavior2D(new Vec2D(0,0.1)));
// physics.setWorldBounds(new Rect(0,0,width,height));
List<Body2D> particles = new FasterList();
for (int y = 0, idx = 0; y < DIM; y++) {
for (int x = 0; x < DIM; x++) {
Body2D p = w.addBody(new BodyDef(BodyType.DYNAMIC), new FixtureDef(new CircleShape(REST_LENGTH / 4f), 0.1f, 0f));
p.setTransform(new v2(x * REST_LENGTH, y * REST_LENGTH), 0);
particles.add(p);
if (x > 0) {
spring(w, p, particles.get(idx - 1), STRENGTH, REST_LENGTH);
}
if (y > 0) {
spring(w, p, particles.get(idx - DIM), STRENGTH, REST_LENGTH);
}
idx++;
}
}
spring(w, particles.get(0), particles.get(particles.size() - 1), INNER_STRENGTH, (float) sqrt(sqr(REST_LENGTH * (DIM - 1)) * 2));
spring(w, particles.get(DIM - 1), particles.get(particles.size() - DIM), INNER_STRENGTH, (float) sqrt(sqr(REST_LENGTH * (DIM - 1)) * 2));
// head=physics.particles.get((DIM-1)/2);
// head.lock();
}
Aggregations