Note that there are some explanatory texts on larger screens.

plurals
  1. POBox2DWeb - Adding and Removing Body in the world
    primarykey
    data
    text
    <p>I have the following code in Box2D, I want to move my dynamic body to the point where the mouse is click. Also in the future i want to be able to remove the dynamic body, I am not able to find anything equivalent to world.DeleteBody(...). Please Help.</p> <pre><code>var world; var b2Vec2 = Box2D.Common.Math.b2Vec2 , b2BodyDef = Box2D.Dynamics.b2BodyDef , b2Body = Box2D.Dynamics.b2Body , b2FixtureDef = Box2D.Dynamics.b2FixtureDef , b2Fixture = Box2D.Dynamics.b2Fixture , b2World = Box2D.Dynamics.b2World , b2MassData = Box2D.Collision.Shapes.b2MassData , b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape , b2CircleShape = Box2D.Collision.Shapes.b2CircleShape , b2DebugDraw = Box2D.Dynamics.b2DebugDraw ; var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); var mouseX, mouseY, isMouseDown; var bBallBody, bBallbodyDef; function init() { world = new b2World( new b2Vec2(0, 10) //gravity , true //allow sleep ); setupWorld() ; //setup debug draw var debugDraw = new b2DebugDraw(); debugDraw.SetSprite(document.getElementById("canvas").getContext("2d")); debugDraw.SetDrawScale(1.0); debugDraw.SetFillAlpha(0.3); debugDraw.SetLineThickness(1.0); debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit); world.SetDebugDraw(debugDraw); window.setInterval(update, 1000 / 60); }; function setupWorld() { setupGround(); addBouncingBall(); } function setupGround() { var fixDef = new b2FixtureDef; fixDef.density = 1.0; fixDef.friction = 0.5; fixDef.restitution = 0.2; var bodyDef = new b2BodyDef; //create ground bodyDef.type = b2Body.b2_staticBody; bodyDef.position.x = 300; bodyDef.position.y = 400; fixDef.shape = new b2PolygonShape; fixDef.shape.SetAsBox(290, 10); world.CreateBody(bodyDef).CreateFixture(fixDef); } function addBouncingBall() { var fixDef = new b2FixtureDef; fixDef.density = 1.0; fixDef.friction = 1.0; fixDef.restitution = 0.1; var bBallbodyDef = new b2BodyDef; bBallbodyDef.type = b2Body.b2_dynamicBody; fixDef.shape = new b2CircleShape(Math.random() + 30); bBallbodyDef.position.x = Math.random() * 300; bBallbodyDef.position.y = Math.random() * 300; bBallBody = world.CreateBody(bBallbodyDef).CreateFixture(fixDef); console.log(bBallBody.m_body.GetPosition().x); }; document.addEventListener("mousedown", handleMouseMove, true); function handleMouseMove(e) { isMouseDown = true; mouseX = e.clientX; mouseY = e.clientY; }; function update() { if(isMouseDown) { for (b = world.GetBodyList() ; b; b = b.GetNext()) { if (b.GetType() == b2Body.b2_dynamicBody) { console.log(b.x, b.y); b.x = 100; b.y = 100; } } isMouseDown = false; } world.Step(1 / 60, 10, 10); world.DrawDebugData(); world.ClearForces(); }; </code></pre> <p>Update:</p> <p>Deletion of an object from the world is done as follows,</p> <ol> <li><p>Create a Timer to Schdule the deletion of the object.</p> <pre><code> window.setInterval(removeObjScheduledForRemoval, 1000/90); </code></pre></li> <li><p>Collect the objects to be deleted in a Array.</p> <pre><code> var bricksScheduledForRemoval = Array(); var index = -1; function removeObjScheduledForRemoval() { for(var i = 0; i &lt;= index; i++){ world.DestroyBody(bricksScheduledForRemoval[i]); bricksScheduledForRemoval[i] = null; } bricksScheduledForRemoval = Array(); index = -1; } </code></pre></li> </ol> <p>The complete code is available here, <a href="http://box2dinabox.blogspot.in/2012/07/the-completed-bananamonkey-game.html" rel="nofollow">http://box2dinabox.blogspot.in/2012/07/the-completed-bananamonkey-game.html</a></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.
 

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