Using Joints

Topics: Developer Forum, User Forum
Mar 24, 2011 at 6:41 AM
Edited Mar 24, 2011 at 6:56 AM

I have a simple question regarding joints. I'm attempting to create a tank in my  Question:

game and I want to simulate the tread with rotating circles along the bottom of the Tank. I know I need to create the tank body and multiple circle bodies attached by joints that I can apply a torque to.

I've looked at the examples such as the theo jansen walker but i'm having difficulty understanding how to implement joints. I've created my main body, a simple rectangle at this point, and a Body array of circles. I'm not sure which joint to use, I've attempted to use a fixedlinejoint because I want each wheel a fixed distance and angle away.

I need to know how once 2 bodies are created, a fixed angle & distance joint can be created between them.

Heres my basic code

 

class Tank
    {
        private PhysicsGameScreen screen;
        public Body Body;
        public Body[] Wheels;
        public FixedLineJoint j1;
        public float MIP = 64;

        public Tank(World world, PhysicsGameScreen _screen, Vector2 _position)
        {
            screen = _screen;
            position = _position;
            Body = BodyFactory.CreateBody(world);
            Body.Position = position / MIP;
            Wheels = new Body[4];
	    //attempting to create joint between the main body and the 1st wheel
            j1 = new FixedLineJoint(Body, Body.Position, new Vector2(11/MIP, 6/MIP);


            //wheels
            for(int i = 0; i < 4; i++)
                Wheels[i] = BodyFactory.CreateCircle(world, 11 / MIP, 10);

            //Positions along the bottom of the body
            Wheels[0].Position = Body.Position - new Vector2(11 / MIP, 6 / MIP);
            Wheels[1].Position = Body.Position - new Vector2(33 / MIP, 6 / MIP);
            Wheels[2].Position = Body.Position - new Vector2(-11 / MIP, 6 / MIP);
            Wheels[3].Position = Body.Position - new Vector2(-33 / MIP, 6 / MIP);

            //body
            Vertices vertices = new Vertices(6);
            PolygonShape poly = new PolygonShape(10);
            vertices.Add(new Vector2(-44 / MIP, 3 / MIP));
            vertices.Add(new Vector2(-44 / MIP, 0 / MIP));
            vertices.Add(new Vector2(-30/MIP, -22/MIP));
            vertices.Add(new Vector2(30/MIP, -22/MIP));
            vertices.Add(new Vector2(44 / MIP, 0 / MIP));
            vertices.Add(new Vector2(44 / MIP, 3 / MIP));
            poly.Set(vertices);
            Fixture f1 = Body.CreateFixture(poly);
        }
Mar 24, 2011 at 3:51 PM
Edited Mar 24, 2011 at 7:37 PM

I'm slowly figuring out how to use joints, I've got a number of joints to work but not the way I want them to, I keep experimenting but can't find which joint to use. I need a joint that can be a fixed distance and fixed angle relative to the main body. Which one do I use for that?

edit: i've got it mostly working

I apply a distance joint an get the start of what I'm looking to achieve, this image http://img204.imageshack.us/img204/1397/tankj.jpg shows what I've got. I just need to know how to fix it to this angle, can somebody please help me?

Mar 25, 2011 at 12:49 AM
Edited Mar 25, 2011 at 12:49 AM

Have you looked at the car sample in latest release?  It uses line joints which remain at a fixed angle and provide suspension.  You can then apply torque to each wheel to make it move.  You would then just need to put your track around these wheels some how.