About attaching bodies together (character movement)

Topics: User Forum
Jan 30, 2010 at 5:36 PM
Edited Jan 30, 2010 at 6:23 PM

Hey everybody.

First of all I'd like to thank the creators of this fantastic physics library for all their hard work with the library, it's absolutely awesome.

I'm currently doing a tile based game with the engine and before I try out the "turn N amount of tiles into one big body"-algorithm I'd like to give this "attach rectangle to sphere"-technique a go, but I'm having trouble figuring out how am I supposed to effectively attach the player body (Rectangle body) to a sphere (obviously the sphere won't be rendered later on... but that's irrelevant now). Aim is to stop the player character rotation (which is easy on it's own) but how do I effectively attach my rectangle to a sphere?

And after that the player movement is basically "rolling the ball" and the character rectangle follows it, right? Adding torque(?) to it. So any help with how to attach the rectangle to the sphere would be greatly appreciated. My own tests mainly have resulted into complete explosion of the game, meaning it just blows up ... I tried basically creating a FixedAngleJoint to tell the player body to stay upright and then using a WeldJoint attached the body to the sphere but it didn't go too well...

Thank you!


EDIT # 1: Exactly like Sonic does at the end of this thread: http://farseerphysics.codeplex.com/Thread/View.aspx?ThreadId=76174

For what ever reason, I cannot seem to crack this... Cheers again.

Jan 31, 2010 at 7:04 PM

Anyone? I'm having the exact same problem but just cannot nail this. Trying to do the circle acts as an player and rolls around with a FixedAngleJoint on it. Other than that got my world setup and running the way I want, just the player hitting the "corners" of the tiles as many have mentioned on the forums. Trying to get over that with using a circle. 

Feb 1, 2010 at 3:09 AM

You need to use a RevoluteJoint to attach the rectangle to the sphere, and you also need to put the rectangle and the sphere into the same collision group so that they dont collide while stuck together.

Joint code:

RevoluteJoint joint = new RevoluteJoint(Rectangle, Circle, Circle.Position);
CollisionCategory code:
rectangle.CollisionCategories = CollisionCategory.Cat1;
circle.CollisionCategories = CollisionCategory.Cat1;
rectangle.CollidesWith = CollisionCategory.All & ~CollisionCategory.Cat1;
circle.CollidesWith = CollisionCategory.All & ~CollisionCategory.Cat1;

Hope this helps! :)

ps: A weld joint "welds" the 2 bodies together so that they can't move seperately. A revolute joint is like a bolt, the 2 bodies can rotate around it.

Feb 3, 2010 at 8:00 PM
Edited Feb 3, 2010 at 8:06 PM

thank you for the answers RobertDodd.

This is what I currently do:

            Body rectangle = BodyFactory.Instance.CreateRectangleBody(PhysicsSimulator, 64, 64, 1f);
            Body circle = BodyFactory.Instance.CreateCircleBody(PhysicsSimulator, 5, 1.0f);

            rectangle.Position = Gfx.I.getTitleAreaPosition(new Vector2(35, 35));
            circle.Position = Gfx.I.getTitleAreaPosition(new Vector2(35, 35));

            Vector2 offsetPosition = circle.Position;
            offsetPosition.Y += 5;

            RevoluteJoint joint = new RevoluteJoint(circle, rectangle, offsetPosition);

            Geom rectangleGeometry = GeomFactory.Instance.CreateCircleGeom(PhysicsSimulator, rectangle, 8, 50);
            Geom circleGeometry = GeomFactory.Instance.CreateCircleGeom(PhysicsSimulator, circle, 5, 10);

            rectangleGeometry.CollisionCategories = CollisionCategory.Cat5;
            circleGeometry.CollisionCategories = CollisionCategory.Cat5;
            rectangleGeometry.CollidesWith = CollisionCategory.All & ~CollisionCategory.Cat5;
            circleGeometry.CollidesWith = CollisionCategory.All & ~CollisionCategory.Cat5;

Which basically creates me the rectangle, circle and the joint to join them together but... but it's not quite .. uhm.. I mean, they are joined together but how do I actually make sure the rectangle stays "on top" of the circle all the time? And how do I actually "spin" the circle with a correct way? Obviously when I want to move around I want to roll the circle to make it move around smoothly but what am I supposed to add torque to it? Impulse? Or?



Feb 6, 2010 at 10:13 PM

Hello, just now Im struggling with player phisics too, and so far, the best (and only) way to stop the square from "falling" is to use a anglejoint:

FixedAngleJoint aj = new FixedAngleJoint(_rectangleBody, 0);

With this Im not allowing the rectangle to rotate, and so far, it always remain over the circle, now, Im having other issues like jumping and stuff, but Im progressing(or I like to think I am), but if you already have solved that, I would love to see your solution.