use of spacegraph.space2d.phys.collision.shapes.Shape 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);
}
}
}
use of spacegraph.space2d.phys.collision.shapes.Shape in project narchy by automenta.
the class ParticleSystem method createParticleGroup.
public ParticleGroup createParticleGroup(Dynamics2D world, ParticleGroupDef groupDef) {
ParticleGroup group = new ParticleGroup();
group.m_system = this;
group.m_groupFlags = groupDef.groupFlags;
group.m_strength = groupDef.strength;
group.m_userData = groupDef.userData;
group.m_destroyAutomatically = groupDef.destroyAutomatically;
world.invoke(() -> {
float stride = getParticleStride();
final Transform identity = tempTransform;
identity.setIdentity();
Transform transform = tempTransform2;
transform.setIdentity();
int firstIndex = m_count;
if (groupDef.shape != null) {
final ParticleDef particleDef = tempParticleDef;
particleDef.flags = groupDef.flags;
particleDef.color = groupDef.color;
particleDef.userData = groupDef.userData;
Shape shape = groupDef.shape;
transform.set(groupDef.position, groupDef.angle);
AABB aabb = temp;
int childCount = shape.getChildCount();
for (int childIndex = 0; childIndex < childCount; childIndex++) {
if (childIndex == 0) {
shape.computeAABB(aabb, identity, childIndex);
} else {
AABB childAABB = temp2;
shape.computeAABB(childAABB, identity, childIndex);
aabb.combine(childAABB);
}
}
final float upperBoundY = aabb.upperBound.y;
final float upperBoundX = aabb.upperBound.x;
for (float y = MathUtils.floor(aabb.lowerBound.y / stride) * stride; y < upperBoundY; y += stride) {
for (float x = MathUtils.floor(aabb.lowerBound.x / stride) * stride; x < upperBoundX; x += stride) {
v2 p = tempVec;
p.x = x;
p.y = y;
if (shape.testPoint(identity, p)) {
Transform.mulToOut(transform, p, p);
particleDef.position.x = p.x;
particleDef.position.y = p.y;
p.subbed(groupDef.position);
Tuple2f.crossToOutUnsafe(groupDef.angularVelocity, p, particleDef.velocity);
particleDef.velocity.addLocal(groupDef.linearVelocity);
createParticle(particleDef);
}
}
}
}
int lastIndex = m_count;
group.m_firstIndex = firstIndex;
group.m_lastIndex = lastIndex;
group.m_transform.set(transform);
group.m_prev = null;
group.m_next = m_groupList;
if (m_groupList != null) {
m_groupList.m_prev = group;
}
m_groupList = group;
++m_groupCount;
// Arrays.fill..
for (int i = firstIndex; i < lastIndex; i++) {
m_groupBuffer[i] = group;
}
updateContacts(true);
if ((groupDef.flags & k_pairFlags) != 0) {
for (int k = 0; k < m_contactCount; k++) {
ParticleContact contact = m_contactBuffer[k];
int a = contact.indexA;
int b = contact.indexB;
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (firstIndex <= a && b < lastIndex) {
if (m_pairCount >= m_pairCapacity) {
int oldCapacity = m_pairCapacity;
int newCapacity = m_pairCount != 0 ? 2 * m_pairCount : Settings.minParticleBufferCapacity;
m_pairBuffer = BufferUtils.reallocateBuffer(Pair.class, m_pairBuffer, oldCapacity, newCapacity);
m_pairCapacity = newCapacity;
}
Pair pair = m_pairBuffer[m_pairCount];
pair.indexA = a;
pair.indexB = b;
pair.flags = contact.flags;
pair.strength = groupDef.strength;
pair.distance = MathUtils.distance(m_positionBuffer.data[a], m_positionBuffer.data[b]);
m_pairCount++;
}
}
}
if ((groupDef.flags & k_triadFlags) != 0) {
VoronoiDiagram diagram = new VoronoiDiagram(lastIndex - firstIndex);
for (int i = firstIndex; i < lastIndex; i++) {
diagram.addGenerator(m_positionBuffer.data[i], i);
}
diagram.generate(stride / 2);
createParticleGroupCallback.system = this;
createParticleGroupCallback.def = groupDef;
createParticleGroupCallback.firstIndex = firstIndex;
diagram.getNodes(createParticleGroupCallback);
}
if ((groupDef.groupFlags & ParticleGroupType.b2_solidParticleGroup) != 0) {
computeDepthForGroup(group);
}
});
return group;
}
use of spacegraph.space2d.phys.collision.shapes.Shape in project narchy by automenta.
the class PositionSolverManifold method init.
public final void init(ContactSolverDef def) {
// System.out.println("Initializing contact solver");
m_step = def.step;
m_count = def.count;
if (m_positionConstraints.length < m_count) {
ContactPositionConstraint[] old = m_positionConstraints;
m_positionConstraints = new ContactPositionConstraint[MathUtils.max(old.length * 2, m_count)];
System.arraycopy(old, 0, m_positionConstraints, 0, old.length);
for (int i = old.length; i < m_positionConstraints.length; i++) {
m_positionConstraints[i] = new ContactPositionConstraint();
}
}
if (m_velocityConstraints.length < m_count) {
ContactVelocityConstraint[] old = m_velocityConstraints;
m_velocityConstraints = new ContactVelocityConstraint[MathUtils.max(old.length * 2, m_count)];
System.arraycopy(old, 0, m_velocityConstraints, 0, old.length);
for (int i = old.length; i < m_velocityConstraints.length; i++) {
m_velocityConstraints[i] = new ContactVelocityConstraint();
}
}
m_positions = def.positions;
m_velocities = def.velocities;
m_contacts = def.contacts;
for (int i = 0; i < m_count; ++i) {
// System.out.println("contacts: " + m_count);
final Contact contact = m_contacts[i];
final Fixture fixtureA = contact.aFixture;
final Fixture fixtureB = contact.bFixture;
final Shape shapeA = fixtureA.shape();
final Shape shapeB = fixtureB.shape();
final float radiusA = shapeA.radius;
final float radiusB = shapeB.radius;
final Body2D bodyA = fixtureA.getBody();
final Body2D bodyB = fixtureB.getBody();
final Manifold manifold = contact.getManifold();
int pointCount = manifold.pointCount;
assert (pointCount > 0);
ContactVelocityConstraint vc = m_velocityConstraints[i];
vc.friction = contact.m_friction;
vc.restitution = contact.m_restitution;
vc.tangentSpeed = contact.m_tangentSpeed;
vc.indexA = bodyA.island;
vc.indexB = bodyB.island;
vc.invMassA = bodyA.m_invMass;
vc.invMassB = bodyB.m_invMass;
vc.invIA = bodyA.m_invI;
vc.invIB = bodyB.m_invI;
vc.contactIndex = i;
vc.pointCount = pointCount;
vc.K.setZero();
vc.normalMass.setZero();
ContactPositionConstraint pc = m_positionConstraints[i];
pc.indexA = bodyA.island;
pc.indexB = bodyB.island;
pc.invMassA = bodyA.m_invMass;
pc.invMassB = bodyB.m_invMass;
pc.localCenterA.set(bodyA.sweep.localCenter);
pc.localCenterB.set(bodyB.sweep.localCenter);
pc.invIA = bodyA.m_invI;
pc.invIB = bodyB.m_invI;
pc.localNormal.set(manifold.localNormal);
pc.localPoint.set(manifold.localPoint);
pc.pointCount = pointCount;
pc.radiusA = radiusA;
pc.radiusB = radiusB;
pc.type = manifold.type;
// System.out.println("contact point count: " + pointCount);
for (int j = 0; j < pointCount; j++) {
ManifoldPoint cp = manifold.points[j];
VelocityConstraintPoint vcp = vc.points[j];
if (m_step.warmStarting) {
// assert(cp.normalImpulse == 0);
// System.out.println("contact normal impulse: " + cp.normalImpulse);
vcp.normalImpulse = m_step.dtRatio * cp.normalImpulse;
vcp.tangentImpulse = m_step.dtRatio * cp.tangentImpulse;
} else {
vcp.normalImpulse = 0;
vcp.tangentImpulse = 0;
}
vcp.rA.setZero();
vcp.rB.setZero();
vcp.normalMass = 0;
vcp.tangentMass = 0;
vcp.velocityBias = 0;
pc.localPoints[j].x = cp.localPoint.x;
pc.localPoints[j].y = cp.localPoint.y;
}
}
}
Aggregations