Skip to content

Navigation Menu

Sign in
Sign up

Low Gravity #145

Unanswered
marwie asked this question in Q&A
Mar 30, 2022 · 4 comments · 3 replies
Discussion options

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?

You must be logged in to vote

Replies: 4 comments 3 replies

Comment options

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) 
)
You must be logged in to vote
0 replies
Comment options

@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

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Comment options

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 
})
You must be logged in to vote
3 replies
Comment options

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) ?

Comment options

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.

Comment options

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #144 on March 30, 2022 18:40.

AltStyle によって変換されたページ (->オリジナル) /