React + Cannon how to move an object in one direction without being affected by gravity?


With this example (React + Three + Cannon), I’d like my yellow cube to move only on the X axis without being affected by gravity. However hard I try to compensate for gravity by assigning an opposite force, my cube either rises or falls. The adjustment is extremely fine and I don’t think this is the right solution. The cube starts at position 3 on the Y axis like the instances, but after a certain delay, they fall.

For my instances, I’d like the same principle, except that the movement must be on the Z axis only.

Thank you for your help…

That sounds hard. I would just reset the y position to what I want it to each frame?
Otherwise its literally like trying to fly a helicopter with an algorithm. The physics simulation is running multiple substeps per frame and you’re trying to adjust it one whatever the useFrame interval is, so you’re not going to intercept every force applied to it during the simulation.
Other options might be to set the friction to 0 and let them slide on the ground, or on an invisible box…

Maybe mass = 0? You can set the mass depending how you want the object to interact with gravity.

Mass = 0 is usually a signal to the physics engine that the object is static and it will stop applying forces to it.

Hi, thanks for your advices.

I implement with a support under the Cube and it works but my problem now is how to filter collision between Support and Instances… (At line 54)

"if myObject == something then doSomething "

I try with the mass properties but i don’t see it on the event collide…


Yes, but it’s more difficult, i don’t want to filter the collision in this way.

My Cube has to be on the support. So it always collides with the support. What I want is to recognize the Instance on collision to make the Support disappear and have my Cube fall.

You probably have to research how to set up a collision callback in cannon.

I’m most familiar with ammo.js which gives you a list of “contact pairs” on each simulation substep… which you can loop through and look for a contact between your 2 objects.

I don’t know how it works in cannon tho…