1

I was working on a project where I found it difficult to figure out how to make a Matter Body in matter.js isStatic false when a key is pressed.

if (keyIsPressed(UP_ARROW)) {
  this.body.isStatic(false)
}

Can you tell what is the correct syntax to make a Matter body isStatic false when a key is pressed?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 1
    [Relevant GitHub issue](https://github.com/liabru/matter-js/issues/187). Try [`Matter.Body.setStatic(body, isStatic)`](https://brm.io/matter-js/docs/classes/Body.html#method_setStatic). – ggorlen Dec 15 '20 at 17:44

2 Answers2

1

Static bodies in MatterJS have infinite mass. Long story short, if you want to change a body from isStatic=true to isStatic=false, you have to also set the mass.

Matter.Body.setMass(myBody, 0.001 * (myBody.width * myBody.height))
Matter.Body.setStatic(myBody, false)

0.001 is the default body density. We multiply that by the area to get the mass.

reference: MatterJS GitHub Question

gfdb
  • 311
  • 2
  • 9
-1

try this

function keyPressed(){
    if (keyCode === 38){
     isStatic : false}
}

but before pls check u have matter.js and p5.js