javascriptbox2dbox2dweb

In box2dweb how to destroy created body


I create bodies at mouse click with this pretty standard way in box2dweb:

*stage.onMouseDown = function(){
     var fixDef = new box2d.b2FixtureDef;
     fixDef.density = 1;
     fixDef.friction = 0.5;
     fixDef.restitution = 0.7;
     var bodyDef = new box2d.b2BodyDef();
     bodyDef.type = box2d.b2Body.b2_dynamicBody;
     bodyDef.position.x = mouseX /scale;
     bodyDef.position.y = mouseY /scale;
     fixDef.shape = new box2d.b2CircleShape(Math.random()*100/scale);
     world.CreateBody(bodyDef).CreateFixture(fixDef);}

I really don't know how to insert a name or an Id for my created bodies (eventually I can add a var num++ at every creation). Also, I don't know how to get back my body through the id and call the method .DestroyBody to delete it specifically.

I'm at early stages with JavaScript and Objective C so methods and docs made for Actionscript make me nuts..

Thanks in advance.

Question update:

I found a way to get back my already created object, finding the one i want among all of them using this way:

note: myBody is global

        myBody['enter'+prodNum] = bodyDef;
        bodyDef.userData = prodNum;
        myBody['enter'+prodNum].id = bodyDef.userData;

prodNum is a global var which has a "++" at every cycle. With this i can recall my body back using both the body's var name and the bodyDef.userData property.

with the following function, called in my init() that's executed through window.onload, i can, as console.log shows, change what i want of my retrieved body, however no changes are applied to the body in canvas, even if its property in log resulted modified i cannot notice any change on the screen.

function reduceObj(){

     var itsMe;

                itsMe = myBody.enter10;

        var newPosX = itsMe.position.x;

        itsMe.active = false;
        itsMe.awake = true;
        itsMe.linearVelocity.x = 2000;
        itsMe.position.x = newPosX+500;
        itsMe.fixedRotation=true;
        itsMe.allowSleep=true;


        console.log(myBody.enter10,itsMe,itsMe.id,'it s me');



 }

Cannot get why all this happens.. plus i already set the step() function which should refresh my world every x milliseconds... Help pls


Solution

  • The CreateBody function should return a reference you can keep, to destroy the body later.

    var mybody = CreateBody( bodyDef );
    mybody.CreateFixture( fixDef );
    

    You can't just set properties in a body to change it, you need to use the appropriate functions:

    // later...
    mybody.SetActive( false );
    mybody.SetAwake( true );
    
    var vel = mybody.GetLinearVelocity();
    vel.x = 2000;
    mybody.SetLinearVelocity( vel );
    
    var pos = mybody.GetPosition();
    pos.x += 500;
    mybody.SetPosition( pos );
    
    mybody.SetFixedRotation( true );
    mybody.SetSleepingAllowed( true );
    

    Please keep in mind that 500 units is half a kilometer, so that's probably not what you would want to be doing. Use meters for your dimensions, not pixels. A velocity of 2000m/s is around 7200km/h or mach 6 (as reference the fastest aircraft ever built does about mach 8, so this is also most likely not what you want). Take a look at this page for some other common gotchas: http://www.iforce2d.net/b2dtut/gotchas