understanding mouseJoints

Discuss issues specific to miscellaneous ports of Box2D
Posts: 9
Joined: Sat Apr 18, 2009 9:11 am

understanding mouseJoints

Postby kirkjerk » Fri Sep 18, 2015 11:00 am

Hi there!

I'm working in p5, and using Daniel Shiffman's "Nature of Code" examples - https://github.com/shiffman/The-Nature- ... 2d-html5-- great stuff.

I'm having trouble wrapping my head around setting the target for a mouseJoin -specifically, I always want to attach it to the center of the body, not do a test to make sure the mouse is over the body.

The code is

Code: Select all

  this.makeMouseJoint = function(){
    var mouseJointDef = new box2d.b2MouseJointDef();
    // Body A is just a fake ground body for simplicity (there isn't anything at the mouse)
    mouseJointDef.bodyA = world.CreateBody(new box2d.b2BodyDef()); //world.GetGroundBody();
    mouseJointDef.bodyB = this.body;
    var mp = scaleToWorld(mouseX,mouseY);
    mouseJointDef.target = mp;
    mouseJointDef.maxForce = 100.0 * this.body.m_mass;
    mouseJointDef.frequencyHz = 5.0;
    mouseJointDef.dampingRatio = 0.9;
    this.mouseJoint = world.CreateJoint(mouseJointDef);   

where this.body is the thing I'm trying to attach to the mouse, and then on update,

Code: Select all

if(!this.mouseJoint) return
    var mouseWorld = scaleToWorld(mouseX,mouseY);

The thing it's always relative to where i first click... is it as easy as changing the initial target to be the .x and .y of the body, or what?

Posts: 9
Joined: Sat Apr 18, 2009 9:11 am

Re: understanding mouseJoints

Postby kirkjerk » Wed Sep 30, 2015 11:06 am

Answer: why yes!

Code: Select all

       //var mp = scaleToWorld(mouseX,mouseY);
   // mouseJointDef.target = mp;
   mouseJointDef.target = this.body.GetPosition();

I still don't quite get the meaning of "target" or whether I should make bodyA a fakey body def or world.GetGroundBody(), but my core issue was getting the "target" location to be centered on the body.

Return to “Miscellaneous”

Who is online

Users browsing this forum: No registered users and 2 guests