liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.26k stars 1.95k forks source link

Body: Gravity Scaling #503

Open TheColorRed opened 6 years ago

TheColorRed commented 6 years ago

Is there a way to set the gravity scale?

If not, I would like to suggest something like:

I am no expert on the math, but wouldn't adding * gravityScale to these lines in the update be the solution? https://github.com/liabru/matter-js/blob/63b02cfbeebe93d28082504f5e89e93f98739743/src/body/Body.js#L581-L583

TheColorRed commented 6 years ago

I have added the code for this, and it seems to work the way I was thinking.

Here are the new sources:

https://github.com/TheColorRed/matter-js/blob/master/src/body/Body.js#L51 https://github.com/TheColorRed/matter-js/blob/master/src/body/Body.js#L592-L594 https://github.com/TheColorRed/matter-js/blob/master/src/body/Body.js#L433-L441

gravity scale

The box with a gravityScale: 0 is still affected by other bodies, as seen in this screen capture:

gravity scale 1

Here are two boxes that have different gravity scales that collide with one another (the x offset of each are slightly different otherwise the one with more gravityScale will push the other one down/up with it):

gravity scale 2

liabru commented 6 years ago

Interesting idea, but I'd prefer to avoid calling this gravity and instead generalise it more. I think it may be best implemented as a plugin and potentially tied into #498 and the concepts in the manipulation examples somehow. Maybe a matter-kinematic plugin would be a neat, I'll think about it.

TheColorRed commented 6 years ago

I used the term gravityScale because I was basing this off of Unity3d's Rigidbody2d, which uses that name/term and applies the same effect. Also in Unity3d, only dynamic bodies have gravity scaling whereas Kinematic/Static bodies don't.

off-topic (kinda): I think that it could be a good idea to remove body.isStatic and replace it with an enum, like body.type and accepts one of the following:

kindid commented 6 years ago

Did anything come of this? I'm interested because I'm tying to implement a "float" animation state for a character. She jumps up, under normal gravitational waves but when she reaches her zenith she opens a "parachute" (actually an umbrella!) and floats down. Now, air friction seems to apply in both dimensions and this I do not want - I want her to start floating along the x axis. The only way I have affected this so far is by changing gravity - naturally this works but also affects all other bodies in the world.

I may well just borrow your code and fork a copy of matter for my own use.

liabru commented 6 years ago

@kuiash I suggest just disabling the built in air friction and gravity and then applying your own forces as needed.

kindid commented 6 years ago

I've added in the gravity scale and it's doing just what I want it too.