Open TheColorRed opened 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
gravityScale: -2
gravityScale: -1
gravityScale: 0
gravityScale: 1
(default)gravityScale: 2
The box with a gravityScale: 0
is still affected by other bodies, as seen in this screen capture:
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):
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.
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:
Body.Dynamic
(default)Body.Static
Body.Kinematic
(when and if added)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.
@kuiash I suggest just disabling the built in air friction and gravity and then applying your own forces as needed.
I've added in the gravity scale and it's doing just what I want it too.
Is there a way to set the gravity scale?
If not, I would like to suggest something like:
gravityScale: 2
would make it have twice the gravity (falls twice as fast)gravityScale: 1
would be the default (runs as it currently does)gravityScale: 0
would make it so gravity doesn't affect the body (at least visually)gravityScale: -1
would make it have an inverse of the default gravity (like a balloon with helium)gravityScale: -2
would make it have twice the inverse of the default gravity (rises twice as fast)I am no expert on the math, but wouldn't adding
* gravityScale
to these lines in theupdate
be the solution? https://github.com/liabru/matter-js/blob/63b02cfbeebe93d28082504f5e89e93f98739743/src/body/Body.js#L581-L583