Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is kind of an old question, but I figured it out this evening:</p> <p>Box2D Jquery doesn't set any DOM accessible User Data, so we have to set some.</p> <p>In your $('element').box2d, set an option 'mouseEnabled' to true/false.</p> <p>Then, in</p> <pre><code>$.fn.extend({ box2d: function(options) { </code></pre> <p>add the options:</p> <pre><code>mouseEnabled = opts['mouseEnabled']; userData = { mouseEnabled:mouseEnabled, //Add additional options here } </code></pre> <p>Below that, find the line that says </p> <pre><code>createDOMObjects(absolute_elements, shape, static_, density, restitution, friction); </code></pre> <p>and append userData to the arguments so that it says:</p> <pre><code>createDOMObjects(absolute_elements, shape, static_, density, restitution, friction, userData); </code></pre> <p>NEXT</p> <p>Find createDOMObjects and append userData to the arguments</p> <pre><code> createDOMObjects = function(jquery_selector, shape, static_, density, restitution, friction, userData) { </code></pre> <p>You are now going to need to add userData to the b2BodyDefinitions of each body. So append userData to createBox and createCircle. Apply the userData to the BodyDef like so within the two creation functions: bodyDef.userData = userData;</p> <p>If you did it right, if you logged out the body on getBodyAtMouse(); you would see m_userData is no longer null!</p> <p>LASTLY In updateMouseDrag, add a return is the userData for mouseEnabled is false!</p> <pre><code>if (body) { console.log(body.m_userData) if(body.m_userData.mouseEnabled == false){return;} md = new b2MouseJointDef(); md.bodyA = world.GetGroundBody(); md.bodyB = body; md.target.Set(mouseX, mouseY); md.collideConnected = true; md.maxForce = 300.0 * body.GetMass(); mouseJoint = world.CreateJoint(md); body.SetAwake(true); } </code></pre> <p>VOILA! Now, wherever you insantiate your box2D elements, just add mouseEnabled to it!</p> <pre><code>$(".block").box2d({'y-velocity':10, 'mouseEnabled':false}); </code></pre> <p>If you want to change the mouseEnable during runTime, just set the body.m_userData.mouseEnabled to anything!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload