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
where this.body is the thing I'm trying to attach to the mouse, and then on update,
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?
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);
this.mouseJoint.SetTarget(mouseWorld);
return;
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?