Help with Images and Physics

Ok so I have some images Ive made, and once I run the main.lua file Everything works as planned except the two images that are supposed to interact with each other don’t really do anything. And when I change the amount of gravity they don’t interact period. With the “right” amount of gravity there is some interaction because the image that is supposed to bounce of the other one does so, except it overlaps with it.

And in some tutorials I have watched (specifically the make a game in 8 minutes one. Link here: http://www.youtube.com/watch?v=qEMGcy-mizM&list=PL6C17072D6565DB53&index=13&feature=plpp_video)
the balloon keeps bouncing until it comes to a stand still, yet in my game it bounces for about 1 second before coming to a stop.

Basically what I’m asking is why don’t my images interact with each other as they should, and does the physics engine expire after a certain amount of time?

Any ideas/comments would really be appreciated! [import]uid: 162953 topic_id: 29111 reply_id: 329111[/import]

Hi meziashi,

It would be helpful if you posted some/all your code to help figure out what the issue is.

When you say that the two images don’t really do anything, what do you mean exactly? Do they not fall down under the force of gravity?

In general, physics does take some experimenting to get right. You may want to ensure you’re setting reasonable density values for your objects (which affects their mass), and also setting their bounce values appropriately. Of course, you’ll also want to make sure your objects’ bodyType is “dynamic”.

  • Andrew [import]uid: 109711 topic_id: 29111 reply_id: 117109[/import]

So first of all my app is being made for horizontal play, and these are the settings I’m currently using to make the gravity behave in the desired direction.
I have taken the 3 sections of code related to my problem, which still persists. The bird image starts falling towards the bottom of the screen, yet when it comes in contact with the woodblock it simply passes through instead of bouncing. Maybe I have to mess with the density values?

Thanks so much for replying

[lua]local physics = require(“physics”)
physics.start()

–set gravity to act downwards
physics.setScale ( -20 )

physics.setGravity (0, -7)

local block2 = display.newImage(“WoodBlock2.png”)
block2.x = display.contentWidth/2
block2.y = display.contentHeight - 40
–add block 2 as physics body
physics.addBody(block2, { bounce = 1, density = 5 } )
block2.bodyType = “static”

local bird = display.newImage(“Bird2Side.png”)
bird.x = display.contentWidth/2
bird.y = display.contentHeight - 250
–add starting the bird as a physics body
physics.addBody(bird, { bounce = 0.5, density = .5})
bird.bodyType = “dynamic”[/lua] [import]uid: 162953 topic_id: 29111 reply_id: 117112[/import]

Hi there,

I think [lua]physics.setScale()[/lua] should always be set to a positive number. Try positive 20 for that, as well as +7 (instead of -7) for the gravity, and it runs fine for me. (You may then want to play around with the bounce and density values to get the desired effect.)

  • Andrew [import]uid: 109711 topic_id: 29111 reply_id: 117114[/import]

Im not sure how to explain this well but ill try. I have my app made to be played horizontally .
When I set the physics.setScale and physics.setGravity to positive the gravity acts in the opposite direction that I want it to. Basically it goes up instead of down. Then the bird just flys of the screen, and when I put in negative values it behaves odd as stated earlier. Changing the density didn’t really help but ill keep toying around with things thanks for the tips!

*hmm maybe the gravity is set to a certain direction when you have your app optimized for horizontal play. But that seems odd that i can’t invert the gravity… [import]uid: 162953 topic_id: 29111 reply_id: 117116[/import]

physics.setGravity() takes two parameters X gravity and Y gravity.

physics.setGravity(0,-7) should make everything float upwards since you have negative gravity on the Y axis.

physics.setGravity(7,0) should make everything float right (I think) where as physics.setGravity(-7, 0) would make things float left (that might be flipped).

physics.setScale() is used to change the number of pixels per meter and has no visual effect. A negative number doesn’t seem to make much sense here. Please carefully read:

http://docs.coronalabs.com/api/library/physics/setScale.html

to see exactly what .setScale() does and doesn’t do. [import]uid: 19626 topic_id: 29111 reply_id: 117144[/import]

hmm i tweaked a few things and things are working well now, thanks a ton guys! [import]uid: 162953 topic_id: 29111 reply_id: 117226[/import]