Hi, I want to join dynamic bodies with WeldJoint when they collide with each other.
I managed to do that with the use of ContactListener. I take the bodies from PreSolve (I don't want the impact to influence the bodies) and Weld them right after world.step.
Joint is created, but I cannot get them to be welded in the right position (the one they connected in).
I tried all the logical formulas I could think of, and some dumb ones as well. getLocalPoints, getWorldPoints and even something with manifold, but I couldn't achieve the right outcome.
Help?
relevant part of the code
ContactListener:
Play screen:
I managed to do that with the use of ContactListener. I take the bodies from PreSolve (I don't want the impact to influence the bodies) and Weld them right after world.step.
Joint is created, but I cannot get them to be welded in the right position (the one they connected in).
I tried all the logical formulas I could think of, and some dumb ones as well. getLocalPoints, getWorldPoints and even something with manifold, but I couldn't achieve the right outcome.
Help?
relevant part of the code
ContactListener:
Code: Select all
private Array<Body> weldsToMake;
private Array<Vector2> positionWeld;
public void preSolve(Contact c, Manifold m) {
Fixture fa = c.getFixtureA();
Fixture fb = c.getFixtureB();
if(fa.getUserData() != null
&& fa.getUserData().equals("animal")
&& fb.getUserData().equals("platform")) {
weldsToMake.add(fb.getBody()); //platform
weldsToMake.add(fa.getBody()); //animal
}
if(fb.getUserData() != null && fa.getUserData() != null
&& fb.getUserData().equals("animal")
&& fa.getUserData().equals("platform")) {
weldsToMake.add(fa.getBody()); //platform
weldsToMake.add(fb.getBody()); //animal
}
}
Play screen:
Code: Select all
public void WeldAnimals() {
//weld box to scale
for(int i = 1; i < cl.getWelds().size / 2; i++){
WeldJointDef jdef = new WeldJointDef();
jdef.collideConnected = false;
Vector2 worldAnchorPoint = cl.getWelds().peek().getWorldPoint(new Vector2(0, -1));
jdef.bodyA = cl.getWelds().pop();
jdef.bodyB = cl.getWelds().pop();
jdef.localAnchorA.set(jdef.bodyA.getLocalPoint(worldAnchorPoint));
jdef.localAnchorB.set(jdef.bodyB.getLocalPoint(worldAnchorPoint));
world.createJoint(jdef);
}
}