# [solved] Change gravity for a certain space/area

## Recommended Posts

Hi @all again,

is there a way to change the gravity for certain space?

Case 01: I want for example that the player or other objects can fly to space. So if specific distance has been reached from the ground, the gravity should decrease with rising distance, until null.

Case 02: If the player or other objects goes into a hidden area/space/box, the gravity for all objects in there is different to the world gravity (scene.gravity).

Thanks for ideas.

##### Share on other sites

Hmm.. that sounds fun. Maybe something like applying a force or impulse based on the y value of the object in the opposite direction of the gravity to nullify it: http://www.babylonjs-playground.com/#BEFOO#133 I know, doesn't work that well yet, but you get the idea. Not sure how exactly gravity is applied... would have to find that in the babylon code or just disable it and do your own gravity

##### Share on other sites

Are you using one of the physics plugins?

If yes, I wonder if it wouldn't be possible to simply change the gravity of the world object, whenever the condition is met. This of course wouldn't work for multiplayer.

##### Share on other sites

Yeah, not sure what the current default physics plugin is cannon or oimo. Well, if you have only one object that you want to apply gravity to, then you can change the worlds gravity I guess... if you have something like..lets say a rocket that flys to space and half the way you detach the first primary thrusters... then the thrusters should fall back to the planet with the the gravity at that certain point but the rest of the rocket keeps going and reaches the lower outer space gravity... sooo..depends on how complex you want it I guess

##### Share on other sites

I use cannon.js.

I want to make two rooms. In one there is the gravity for all objects -9,81 and in the second room and in the other one it is +9,81. Just an example.

Another example is a simulation: The player starts to fly from a planet (planet gravity) with a spaceship to space (no gravity).

##### Share on other sites

In that case you could always play with impulses, although that might get a little complex.

I wonder if you could create multiple worlds, and whether or not it would destroy performance.

``````var earthWorld = new CANNON.World();

var spaceWorld = new CANNON.World();

earthWorld.gravity.set(0,-10,0);

spaceWorld.gravity.set(0,10,0);

var body = new CANNON.Body(shapes, sizes and shit);

(renderloop{

earthWorld.step(60/1);

spaceWorld.step(60/1);

if(calculateDistance(body)){

earthWorld.remove(body);

};

)};

function calculateDistance(cannonBody){

//If distance from earth is bigger than x ->

return true;
};``````

I have No idea whether this would work, though.

Same thing with rooms.

Edit: Whoops. Don't step it 60/1

##### Share on other sites

• 4 weeks later...

So I am back in this topic. Sorry for my delay.

I prepared one nice Demo: http://playground.babylonjs.com/#XMXU7

You can move the player with arrow keys AND you can deactivate/reactiveate the world gravity with key K. (The green space got a positive gravity  Y: + 2 * 9,81. The red space got a negativ gravity Y: - 9.81.)

This works like i want, BUT the Problem is: If you deactivate the world gravity (key k) and move with the player in the red box (with gravity y:-9,81), the player got no friction and slips like ice. The player sould walk normal in the red box with world gravity off.

##### Share on other sites

I'm not certain why you might want to change the Workd Scale gravity dynamically, but this is certainly something you can accomplish; and I find this action more controlable using Oimo instead of Cannon - but this is based upon many experiments taking all settings in each physics extension into consideration. But if I was personally attempting to alter the affect of gravity using either Oimo or Cannon as the physics engine, I would change the mass of my objects using a variable to pass to each meshes' physics imposter; which would allow a single change in the variable to affect as many objects as required at the time. This also allows you to set the behavior of each mesh or group of meshes to behave very specificaly - as opposed to to achieving the desired behavior using a single global setting - which is far more difficult to achieve the desired result from the physics simulation in detail. So it is far more flexible and simpler to change the imposter's mass over time and/or to make a single adjustment to each imposter's mass - however, make certain you utilize mesh.physicsImpostor.forceUpdate() to apply the change in mass to each object's physics imposter.

