I have a QML Box2D project that I'm working on which has a number of different Polygons in the form of Box2D Dynamic Bodies with Polygon Fixtures that all move dynamically around a World.
My goal is to make it so that whenever two different bodies collide with one another, they will both join on whatever sides that they collided on initially by matching the two sides up face to face and essentially joining into a single Body but remaining as two separate bodies in order to track them using the game's logic.
It will be something like two magnets floating in space and connecting to each other, and then having the two magnetic sides attracting one another and becoming fused together in a sense.
While I have had no issues creating the Bodies, and similarly no issues with determining when collisions happen and performing various functions when they do collide, I am not able to get the two objects to simply combine on a single side nearest the collision..
Here's what I've tried to do so far, without success:
Body {
id: body
world: physicsWorld
property var vertices
bodyType: Body.Dynamic
target: gamePiece
function beginContact(other) {
if (other.getBody() !== wallBody) {
var newObject = Qt.createQmlObject('import QtQuick 2.9; import Box2D 2.0; DistanceJoint { }', body, "dynamicSnippet1");
newObject.bodyA = body;
newObject.bodyB = other.getBody();
newObject.length = 80;
DistanceJoint.collideConnected = true;
body.angularVelocity = 0;
body.fixedRotation = true;
console.log("Created Distance Joint " + newObject);
} else {
console.log("Skipping Body collision with wall");
}
}
fixtures: Polygon {
density: 2
friction: 0.9
restitution: 0.01
vertices: body.vertices
onBeginContact: { body.beginContact(other) }
}
}
Every object that collides with another Body like it will simply become pulled into the colliding Body completely, and the sides do not match up at all.
How would I determine what the sides are of the Bodies that make contact and how is the best way to connect them?