"Balloon" or object that has a "reverse gravity" force

Hey guys I have searched the forums for answers on this but can’t come up with much.

I’m looking to create a scenario where there is global gravity and objects are affected by it. I.e. a box is dynamic physics body. The trick is that I want to have a balloon that is “floating” in the opposite direction of the gravity but also has enough “upward” force to lift the box if they were attached.

I’ve tried to use applyforce but the “balloon” seems to only move “upward” temporarily. I need it to have a constant “negative gravity”

Any help would be greatly appreciated, as I’m a LUA/OOP noob.

THANKS!! [import]uid: 10361 topic_id: 5449 reply_id: 305449[/import]

Use applyForce repeatedly in an enterFrame event listener. [import]uid: 12108 topic_id: 5449 reply_id: 18376[/import]

Duh! Yep. I’m a noob.

Thanks jhocking! Makes sense.

[import]uid: 10361 topic_id: 5449 reply_id: 18389[/import]

Hi,

I have a similar problem. I need to create a body without gravity effect. In an enterFrame event listener I use applyForce, but a body just flies up. Here is my main.lua file.

[lua]local physics = require(“physics”);
physics.start();
gx, gy = physics.getGravity();

function update(e)
for i=1,bullets.numChildren do
local bullet = bullets[i];
bullet:applyForce( 0, -gy, bullet.x, bullet.y );
end;
end;

function addBullet()
local bullet = display.newImage( “bullet.png”, 160, 240, true );
physics.addBody( bullet, { density=0.6, friction=0.6, bounce=0.4, radius=4 } );
bullet.isBullet=true;
bullets:insert(bullet);
end;

function create()
g = display.newGroup();
bullets = display.newGroup();
g:insert(bullets);
end;

create();
addBullet();
Runtime:addEventListener( “enterFrame”, update );[/lua]

What am I doing wrong?
Thanks. [import]uid: 90140 topic_id: 5449 reply_id: 55864[/import]

Hi,
as applyForce has an accumulative effect, applying -gy every frame will quickly result in a force that will be greater than the gravity in your physics world.

I can see two possible solutions. The best idea would probably be to follow the opposite approach and set up physics with no gravity and then apply the force of gravity to objects that you want to fall, and simply leave the objects that you want to float alone. In such a scenario the applying of force every frame you’re doing above would probably simulate gravity quite well, as long as it’s pushing down and not up.

The second approach is trickier and would involve selectively generating the upwards thrust you’re using above, so that it is only applied when the object is falling due to gravity, and is not applied again until the object has spent the upward thrust you’ve just applied and starts falling again. This would result in a bobbling motion with the object constantly being thrust up, dropping again when gravity overcomes the thrust, then thrust up again, etc. It probably wouldn’t be very accurate either, the object probably wouldn’t stay at the same height over any long period of time, but if you’re simulating a balloon blowing along in a breeze, for example, it might work for you.

Personally I’d go with the first idea, especially if you want weightless bodies to interact with ‘heavy’ bodies. The physics engine isn’t designed to deal with true weightlessness, I think it needs everything to have a mass, so the best way to simulate weightlessness would be to have no direction being ‘down’ (i.e. setGravity(0,0) ) so the objects interact as if they’re lying flat on a table. In such an environment, gravity could be considered just another force that needs to be applied to some objects. [import]uid: 89028 topic_id: 5449 reply_id: 75541[/import]

use :setGravityScale(<negative value>) instead

Hi all,

Just to clarify, there is no API called “:setGravityScale()”. These two APIs address the topic:

  1. physics.setGravity( x,y )

https://docs.coronalabs.com/api/library/physics/setGravity.html

  1. body.gravityScale = g

https://docs.coronalabs.com/api/type/Body/gravityScale.html

Take care,

Brent

use :setGravityScale(<negative value>) instead

Hi all,

Just to clarify, there is no API called “:setGravityScale()”. These two APIs address the topic:

  1. physics.setGravity( x,y )

https://docs.coronalabs.com/api/library/physics/setGravity.html

  1. body.gravityScale = g

https://docs.coronalabs.com/api/type/Body/gravityScale.html

Take care,

Brent