schteppe / p2.js

JavaScript 2D physics library
Other
2.63k stars 329 forks source link

how to make a Kinematic type body to DYNAMIC type #295

Closed jimmyyao88 closed 7 years ago

jimmyyao88 commented 7 years ago

Hi Stefan ,Here is a Kinematic body move from left to right , i control another body to jump on that body , weirdly, the kinematic body go to the top .... what i want is the body back to DYNAMIC and down to the ground.... world.on("beginContact",function(evt){ if(evt.bodyA === platformBody && evt.bodyB === boxBody ) { platformBody.type = p2.Body.DYNAMIC; }; })

bug

jimmyyao88 commented 7 years ago

may-07-2017 21-55-48

jimmyyao88 commented 7 years ago

@schteppe

schteppe commented 7 years ago

You probably have to set its mass and update the mass properties too. See https://github.com/schteppe/p2.js/wiki#mass-properties

If that doesn't work, you could also create a new body.

jimmyyao88 commented 7 years ago

@schteppe ,hi Stefan,appreciate ur reply, I edit the example demo:'Canvas: Character demo' in readme,when the Character box jump and touch the platform body,I hope the platform body will drop down beacuse of the gravity,Here is the code: world.on('beginContact', function (evt){ ..code... for(var i=0;i<platforms.length;i++){ //follow what u said : update the body's mass property platforms[i].mass = 1; platforms[i].type = p2.Body.DYNAMIC; console.log('updated') }
}); may-09-2017 13-17-18 BUT , it seems dosent work, is it means the only way to make the body have influence by World Gravity and drop down is to create a new body?

schteppe commented 7 years ago

Did you try running . updateMassProperties() too? As in the wiki?

jimmyyao88 commented 7 years ago

@schteppe AWESOME! LIFESAVER

schteppe commented 7 years ago

Cool! Updated the wiki to be more clear about changing body.type.