Objects collide before they touch

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:
 

  1. 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.
     
  2. 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.

Questions:

  1. How big are you truck and chair images? Is there a lot of border around the actual pictures which will be part of the physics body?

  2. Why are you setting the truck? Is it really necessary? Could it be affecting the position of the image relative to it’s body?

  3. Have you tried physics.setDrawMode(“hybrid”) ? (This should be #1 !)

You are also calling your collision listener ‘local’ but listening to Runtime, which is anything but local. You also are not returning true or false from you collision listener, which is probably having some strange effects.

I recommend you read the physics guide, especially the section on Bodies and the section on Collisions.

Hello horacebury

Thanks for your thoughts. 

1.) Chair: 116x89, 12KB // Truck: 300*206, 48KB

2.) What do you mean by “setting the truck”?

3.) I didn’t try physics.setDrawMode(“hybrid”) yet … I will try it later.

I initially had a local collision listener (truck), but changed to global. But it seems I forgot to rename the function. I will adapt the code later.

I read the collision section a couple of times but will do it again.

  1. Is there much masking space around the edges of the actual images up to the pictures within the images?

  2. I meant the reference point.

  3. Hybrid will show you where the physics bodies are in relation to the display objects.

I would second looking at hybrid. You’ll get a feel for where the bounds are for the physics objects in your game.

The problem was because of the reference point setting. I skipped that and everything worked. Thanks a lot!

Questions:

  1. How big are you truck and chair images? Is there a lot of border around the actual pictures which will be part of the physics body?

  2. Why are you setting the truck? Is it really necessary? Could it be affecting the position of the image relative to it’s body?

  3. Have you tried physics.setDrawMode(“hybrid”) ? (This should be #1 !)

You are also calling your collision listener ‘local’ but listening to Runtime, which is anything but local. You also are not returning true or false from you collision listener, which is probably having some strange effects.

I recommend you read the physics guide, especially the section on Bodies and the section on Collisions.

Hello horacebury

Thanks for your thoughts. 

1.) Chair: 116x89, 12KB // Truck: 300*206, 48KB

2.) What do you mean by “setting the truck”?

3.) I didn’t try physics.setDrawMode(“hybrid”) yet … I will try it later.

I initially had a local collision listener (truck), but changed to global. But it seems I forgot to rename the function. I will adapt the code later.

I read the collision section a couple of times but will do it again.

  1. Is there much masking space around the edges of the actual images up to the pictures within the images?

  2. I meant the reference point.

  3. Hybrid will show you where the physics bodies are in relation to the display objects.

I would second looking at hybrid. You’ll get a feel for where the bounds are for the physics objects in your game.

The problem was because of the reference point setting. I skipped that and everything worked. Thanks a lot!