Hello everybody
It is my first Corona project, so the question might be a bit basic.
I programmed a small game where chairs fall from the sky and a track has to collect them before they reach ground. If the user touches the display, the truck will move forward. So far so good.
There are two problems I can’t solve:
- Often, a collision is detected (and the chair removed), before the chair and the truck really collide (so a chair is far over the truck, but there is still a collision detected). Tried to play with isSensor and radius, but can’t fix this.
- The chairs usualy move through the left side of the truck, but will be detected on the right side. Really makes no sense to me.
Chair and Truck are both PNG files, which don’t have much spare space around the object.
This is how the truck is created:
-- truck local truck = display.newImageRect("truck.png", 250, 170) truck:setReferencePoint(display.BottomLeftReferencePoint) physics.addBody(truck, "static", {isSensor=false}) --XY collision truck.x =50 --XY truck.y = 665 truck.speed = 0.1 truck.myName = "truck"
This is how Chairs are created:
function createFurniture() local newChair = display.newImage("chair.png") table.insert( chairTable, newChair ) physics.addBody( newChair, "dynamic", {isSensor=false}) -- collision XY newChair.myName = "chair" newChair.isBullet = true local whereFrom = math.random( 1, 13 ) newChair.x = 200 + 60 \* whereFrom -- Position Chair newChair.y = 0 newChair:setLinearVelocity( 0, 0 ) -- Stuehle leicht nach hinten ziehen end
This is how collision detection looks like:
local function onLocalCollision( event ) if ( event.phase == "began" and collisionOn == 1) then local obj1 = event.object1 local obj2 = event.object2 ---local obj1 = self.myName ---local obj2 = event.other.myName if ( obj1.myName == "truck" and obj2.myName == "chair" ) then display.remove( obj2 ) -- remove chair print("coll 1") --update stuhlcounter chairCount = chairCount +1 chairCountText.text = chairCount elseif ( obj1.myName == "chair" and obj2.myName == "truck" ) then display.remove( obj1 ) -- remove chair print("coll 2") --update stuhlcounter chairCount = chairCount +1 chairCountText.text = chairCount --update stuhlcounter elseif (obj1.myName == "road" and obj2.myName == "chair") then print("road") display.remove( obj2 ) -- remove chair lebenCount = lebenCount -1 lebenCountText.text = lebenCount end end end Runtime:addEventListener("collision", onLocalCollision)
I am thankful for every hint.
Thanks a lot for your support.