How do I make a matter body isStatic false after pressing a key in matter.js?

878 views Asked by At

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?

2

There are 2 answers

0
Sri Sayee Kathiravan On

try this

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

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

0
gfdb On

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