Multiple Joint questions

Hey guys, kind of embarrassed this is going to be my first post as its all questions 🙂 but I appreciate any help ahead of time as I'm running out of time.

I'm creating a light gun, basically like a flashlight with box2d collision. I currently have a triangle connected to a mouse joint that follows the mouse constantly; however, it rotates sort of randomly because the joints are both in the middle of the triangle, is there a way to stop rotation or at least prevent this from happening. I believe fixed rotation is the command?

On top of this my lightGun needs to only rotate in front of the player, so it must follow the mouseJoint on a limited angle. Is it possible to limit the rotation of a mouse joint?

here is my current code.


I apologize for "the_circle" I swapped from a circle to a triangle recently.

Also alpha code 😉
Code:
                        var bd:b2BodyDef;
         var the_circle:b2PolygonDef = new b2PolygonDef();
         var the_joint:b2DistanceJointDef = new b2DistanceJointDef();
         
         //Triangle Code required for implementation
         
         the_circle.vertexCount = 3;
         the_circle.vertices[0].Set(0, -2);
         the_circle.vertices[1].Set(2, 2);
         the_circle.vertices[2].Set(-2, 2);
         the_circle.density = 0.01;
         the_circle.friction = 0.4;
         the_circle.restitution = 0.3;
         bd = new b2BodyDef();
            
         the_circle.isSensor = true;
         bd.position.Set(halfWidth +4, halfHeight * 20);
         use fixedRotation = true;
         
         var circle:b2Body = world.boxWorld.CreateBody(bd);
         var gun:CitrusShape = new CitrusShape(circle.CreateShape(the_circle), ShapeNames.LIGHT_GUN);
         
         circle.SetMassFromShapes();
         
         the_joint.Initialize(circle, topBodyShape.body, gun.body.GetPosition(), topBodyShape.body.GetPosition());
         
         the_joint.collideConnected = true;
         
         var joint:b2DistanceJoint = world.boxWorld.CreateJoint(the_joint)as b2DistanceJoint;
         var body = circle;
            var mouseJointDef:b2MouseJointDef = new b2MouseJointDef;
         mouseJointDef.body1 = world.boxWorld.GetGroundBody();
         mouseJointDef.body2 = body;
         
         mouseJointDef.target.Set(circle.GetPosition().x, circle.GetPosition().y);
         mouseJointDef.maxForce = 75;
         mouseJointDef.timeStep = m_timeStep;
         mouseJoint=world.boxWorld.CreateJoint(mouseJointDef) as b2MouseJoint;
         _lightCircle = gun;



thanks to ANYONE who can help, thanks,

Mike
Hey

Firstly: to fix rotation of a body, just set
Code:
bodyDef.fixedRotation = true;


Secondly, to me it sounds like it's not the angle of the mouseJoint that you want to limit, but the angle of the RevoluteJoint between the "player" and the "light cone".

So, my advice: Attach the mouseJoint to the "fat end" of the light cone (the end furthest away from the player), create a player body (a circle or something). Then connect the player and light cone by a RevoluteJoint, and finally: limit the angle of that RevoluteJoint.

I'm not sure if this will work though, because if you move the mouse too far away from the end of the cone, an enormous force will be applied, but the RevoluteJoint will still be holding the cone in place. I don't know how your game works, so I don't know how big of a problem that will be.
Thanks for your help that makes sense.

Just a question about this force, I don't think it should matter because the actual cone will be a sensor, and I can't see the force getting too large at the window size is only 768 in width.

Heres to hoping 😉

Original thread: https://box2d.org/forum/viewtopic.php?t=2945