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?

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

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 :)

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 :)

Peach

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
---------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 )

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

1
2
3
4
---------SET PHYSICS---------------
local physics = require("physics")
physics.start()
physics.setGravity(0,0)

Hehe, yes - that will do that ;)

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

Peach

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.

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

Oh wow thats good to know, thanks for the heads up on using "type" ill change to something else!

I just tried changing type to objType in all my variables where needed, and it caused the collision detection not to work...

Post up some code, it shouldn't make any difference :) Must be a slight oversight on some variable.

ok yea got it working, typo on my part, sorry to waste a min of your time, thanks for the help!

No probs, anytime :)

views:1551 update:2011/10/4 17:12:07
corona forums © 2003-2011