use of spacegraph.space2d.phys.dynamics.Body2D in project narchy by automenta.
the class ParticleSystem method solveViscous.
void solveViscous(final TimeStep step) {
float viscousStrength = m_viscousStrength;
for (int k = 0; k < m_bodyContactCount; k++) {
final ParticleBodyContact contact = m_bodyContactBuffer[k];
int a = contact.index;
if ((m_flagsBuffer.data[a] & ParticleType.b2_viscousParticle) != 0) {
Body2D b = contact.body;
float w = contact.weight;
float m = contact.mass;
Tuple2f p = m_positionBuffer.data[a];
final Tuple2f va = m_velocityBuffer.data[a];
final float tempX = p.x - b.sweep.c.x;
final float tempY = p.y - b.sweep.c.y;
final float vx = -b.velAngular * tempY + b.vel.x - va.x;
final float vy = b.velAngular * tempX + b.vel.y - va.y;
final Tuple2f f = tempVec;
final float pInvMass = getParticleInvMass();
f.x = viscousStrength * m * w * vx;
f.y = viscousStrength * m * w * vy;
va.x += pInvMass * f.x;
va.y += pInvMass * f.y;
f.x = -f.x;
f.y = -f.y;
b.applyLinearImpulse(f, p, true);
}
}
for (int k = 0; k < m_contactCount; k++) {
final ParticleContact contact = m_contactBuffer[k];
if ((contact.flags & ParticleType.b2_viscousParticle) != 0) {
int a = contact.indexA;
int b = contact.indexB;
float w = contact.weight;
final Tuple2f va = m_velocityBuffer.data[a];
final Tuple2f vb = m_velocityBuffer.data[b];
final float vx = vb.x - va.x;
final float vy = vb.y - va.y;
final float fx = viscousStrength * w * vx;
final float fy = viscousStrength * w * vy;
va.x += fx;
va.y += fy;
vb.x -= fx;
vb.y -= fy;
}
}
}
use of spacegraph.space2d.phys.dynamics.Body2D in project narchy by automenta.
the class Contact method getWorldManifold.
/**
* Get the world manifold.
*/
public void getWorldManifold(WorldManifold worldManifold) {
final Body2D bodyA = aFixture.getBody();
final Body2D bodyB = bFixture.getBody();
final Shape shapeA = aFixture.shape();
final Shape shapeB = bFixture.shape();
worldManifold.initialize(m_manifold, bodyA, shapeA.radius, bodyB, shapeB.radius);
}
use of spacegraph.space2d.phys.dynamics.Body2D in project narchy by automenta.
the class Contact method update.
public void update(ContactListener listener) {
oldManifold.set(m_manifold);
// Re-enable this contact.
m_flags |= ENABLED_FLAG;
boolean touching = false;
boolean wasTouching = (m_flags & TOUCHING_FLAG) == TOUCHING_FLAG;
boolean sensorA = aFixture.isSensor();
boolean sensorB = bFixture.isSensor();
boolean sensor = sensorA || sensorB;
Body2D bodyA = aFixture.getBody();
Body2D bodyB = bFixture.getBody();
Transform xfA = bodyA;
Transform xfB = bodyB;
if (sensor) {
Shape shapeA = aFixture.shape();
Shape shapeB = bFixture.shape();
touching = pool.getCollision().testOverlap(shapeA, aIndex, shapeB, bIndex, xfA, xfB);
// Sensors don't generate manifolds.
m_manifold.pointCount = 0;
} else {
evaluate(m_manifold, xfA, xfB);
touching = m_manifold.pointCount > 0;
// stored impulses to warm start the solver.
for (int i = 0; i < m_manifold.pointCount; ++i) {
ManifoldPoint mp2 = m_manifold.points[i];
mp2.normalImpulse = 0.0f;
mp2.tangentImpulse = 0.0f;
ContactID id2 = mp2.id;
for (int j = 0; j < oldManifold.pointCount; ++j) {
ManifoldPoint mp1 = oldManifold.points[j];
if (mp1.id.isEqual(id2)) {
mp2.normalImpulse = mp1.normalImpulse;
mp2.tangentImpulse = mp1.tangentImpulse;
break;
}
}
}
if (touching != wasTouching) {
bodyA.setAwake(true);
bodyB.setAwake(true);
}
}
if (touching) {
m_flags |= TOUCHING_FLAG;
} else {
m_flags &= ~TOUCHING_FLAG;
}
if (!sensor && touching) {
m_angularVelocity_bodyA = aFixture.body.velAngular;
m_linearVelocity_bodyA.set(aFixture.body.vel);
m_angularVelocity_bodyB = bFixture.body.velAngular;
m_linearVelocity_bodyB.set(bFixture.body.vel);
}
if (listener == null) {
return;
}
if (!wasTouching && touching) {
if (!listener.beginContact(this))
touching = false;
}
if (wasTouching && !touching) {
listener.endContact(this);
}
if (!sensor && touching) {
listener.preSolve(this, oldManifold);
}
}
use of spacegraph.space2d.phys.dynamics.Body2D in project narchy by automenta.
the class RevoluteJoint method getJointAngle.
public float getJointAngle() {
final Body2D b1 = A;
final Body2D b2 = B;
return b2.sweep.a - b1.sweep.a - m_referenceAngle;
}
use of spacegraph.space2d.phys.dynamics.Body2D 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);
}
Aggregations