mergine many shapes together

Topics: Developer Forum
Nov 27, 2012 at 2:41 AM
Edited Nov 27, 2012 at 2:49 AM

Hi All,

I am new to Farseer ,  I have a game to do with .

in the game I have a weapon that merges the game objects together(that contacts each other) , as instance , so when I hit two boxes they should merge and act as one shape .

I tried doing that with two ways , first adding fixtures and second my adding weld joint , it works fine when I try to merge two shapes , but when I have 3 objects , like 3 boxes when I merge them , their position change , they form another shape

here's the code am using for the weld joints :

 

         for (int i = 0; i < objects.Count - 1; i++)
         {
               Body body1 = gameObjects[indices[(i * 2) ]].physicsBody;
               Body body2 = gameObjects[indices[(i * 2) + 1]].physicsBody;
                
                Vector2 dist= body1.Position - body2.Position;

                WeldJoint weldjoint = new WeldJoint(body1, body2,
            Vector2.Zero,dist);

                    world.AddJoint(weldjoint);
          }

 

the indices are an integer list for the objects contacting each other ,
containing number like 0,1,1,2 , so a weld joint must be created between objects with index 0 and 1 , and weld joint between the object with index 1 and 2

 

the problem :

 

before I merge them :

 

http://s7.postimage.org/tbmqxncnv/before.png

after I merged them


http://s7.postimage.org/q2cx4xdy3/after.png 

 

the box at the bottom just rotates and looks like in the pic

Coordinator
Nov 28, 2012 at 2:26 AM

A weld joint is a soft constraint and might cause trouble when forces are applied to the shapes it connects, or when they have too large a mass. I think your problem stems from the fact that you apply the joint on the center of the firstbody, to an offset (dist) of the other body.

Another solution you can do, is to use multiple fixtures on a single body. This is a lot more stable as multiple shapes will move as one. Simply copy out the shapes from the 2 bodies, remove the two bodies, and create a new body that contains the 2 shapes from before. Take a look at the SimpleSample2 from the XNA Samples for more information about how to do this.