Change the mass of physics impostor by clicking a button

I create a button to control the mass of a physics impostor. Also I know use ‘‘mesh.physicsImpostor.setMass()’’ to change physics impostor’s mass. I write the code, and when I click the button, the physics impostor’s mass will increase 1 kg. However, When I click the button a few times, the mass of the physics impostor increased and the physics impostor always begin to move, just like apply a force on it. I don’t want it to change its position. I am confused, don’t know which step is wrong?

Hi Ethan and other friends.

Ethan, because of the type of keypress listening you are doing (document.onkeydown)… and because of the way you are setting mass (if 0 mass, then set it 1, else 0)… that makes the sphere have mass = 1 only when a key is pressed. SO… if sphere is above floor, every keypress will move sphere downward… because scene.gravity is set … and sphere impostor mass = 1 (with keypress).

Anyway… here are some REAL buttons…

Just one button activated. Line 86 sets its click-handler to be func1, located at lines 33-36. Seems to work OK… so far.

All in all, I wanted to escape that document.onkeydown keypress-listener… cuz that is UGLY… cuz it’s too wide-scoped. canvas.onkeydown MAYBE better. That’s still DOM-level keypress-listening, though.

I think there might be some BabylonJS-level keypress listeners that are even nicer… though… some keypress listening that is “closer to our hearts”, ya know? (like maybe scene.actionManager or something we can see on the BJS “scopes”) hehe.

Like I have a clue about keypress listening. heh. That’s why I like GUI 2d buttons… best.

Anyway, there’s a fresh playground… for testing this fresh challenge… getting Ethan’s setMass workin’ better and powered-up.

We can also set scene.gravity at any time and in any direction, too. Not possible in real life, but certainly possible in a BabylonJS scene. We DO have 4 un-used buttons that we could “wire-up”. :open_mouth: PARTY!

I wonder if gyro’s work in BJS physics scenes. hmm. :wink: Kick the Sherpa! (South Park-related gyro-humor) :slight_smile:

Thank you a lot ! I know what the problem is, maybe I can modify my code to solve it.

1 Like