Quick Gravity Question

Hi all,

Is there a way to apply gravity to one object but “reverse gravity” to another object? For example, bricks that fall and balloons that rise, all in the same screen?

I’m trying to create an environment in which many brick-type objects are working against many balloon-type objects. The idea is that the physics environment takes care of most of their motion.

It seems this should be fairly simple, but I have not found the answer.

Thank you in advance for any help you can provide. [import]uid: 70259 topic_id: 27521 reply_id: 327521[/import]

I believe that gravity is applied to the world, not to objects in the world. I wish it were the other way. There are some games that would seriouslly benefit from per-object gravity or at least make kinematic objects respond to collisions.

[import]uid: 19626 topic_id: 27521 reply_id: 111843[/import]

Just to throw an idea in, don’t know if it is possible as I’m only guessing here.

If an object has a density, friction, mass or some other parameter that affects how fast/slow gravity affects it can that not be set to maybe a minus figure to make it go the other way?

Like I say, I have no idea about the physics as I haven’t looked into them yet but just thought I’d throw that in as it may spark someone else of on an idea. [import]uid: 37683 topic_id: 27521 reply_id: 112030[/import]

@robmiracle is correct. The gravity is applied to the environment as a whole.

The are two ways really that I can think of achieving this however. The first is to set the gravity and have the bricks be a dynamic physics body and therefore subjected to it. For the balloons rising, don’t use gravity at all but just move them with a Runtime enterFrame listener or a transition.to etc.

The second way would be to calculate it mathematically and apply forces to certain objects to simulate negative gravity (rising) on them. I created a tutorial a while back on simulating water buoyancy that uses exactly this technique for the effect. The blocks fall by gravity and rise to the surface when they are submerged in the water.

Take a look at it here if you want:

TUTORIAL: Water Buoyancy In Corona SDK
[import]uid: 74503 topic_id: 27521 reply_id: 112032[/import]

What I ended up doing for a flying game I did was to set the world gravity to 0,0, so that none of my dynamic objects would fall. Then I would use applyLinearForce() or 9.8 (which should be normal gravity) in a downward push to simulate things that should fall.

You have to be careful because setting somethings to zero might not make sense. After all, things like zero density is a vaccum and negative density doesn’t exist. I’m not sure what state the physics engine would get into if you use unrealistic values.

[import]uid: 19626 topic_id: 27521 reply_id: 112055[/import]

Gravity is global, so you are stuck there (been there done that).

As rob pointed out go 0,0 on x,y gravity and use force. It’s going to make your code more complicated, but that’s the price to pay.

This was the 3rd or 4th question I asked when first starting with Corona. Guess we all had that question at one time or another :slight_smile:
ng [import]uid: 61600 topic_id: 27521 reply_id: 112061[/import]