Upward and downward objcts on the same screen

Hello everyone,

I am having an issue with my project I am working on. On my project I have objects going upward ( using, physics.setGravity(0, -0.3), a negative y-direction gravity value ). This works fine, as expected. But the problem arises when I try to create - spawn - other bodies downwards ( positive y-direction gravity value ). Is there a way I can get this accomplished? I have tried to set the gravity inside each spawn function with different y-direction gravity values, but I am only getting objects going up and no objects coming down.

Here are the functions I mentioned above:

 local objectUpWard = function()  
  
 physics.setGravity(0, -0.4) -- upward gravity  
 local temp3 = math.random(1,#carTableName)  
  
 local object = display.newImageRect(carTableName[temp3] .. ".png", 45,90)  
 --carLane1.rotation = 180  
 object.yScale = 0.8  
 object.xScale = 0.7  
 object.x = \_W \*0.5 - 57  
 object.y = \_H + 134  
  
 physics.addBody(object, {density=1.2, bounce = 0, radius = 5})  
 object.hit = "obj"  
 object.myName = "obj"  
  
end  

And the downward function:

  
 local objectDownWard = function()  
  
 physics.setGravity(0, -0.4) -- upward gravity  
 local temp4 = math.random(1,#carTableName)  
  
 local object1 = display.newImageRect(carTableName[temp3] .. ".png", 45,90)  
 --carLane1.rotation = 180  
 object1.yScale = 0.8  
 object1.xScale = 0.7  
 object1.x = \_W \*0.5 - 57  
 object1.y = \_H + 134  
  
 physics.addBody(object1, {density=1.2, bounce = 0, radius = 5})  
 object1.hit = "obj1"  
 object1.myName = "obj1"  
  
end  
  

Your help is highly appreciated!

Thank you. [import]uid: 75258 topic_id: 22416 reply_id: 322416[/import]

Hey Paulo,

Both of those are using upward gravity. (0, -0.4)

That said, switching gravity will effect all non-static physics bodies, so perhaps applying linear velocity might be a better option for one of the “groups”? (Upward or downward objects.) [import]uid: 52491 topic_id: 22416 reply_id: 89392[/import]

Hello Again :slight_smile: ( I feel like I am abusing your kindness and patience by having two issues at the same time. My apologies!)

…Ooops, my bad about the -0.4 … I should have changed the value (copy and paste in action!! ). I will try applying Linear velocity in one of the groups [import]uid: 75258 topic_id: 22416 reply_id: 89406[/import]

My understanding is that gravity affects the entire engine and isn’t settable on a object by object basis (would be nice if it worked that way for floating games), Switching gravity affects the whole world (and dynamic objects only as Peach said!)
[import]uid: 19626 topic_id: 22416 reply_id: 89411[/import]

I would try linear velocity first then if that doesn’t work like you want we’ll look at other options. (But based on your other thread I’m thinking it may be the best solution here.) [import]uid: 52491 topic_id: 22416 reply_id: 89423[/import]

I think you might also want to consider applyForce to the objects that are moving in the opposite direction to the gravity. I would try to have the physical properties imitate real life.

For example, if you have a scene with the gravity force down and you are dropping stones and balloons I imagine you would want the stones to fall and the balloons to float or even go up.

I would have set the gravity down (simulating the earth gravity) and each enterFrame apply an up “buoyancy” force to the balloons, representing the force applied on the balloons because of the heavier air around them.

the amount of force you apply up will determine the movement of the balloons,

If you just need something to move in linear speed regardless, I would use what was suggested already, setting linear speed. [import]uid: 80469 topic_id: 22416 reply_id: 89437[/import]

Keep in mind with force that you’d potentially have to reapply it; harder to get a smooth movement in some scenarios. (And applying too quickly will up the speed as it stacks.) [import]uid: 52491 topic_id: 22416 reply_id: 89540[/import]

Thank you all for your responses. So what I am going to do is create a function where I spawn , first , objects going up using using linear velocity and apply it on “enterFrame” event. Correct? Using this approach, will my objects still be able to register collision with other another object, say coming from the left? Or will I have to figure something out then? I am sorry for asking trivial question, but I have been beating my head agains the wall on this issue for a little while now. Thanks guys, I really appreciate your help. [import]uid: 75258 topic_id: 22416 reply_id: 89793[/import]

You should apply the linear velocity when spawned, not every frame. (Which I assume is meant by Runtime in this sense? Or no?)

Your objects should be able to register collisions without issue.

:slight_smile: [import]uid: 52491 topic_id: 22416 reply_id: 89835[/import]

Hello guys,

So, now I have used LinearVelocity in the SpawnObject function. All is working as intended: I am getting objects going up, and if I want I can easily reverse direction by changing the y value…

The problem now is that I am trying to register collision detection between the objects spawned with a sprite character I have going left and right. From what I understand, kinematic objects cannot collide with dynamic objects, but I don’t know how to have the character collide with the spawned objects ( the ones with LinearVelocity).

What you think? [import]uid: 75258 topic_id: 22416 reply_id: 91158[/import]

Hey.

Is there a reason you don’t want to make all the objects dynamic? That’d be the simplest solution.

Otherwise, I think Object:isSensor = true will still register the collisions, (though I might be wrong).

Give that a try.
[import]uid: 67933 topic_id: 22416 reply_id: 91172[/import]

Hi Spider_newgen,

Thanks for your response. The reason I don’t make all objects ( the sprite going left and right, and the spawned objects going upwards) is that when I make all of them dynamic, then all of them will obey the gravity since they are dynamic. This will not only make the spawned upwards objects not appear, it will also make the sprite character fall which is not what I want to happen.

This issue should be easy to solve, the problem is that I don’t have much experience working with kinematic, dynamic objects yet. So, please anyone out there, your help would be much appreciated. [import]uid: 75258 topic_id: 22416 reply_id: 91218[/import]

…Heres what I have now for spawning the upwards objects:

  
module(..., package.seeall);  
local \_W = display.contentWidth  
local \_H = display.contentHeight  
  
--Pass the state machine reference  
state = {};  
  
 carTableName = {}  
 carTable = {}  
 carTable1 = {}  
 carTable2 = {}  
 carTable3 = {}  
  
 local canHit = {" car ", "carLane1", "carLane2", "carLane3", "chicken"}  
  
 carTableName[1] = "car\_black"  
 carTableName[2] = "car\_green"  
 carTableName[3] = "car\_purple"  
 carTableName[4] = "car\_really\_green"  
 carTableName[5] = "car\_white\_truck"  
 carTableName[6] = "car\_yellow"  
 carTableName[7] = "car\_yellowee"  
 carTableName[8] = "car\_yellower"  
  
function newCarro(velocity)  
  
 local temp = math.random(1,#carTableName)  
  
 local carro = display.newImageRect(carTableName[temp] .. ".png", 45,90)  
 carro.x = \_W \*0.5 - 26  
 carro.y = \_H + 134  
 carro.yScale = 0.8  
 carro.xScale = 0.7  
  
  
 --Set up some custom properties  
 carro.type = "carro";  
 carro.myName = "carro"  
  
 carro.multiplier = nil;  
 carro.points = 1;  
  
 physics.addBody(carro, "kinematic", {density=8, bounce=0, friction=2, radius=40});  
 carro:setLinearVelocity(0,velocity \* -1);  
  
 return carro;  
  
end  
  

The above piece of code, is a “class” that essentially spawns the objects and then appends a Linearvelocity to each spawned object.

Here’s the implementation of the this class in a different file:

  
local function spawnCar4(number)  
  
 local function spawn(e)  
  
 --and assign it a random linear velocity   
 local d = carro.newCarro(math.random(40,90));  
  
 carro[d] = d;  
 carro[d].x = \_W \*0.5 + 57  
  
 -- carro[d].remove = true;  
 localGroup:insert(carro[d]);  
  
 local function onCarCollision(self, event)   
  
 if ( event.other.hit == "chicken" ) then  
 print("Hit Chicken")  
  
 self:removeSelf()  
 return true  
 elseif( event.other.hit == "car") then  
  
 self:removeSelf()  
  
 end  
  
 end  
 carro[d].collision = onCarCollision  
 carro[d]:addEventListener( "collision" , carro[d] )  
  
 end  
  
 tmr4 = timer.performWithDelay(math.random(1200, 19000), spawn, 0);  
  
 end  
  

The onCarCollision() function is not catching/detect the collision between the carro[d] and the ‘chicken’ object which is the character object I need to collide and register collision with.

I have been trying to figure this out for a while now, and no luck.

Any ideas? If you need more explanation please let me know.

Thank you in advance. [import]uid: 75258 topic_id: 22416 reply_id: 91219[/import]

basically gravity is a one type of force which is constantly applying on your dynamic object so if you apply double force then gravity then they will go up instead of down.

to apply double force then gravity you can use something like this

myObj:applyForce(0,-20*math.sqrt(myObj.width*myObj.height,0,0)

not tested but something like this will definitely work also i think corona is not giving mass of object (not sure ) if it is giving then you can use

myObj:applyForce(0,-20*myObj.mass,0,0) else try to find approx mass from width and height of the object and its density that will be the perfect for two gravity
:slight_smile: [import]uid: 12482 topic_id: 22416 reply_id: 91220[/import]

Thanks @hgvyas123 for your quick response. I have tried that with my character and it’s acting strange and not responding as desired.

Again, I am able to get the object I want to go upwards, no problem ( using Linear velocity). I guess my big question would be:
– Do I make my sprite character kinematic or dynamic so that it respondes to collision when it colides with the upward spawned objects? That’s my BIGGEST question.

[import]uid: 75258 topic_id: 22416 reply_id: 91222[/import]

strange i have done same thing in one of my project it behaves perfectly ,

see this for more info

http://www.emanueleferonato.com/2009/07/17/managing-multiple-gravities-with-box2d/ [import]uid: 12482 topic_id: 22416 reply_id: 91229[/import]