Bigger touch detection area

Hi guys,

I have really small players (animated sprites basically).

I added 50 by 50px rectangle as a shape to that animated sprite in order to achieve reliable touch detection.

Problem is that touch stays the same as before (with no shape added).

When touching shape nothing happens…

What am I doing wrong (below code shows no errors)?

local myShape = { -50, -50, 50, 50, 50, -50, -50, 50, isSensor = true} physics.addBody ( player, "dynamic", {shape = myShape, density=1, friction=0, bounce=0, filter = {categoryBits = 4, maskBits = 1}})

function playerTouch(self, event)    if(self.removeSelf == nil) then     return    elseif( event.phase == "began" and gameIsActive == true ) then ---- CODE end end

Many thanks!

Ivan :slight_smile:

Hi @ivan888,

The physics body that you add to a display object (in this case, your player sprite) is not directly related to the touch-sensitive region of the object… it merely works with the physics engine for purposes of collision detection. If you want to make the touch region bigger than the sprite, there are two options:

  1. Increase the size of the sprite by padding every frame with transparent pixels up to the size you desire. However, if you have a considerable number of frames, this may result in a much larger overall image sheet, and thus take up more texture memory.

  2. Joint another (rectangle) physics body to the player using a “weld” joint, and detect touch on that object, not the player. If you have just a few players, this would be fine from a performance standpoint, but if you have dozens or hundreds of players on the screen at once, option #1 is probably better for performance.

Hope this helps,

Brent

Thank you Brent!
I will go with the option #1.

Hi @ivan888,

The physics body that you add to a display object (in this case, your player sprite) is not directly related to the touch-sensitive region of the object… it merely works with the physics engine for purposes of collision detection. If you want to make the touch region bigger than the sprite, there are two options:

  1. Increase the size of the sprite by padding every frame with transparent pixels up to the size you desire. However, if you have a considerable number of frames, this may result in a much larger overall image sheet, and thus take up more texture memory.

  2. Joint another (rectangle) physics body to the player using a “weld” joint, and detect touch on that object, not the player. If you have just a few players, this would be fine from a performance standpoint, but if you have dozens or hundreds of players on the screen at once, option #1 is probably better for performance.

Hope this helps,

Brent

Thank you Brent!
I will go with the option #1.