There are other methods you can apply, such as scene.gravity = new BABYLON.Vector3(X, Y, Z); but I personally find that changing the mass for your physics imposters provide far more flexability and more specific control over the objects in your scene.

DB

##### Share on other sites

@dbawel You are right. Changing mass is easy. I've already thought about that. Problems here are: It is not possible to set negative mass on physicsImposter. Also mass dont work for a gravity space like: (-10,0,0), respectly gravity in X- or Y-Axis direction.

##### Share on other sites

2 minutes ago, Hans said:

You are right. Changing mass is easy. I've already thought about that. Problems here are: It is not possible to set negative mass on physicsImposter. Also mass dont work for a gravity space like: (-10,0,0), respectly gravity in X- or Y-Axis direction.

So there are many solutions to your what might be percieved s a problem, but these can all be overcome. And I've learned from working on projects with @Pryme8 that it is better to set your own object's movement, velocity, etc. - or change in position - and to avoid using impulses altogether in your physics simultations. However, both of these methods to translate your mesh through world space are valid - seperately and together. It simply takes a little experimentation to create the desired movement for your specific scene or instance. However, in my experience, I now find far more control in avoiding the use of impulses, and to conditionally set the change in position, acceleration, and velocity for any mesh in a physics simultation using my choice of physics engine; maintaining every other aspect of the physics simulation - as this allows me far more flexability in any simulation and scene. And I personally find cannon.js to be easier to use in these circumstances. However, Oimo.js is great, but a bit more complex to navigate in such conditions.

DB

##### Share on other sites

Hi @Hans, two things:

1. For per-body gravity, I think you're doing things the right way - just set a global gravity value, and for any body that needs a different amount of gravity, apply an additional force each tick. However, you should probably just use "addForce", rather than zeroing out or directly manipulating the values of the body's "force" property. This way the gravitational force will play nicely with any other forces occurring (e.g. the force you use to move the player around).

I'd suggest not changing the body's mass to change its reaction to gravity, since mass affects lots of other things in the simulation.

2. For your problem at hand:

5 hours ago, Hans said:

This works like i want, BUT the Problem is: If you deactivate the world gravity (key k) and move with the player in the red box (with gravity y:-9,81), the player got no friction and slips like ice. The player sould walk normal in the red box with world gravity off.

Similarly to that other thread, the behavior you're seeing is physically correct - without gravity the body is floating freely, and once he starts moving sideways there's nothing to stop him. So the question to answer is, what kind of non-physical behavior you want? You could stop the player by increasing his linearDamping, but that would affect vertical motion as well so it may not be what you want. Another option would be, when no movement key is pressed, apply a movement force in a direction opposite to the player's velocity.

Which is best probably depends on what's going on thematically in your game. E.g. if the player's gravity is off because he's floating in liquid, then linearDamping would imply he's slowing down because if the liquid's viscosity. If he's a space guy moving around in space, then he probably moves himself with thruster rockets or something, and by implication he probably stops himself by firing them in the opposite direction, so it will make sense for the physics to simulate that.

##### Share on other sites

8 hours ago, fenomas said:

Similarly to that other thread, the behavior you're seeing is physically correct - without gravity the body is floating freely, and once he starts moving sideways there's nothing to stop him

Yes this is right, but if the player is in the read space, there is a gravity and the friction have to stop the player as normal or? But of course the world gravity is off. There is still gravity in the read box as normal. Its like world gravity in the read box, i thougth. Why i have to regulate the friction behavior by myself with linearDamping/counterforce? I want to understand this

Btw. you suppose right, my theme is outer space. And I want to simulate a ship with his own gravity and the player should stand like normal one the ship ground/platform, no matter how the ship is moving or rotating in a world of gravity or without gravity. So i want to reuse the insights of our last topic too. This is my main goal this days.

##### Share on other sites

