-
Notifications
You must be signed in to change notification settings - Fork 154
Hello,
is it possible to set a body to zero gravity (e.g. to behave like in outer space) without having to change the gravity of the whole physics world?
All reactions
Replies: 4 comments 3 replies
I'm curious about this as well. A simple fix would be to run applyForce in the animate function.
body.applyForce(
new CANNON.Vec3(0, 10, 0), // force (not gravity)
new CANNON.Vec3(0, 0, 0)
)
All reactions
@SuboptimalEng you mean to counter gravity?
Id rather not, if there is no option yet maybe it can be added as it should be rather simple
All reactions
Just jumped into the code to see what you meant. If I understand it correctly, a better option could be to add body.type like ZERO_GRAVITY and update the internalStep function to handle it.
https://github.com/pmndrs/cannon-es/blob/master/src/world/World.ts#L539-L551
I'm super new to this btw. Started learning Cannon.js + game dev physics two days ago.
All reactions
Hey! To make only an object appear gravitless, you have to apply an equal and opposite force to the object before each step.
const world = new CANNON.World() world.gravity.set(0, -9.81, 0) const body = ... world.addEventListener('preStep', () => { body.applyForce(new CANNON.Vec3(0, 9.81, 0)) // opposite of gravity })
All reactions
-
👍 1
what do you think about instead having to loop through a set of bodies marked as using gravity just skipping them in core (via some flag as @SuboptimalEng suggested) ?
All reactions
It is not recommended as body types are only three in phisics engines. You can patch it yourself but cannon-es won't add support for it.
All reactions
I see, thanks (just asking because Unity has the option and we are exporting to threejs so im looking into how to translate that setting)