Helicopter style game, terrain collision, how to

Hey everyone,

i am trying to create a game similar to those helicopter games, where you are in the air and you try to avoid the terrain on the ground or the ceiling.

The issue im running into is how to detect collision with my terrain objects. My player object is a “kinematic” object, because i don’t want gravity on it. And i want my terrain to be static so it stays in place, but from what i can see, “kinematic” and “static” can’t collide!!

so im stumped, if anyone has any ideas or suggestions it would be appreciated.

ps - i also have my physics.setGravity(0,0) but when i create a dynamic object it still falls, is that normal? [import]uid: 19620 topic_id: 14559 reply_id: 314559[/import]

Uh, just make your player static -_- btw there is sample code for a collision listener. Look at the egg sample code…
Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 14559 reply_id: 53862[/import]

Erm, no - that isn’t normal. I’m guessing you’ve set something up weird in your code. With 0 gravity your dynamic object will not fall.

With physics set to (0,0) you should be able to use a dynamic body for your helicopter and a static body for your ground without issue.

Share some code if you like, maybe we can help you see where you are going wrong :slight_smile:

For collision detection, I’ve got a really gentle tutorial - http://techority.com/2011/06/19/corona-for-newbies-part-4-physics/

Take a look :slight_smile:

Peach [import]uid: 52491 topic_id: 14559 reply_id: 53887[/import]

ok i think ill share a bit o code. I have done alot of collision stuff in a previous game of mine, so i know how to check if an object has collided, im just trying to get my physics object types figured out how i would like, here is what i have.

[lua]---------SET PHYSICS---------------
local physics = require(“physics”)
physics.setGravity(0,0)
physics.start()
–physics.setDrawMode(“hybrid”)

–Main Player Object Creation
local playerObject = display.newImage(“bomb.png”)
playerObject.x = midX - 80
playerObject.y = midY + 30
physics.addBody(playerObject, “kinematic”, { isSensor = true, density=0, radius = 25})
gameScreen:insert(playerObject)
playerObject.type=“playerObject”

local ground = display.newRect(0,0, 800, 10 )
ground.x = 519
ground.y = 255
gameScreen:insert(ground)
physics.addBody( ground, “static”, { density=1, friction=0.3, bounce=0 } )

local object_1 = display.newRect(0,0, 50, 50 )
object_1.x = 369
object_1.y = 100
gameScreen:insert(object_1)
physics.addBody( object_1, { density=1, friction=0.3, bounce=0.2 } )
object_1.type=“death”

local onLocalCollision = function( self, event )

if event.target.type == “playerObject” and event.other.type == “death” and event.phase == “began” then

testText.text = “collided!”

end
end

—Collision Listener
playerObject.collision = onLocalCollision
playerObject:addEventListener( “collision”, playerObject )[/lua]

Anyways thats pretty much the basic code, when i start the game the object that is dynamic still falls.
Maybe you could try it yourself peach? could be a corona issue? probly not =D [import]uid: 19620 topic_id: 14559 reply_id: 53934[/import]

Alllright i figured it out, the issue is that i was putting the physics.setGravity( 0,0 ) before physics.start()

the correct way is as follows

---------SET PHYSICS---------------  
local physics = require("physics")  
physics.start()  
physics.setGravity(0,0)  

hope this helps someone else [import]uid: 19620 topic_id: 14559 reply_id: 53961[/import]

Hehe, yes - that will do that :wink:

Well done on figuring it out - glad your bodies are working now!

Peach [import]uid: 52491 topic_id: 14559 reply_id: 54039[/import]

btw avoid using “type” as a variable name as it’s a reserved keyword. Using it as you are could cause unexpected results.

If you want to use type in the name maybe make a variant… “objType” or something similar would be ok.

[import]uid: 84637 topic_id: 14559 reply_id: 54059[/import]

Well spotted, Danny.

We should put up a complete list of reserved words sometime.

I once used a sound file called “break” in a build for Android, totally messed it up, haha.

Peach [import]uid: 52491 topic_id: 14559 reply_id: 54154[/import]

So this game:
http://www.helicoptergame.net/
[import]uid: 79135 topic_id: 14559 reply_id: 54173[/import]

Oh wow thats good to know, thanks for the heads up on using “type” ill change to something else! [import]uid: 19620 topic_id: 14559 reply_id: 54182[/import]

I just tried changing type to objType in all my variables where needed, and it caused the collision detection not to work… [import]uid: 19620 topic_id: 14559 reply_id: 54184[/import]

Post up some code, it shouldn’t make any difference :slight_smile: Must be a slight oversight on some variable. [import]uid: 84637 topic_id: 14559 reply_id: 54185[/import]

ok yea got it working, typo on my part, sorry to waste a min of your time, thanks for the help! [import]uid: 19620 topic_id: 14559 reply_id: 54186[/import]

No probs, anytime :slight_smile: [import]uid: 84637 topic_id: 14559 reply_id: 54187[/import]