I am having an issue where I cannot find out how to weld vertices of two polygonial bodies together while also snapping the edges between each object's vertices together resulting in something like this:
Now, I have no real idea how to even get the vertice positions of an active body, so I've tried doing this using one weld joint and creating an anchor at the position of one of the bodies (The one that is being controlled, which is the triangle). I have also not been able to snap the edges together using this method:
The code to achieve the above:
/* Check for new collisions */
Array<Contact> contactList = engine.getPhysicsWorld().getContactList();
/* Iterate through the list of contact events in world */
for (int i = 0; i < contactList.size; i++) {
Contact contact = contactList.get(i);
if (contact.isTouching()) {
boolean sensorCollided = false;
Fixture otherFixture = null;
int sensorVertexNumber = 0;
/* Iterate through the list of vertices for the current polygon object's body */
for (int j = 1; j < polygon.getVerticeCount(); j++) {
sensorCollided = contact.getFixtureA().equals(polygon.body.getFixtureList().get(j));
if (sensorCollided) {
otherFixture = contact.getFixtureB();
sensorVertexNumber = j;
break;
}
sensorCollided = contact.getFixtureB().equals(polygon.body.getFixtureList().get(j));
if (sensorCollided) {
otherFixture = contact.getFixtureA();
sensorVertexNumber = j;
break;
}
}
/* If the colliding fixtures are sensors */
if (otherFixture != null && otherFixture.isSensor() &&
polygon.body.getFixtureList().get(sensorVertexNumber).isSensor())
{
Entity collision = engine.bodyOwnerLookupTable.get(otherFixture.getBody());
Polygon colPolygon = GameEngine.polygonMapper.get(collision);
/* Check if already collided with one sensor or not, we want to collide with two to merge */
if (colPolygon.sensorCollision == null ||
!(colPolygon.sensorCollision.equals(polygon.body.getFixtureList().get(sensorVertexNumber))))
{
/* If not collided with a sensor, set the first sensor collision to this sensor */
if (colPolygon.sensorCollision == null) {
colPolygon.sensorCollision = polygon.body.getFixtureList().get(sensorVertexNumber);
}
/* If this is the second sensor colliding, merge the two bodies together */
else {
/*int oldSensorVertexNumber = 0;
for (int j = 1; j < polygon.getVerticeCount(); j++) {
if (colPolygon.sensorCollision.equals(polygon.body.getFixtureList().get(j)))
oldSensorVertexNumber = j;
}*/
colPolygon.isControlled = true;
colPolygon.setParent(polygon);
polygon.addChild(colPolygon);
/* Create weld joint */
WeldJointDef weldJointDef = new WeldJointDef();
weldJointDef.initialize(polygon.body, colPolygon.body, polygon.body.getPosition());
weldJointDef.collideConnected = false;
engine.getPhysicsWorld().createJoint(weldJointDef);
}
}
}
}
}
If someone knows how to achieve this behaviour, it would be very appreciated if you would let me know how to do it. Thanks.