Adding a collision to my Game -- help

So far for my school project I have this code
–Carz game–
local friction = 0.8
local gravity = .09
local speedX, speedY, prevX, prevY, lastTime, prevTime = 0, 0, 0, 0, 0, 0

–Create Car Image
local myCar = display.newImage(“car.png”)
ScreenW = display.contentWidth
ScreenH = display.contentHeight
myCar.x = ScreenW/2
myCar.y = ScreenH/2 – Centres the car in the middle of the screen.
myCar.y = ScreenH/2 – Centres the car in the middle of the screen.

–Add score
local points = 0
scoreText = display.newText("Score: " …points, 0, 20, arial, 30)
scoreText:setTextColor(0,0,0)
ScreenW = display.contentWidth
ScreenH = display.contentHeight
scoreText.x = ScreenW/2
scoreText.y = ScreenH-30 – Centres the score centre bottom
–Physics
local physics = require “physics”
physics.start()
physics.setGravity(0, 4.95) – 4.95 m/s
physics.setDrawMode(“normal”)

–Spawn Petrol 1
local Fuel = display.newImage(“Petrol.png”)
Fuel.x=math.random(0, 320)
Fuel.y=math.random(-2000, -1000)
physics.addBody(Fuel, dynamic,{friction=2})
I want to but am unsure on how to make a collision function between, “Fuel” and “myCar”
I want it so whenever “myCar” Collides with “Fuel” that the local points will increase by 20 points.
Can someone please help :slight_smile:
-Dan [import]uid: 223003 topic_id: 35661 reply_id: 335661[/import]

[code]
–Give your fuel and car a “name”
Fuel.name = “Fuel”
myCar.name = “myCar”

–Then all you gotta do is create a collision event function
function onCollision( event)
if event.target1.name == “myCar” and event.target2.name == “Fuel” then
event.target2:removeSelf()
event.target2 = nil
points = points + 20
end
end
myCar:addEventListener( “collision”, onCollision )

Something like that
[import]uid: 77199 topic_id: 35661 reply_id: 141840[/import]

Firstly: great thanks ! When I was looking at other forum posts they gave similar code although I did not understand the functionality of it, although having it in the perspective of my own code really helps.

Although… Without any errors the App runs. Although when collision is made nothing occurs. I told the collision function to print(“COLLIDE”) but it isn’t registering that the two items are colliding. I was thinking perhaps that the car needs to have physics added to be “dynamic”. Any advice would be hugely appreciated. [import]uid: 223003 topic_id: 35661 reply_id: 141863[/import]

Hi @danw47,
Welcome to Corona! In a collision, at least one of the bodies must be dynamic… or both. You will not receive a collision event between two kinematic or static bodies (or one kinematic and one static).