14 minutes ago, Hans said:

Yes this is right, but if the player is in the read space, there is a gravity and the friction have to stop the player as normal or? But of course the world gravity is off. There is still gravity in the read box as normal. Its like world gravity in the read box, i thougth. Why i have to regulate the friction behavior by myself with linearDamping/counterforce? I want to understand this

Er, I'm not sure I understand the question fully, but for starters I think your code might be backwards. As written, the scene has a gravity of [0, -9.81, 0] (i.e. gravity pushes in the -Y direction), and then when the player touches the green space you apply a +Y force, and when he hits the red space you apply a -Y force. So effectively, in the green area the player experiences zero gravity, and in red he experiences stronger than normal gravity. Not sure if that's what you wanted.

Either way, note that friction can occur whenever two bodies are being pushed together - it doesn't matter whether they're pushed by gravity, or by "applyForce", or by a combination of both, etc. Right now, in the green area there's no net force pushing them together, hence no friction.

43 minutes ago, Hans said:

Btw. you suppose right, my theme is outer space. And I want to simulate a ship with his own gravity and the player should stand like normal one the ship ground/platform, no matter how the ship is moving or rotating in a world of gravity or without gravity.

It sounds like, inside the ship, you want the player to move as if he was in a static environment with normal gravity. Why not just model it that way, with the ship's floor and walls and whatever being static bodies?

##### Share on other sites

2 hours ago, fenomas said:

Er, I'm not sure I understand the question fully, but for starters I think your code might be backwards. As written, the scene has a gravity of [0, -9.81, 0] (i.e. gravity pushes in the -Y direction), and then when the player touches the green space you apply a +Y force, and when he hits the red space you apply a -Y force. So effectively, in the green area the player experiences zero gravity, and in red he experiences stronger than normal gravity. Not sure if that's what you wanted.

Not exactly: (The green space got a positive gravity  Y: + 2 * 9,81. The red space got a negativ gravity Y: - 9.81.). The green box push the player in the air. The red box pull the player harder to the ground like normal (Y: 2 * -9.81). This ist okay and deliberately.

So we deactivate the world gravity by pressing the key K: The green box pushed the player still in the air (deliberately). The red box is now Y: -9.81 and not Y: 2 * -9.81. I thougth its like normal gravity now in the red box. The player is also pulled down to the ground, but with no friction. How can I simulate the wolrd force in the red box without world gravity to have friction too?

Also a problem is: The player can not stick together with the ground like my other topic, because the collideEvent do not occure. I think this is the reason there is no friction too.

2 hours ago, fenomas said:

Either way, note that friction can occur whenever two bodies are being pushed together - it doesn't matter whether they're pushed by gravity, or by "applyForce", or by a combination of both, etc

Why? How do this the world gravity to apply force with friction?

2 hours ago, fenomas said:

It sounds like, inside the ship, you want the player to move as if he was in a static environment with normal gravity. Why not just model it that way, with the ship's floor and walls and whatever being static bodies?

Because the game sould become a local coop game. There will be two or more players at the end.

##### Share on other sites

Ahhh, I see what you're asking now.

And it looks like the answer is: https://github.com/schteppe/cannon.js/issues/224

In short, Cannon uses the length of the world gravity vector to approximate the maximum friction force, so you'll probably need to set your scene's gravity to a common-sense value, and then apply counter-forces when you want to negate it.

##### Share on other sites

I tested it finally. It works. @fenomas  Thanks again

Unfortunately the solution ist a little bit unfriendly for the performance.

If there is a better solution in the future pls contact me

##### Share on other sites

Wrap your hea around this project. Forget all impulses and do the math:

@Pryme8 was instrumental on this project.

Oh, by the way - it's touch screen only! try both one and multiple fingers...

DB

EDIT - this is made for and currently used on 90+ inch touchsceeens at trade shows daily. So the detail is much greater than you might see on your current touchscreen.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.