I am making a game where a player collects coins… My coins can either be non-physic bodies or kinematic physics bodies(preferably non-physics bodies but whatever works), and they are already moving. My variables are face(for the main character) and coin(for the coins). How can I do collision detection between the two? Code help?
Thanks
Is there a simpler way to do it with using physics?
There is still a way.
From what I have understood in your post *just correct me if I am wrong* you are making the coin non physics object to prevent it from having friction when colliding? *To prevent this you can add isSensor = true parameter in your addBody i.e. physics.addBody(object, “kinematic”, {isSensor = true})* in this way you can use collision event listener and also makes your collision logic quite easier and also saved your coin =D.
Second is if there is lots of objects on your screen: *I prefer you to add type in your player and coin*
player.type = "player" coin.type = "coin"
In this way you can easily classify if your player collided with a coin object.
it will look like this
local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 if agro.type == "player" and hit.type == "coin" then "Do your move here" end end end Runtime:addEventListener("collision", onCollision)
Hope this helps =)
Is there a simpler way to do it with using physics?
There is still a way.
From what I have understood in your post *just correct me if I am wrong* you are making the coin non physics object to prevent it from having friction when colliding? *To prevent this you can add isSensor = true parameter in your addBody i.e. physics.addBody(object, “kinematic”, {isSensor = true})* in this way you can use collision event listener and also makes your collision logic quite easier and also saved your coin =D.
Second is if there is lots of objects on your screen: *I prefer you to add type in your player and coin*
player.type = "player" coin.type = "coin"
In this way you can easily classify if your player collided with a coin object.
it will look like this
local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 if agro.type == "player" and hit.type == "coin" then "Do your move here" end end end Runtime:addEventListener("collision", onCollision)
Hope this helps =)