Also, you can place physics objects in different display groups (http://docs.coronalabs.com/api/library/display/newGroup.html), but those groups cannot be moved/shifted independent of each other. In other words, all groups which include potentially-colliding physics objects must retain the same relative positioning for the Box2D system to correctly detect collisions. This is a common misunderstanding, so I wanted to mention it in advance.

Any other questions, don’t hesitate to ask.

Best regards,
Brent [import]uid: 200026 topic_id: 35661 reply_id: 141873[/import]

Could I please have an example of this how to being I am having a hard time understanding how this works and how to implement it into my code.

Many thanks,
Dan. [import]uid: 223003 topic_id: 35661 reply_id: 142016[/import]

Hi Dan,
There are code examples in the following documentation. It might seem like alot to digest at first, but work through it step by step and it should make sense. At the beginning, you can ignore “preCollision” and “postCollision”, as these are more advanced topics. What you should focus on is collision phases, and either “Global collision listeners” or “Local collision listeners”. Both of these accomplish basic collision, just in a slightly different method.

http://developer.coronalabs.com/content/game-edition-collision-detection

Best regards,
Brent
[import]uid: 200026 topic_id: 35661 reply_id: 142024[/import]

Okay okay I’ve got everything to work except the score ! :slight_smile: (thanks for the help)

I have these codes:
–Add score
local points = 0
scoreText = display.newText("Score: " …points, 0, 20, arial, 30)
scoreText:setTextColor(0,0,0)
ScreenW = display.contentWidth
ScreenH = display.contentHeight
scoreText.x = ScreenW/2
scoreText.y = ScreenH-30 – Centres the score centre bottom

and

–Collision event function
function onCollision (event)
if event.object1.myName==“Fuel” and event.object2.myName == “myCar” then
print(“HIT”)
event.object1:removeSelf()
event.object1 = nil
points = points + 20
end
end
Runtime:addEventListener(“collision”, onCollision)

I have told it to do points + 20 but nothing is happening. Any last words of advice my friend? :slight_smile: [import]uid: 223003 topic_id: 35661 reply_id: 142304[/import]

--Collision event function function onCollision (event) if event.object1.myName=="Fuel" and event.object2.myName == "myCar" then print("HIT") event.object1:removeSelf() event.object1 = nil points = points + 20 scoreText.text = "Score: "..points end end Runtime:addEventListener("collision", onCollision) scoreText.text = "Score: "..points --adding this will update the text [import]uid: 77199 topic_id: 35661 reply_id: 142305[/import]

Thank you lot so much.
could not be anymore grateful

love,
Dan. [import]uid: 223003 topic_id: 35661 reply_id: 142307[/import]

[code]
–Give your fuel and car a “name”
Fuel.name = “Fuel”
myCar.name = “myCar”

–Then all you gotta do is create a collision event function
function onCollision( event)
if event.target1.name == “myCar” and event.target2.name == “Fuel” then
event.target2:removeSelf()
event.target2 = nil
points = points + 20
end
end
myCar:addEventListener( “collision”, onCollision )

Something like that
[import]uid: 77199 topic_id: 35661 reply_id: 141840[/import]

Firstly: great thanks ! When I was looking at other forum posts they gave similar code although I did not understand the functionality of it, although having it in the perspective of my own code really helps.

Although… Without any errors the App runs. Although when collision is made nothing occurs. I told the collision function to print(“COLLIDE”) but it isn’t registering that the two items are colliding. I was thinking perhaps that the car needs to have physics added to be “dynamic”. Any advice would be hugely appreciated. [import]uid: 223003 topic_id: 35661 reply_id: 141863[/import]

Hi @danw47,
Welcome to Corona! In a collision, at least one of the bodies must be dynamic… or both. You will not receive a collision event between two kinematic or static bodies (or one kinematic and one static).

Also, you can place physics objects in different display groups (http://docs.coronalabs.com/api/library/display/newGroup.html), but those groups cannot be moved/shifted independent of each other. In other words, all groups which include potentially-colliding physics objects must retain the same relative positioning for the Box2D system to correctly detect collisions. This is a common misunderstanding, so I wanted to mention it in advance.

Any other questions, don’t hesitate to ask.

Best regards,
Brent [import]uid: 200026 topic_id: 35661 reply_id: 141873[/import]

Could I please have an example of this how to being I am having a hard time understanding how this works and how to implement it into my code.

Many thanks,
Dan. [import]uid: 223003 topic_id: 35661 reply_id: 142016[/import]

Hi Dan,
There are code examples in the following documentation. It might seem like alot to digest at first, but work through it step by step and it should make sense. At the beginning, you can ignore “preCollision” and “postCollision”, as these are more advanced topics. What you should focus on is collision phases, and either “Global collision listeners” or “Local collision listeners”. Both of these accomplish basic collision, just in a slightly different method.

http://developer.coronalabs.com/content/game-edition-collision-detection

Best regards,
Brent
[import]uid: 200026 topic_id: 35661 reply_id: 142024[/import]

Okay okay I’ve got everything to work except the score ! :slight_smile: (thanks for the help)

I have these codes:
–Add score
local points = 0
scoreText = display.newText("Score: " …points, 0, 20, arial, 30)
scoreText:setTextColor(0,0,0)
ScreenW = display.contentWidth
ScreenH = display.contentHeight
scoreText.x = ScreenW/2
scoreText.y = ScreenH-30 – Centres the score centre bottom

and

–Collision event function
function onCollision (event)
if event.object1.myName==“Fuel” and event.object2.myName == “myCar” then
print(“HIT”)
event.object1:removeSelf()
event.object1 = nil
points = points + 20
end
end
Runtime:addEventListener(“collision”, onCollision)

I have told it to do points + 20 but nothing is happening. Any last words of advice my friend? :slight_smile: [import]uid: 223003 topic_id: 35661 reply_id: 142304[/import]

--Collision event function function onCollision (event) if event.object1.myName=="Fuel" and event.object2.myName == "myCar" then print("HIT") event.object1:removeSelf() event.object1 = nil points = points + 20 scoreText.text = "Score: "..points end end Runtime:addEventListener("collision", onCollision) scoreText.text = "Score: "..points --adding this will update the text [import]uid: 77199 topic_id: 35661 reply_id: 142305[/import]

Thank you lot so much.
could not be anymore grateful

love,
Dan. [import]uid: 223003 topic_id: 35661 reply_id: 142307[/import]