In my game, I am trying to make an object more detectable or sensitive to swipe motions, since it’s fairly small in size and hard to swipe.
How would you increase the sensitivity of a display object? I know that increasing the image bounds will obviously lead to more sensitivity, but the increased image bounds will lead to a trigger in collision events that it’s not supposed to. Is there any other way of increasing its sensitivity? Thanks.
Hi @ravipsr,
To clarify, you are using both touch events and physics? So basically, the object could be larger (to improve touch sensitivity) but you don’t want its increased size to affect physics collisions?
If so, remember that the physics body is somewhat independent of the display object that it’s placed on. For example, if you have a square display object of 40x40, you can make the physics body be 30x30, and the extra 5 pixels that pad around it (most likely they’d be transparent) will detect touch, but the physics will appear to occur only on the non-transparent part of the image.
If this isn’t what you’re attempting to do, then perhaps another workaround would be possible, but we’d need to hear more details on what you need to accomplish.
Hope this helps,
Brent
Hi Brent,
To clarify, I am using both touch events and physics. The object can be larger to improve touch sensitivity, but the increased size should not affect physics collisions. In other words, the increased size should only detect touch (for better sensitivity), and not physics collisions.
I did not know that the physics body can be independent of the display object it’s placed on. How would you go about setting this in the code? Is there something in the API?
Thanks for helping out.
Hi @ravipsr,
The following guide explains nearly everything about physics bodies:
http://docs.coronalabs.com/guide/physics/physicsBodies/index.html
If your physics body is a basic circle, you can adjust its size using the “radius” option. If it’s a polygon, you’ll need to construct it using the “shape” option with the proper vertices.
If it’s a rectangle, then you can’t just rely on Corona to auto-trace it, since it would span around the entire image (which clearly you don’t want). If that’s the case, you’ll need to construct it as a 4-sided shape using the “shape” option.
Also, remember that you can test your physics bodies by turning on “hybrid” draw mode:
http://docs.coronalabs.com/api/library/physics/setDrawMode.html
Best regards,
Brent
This solution is perfect for increasing the sensitivity, but without causing any unwanted physic events!
Thanks a lot, Brent!
Hi Brent,
I thought it was working with the sensitivity, but it’s still detecting the collision events even after I set the physics body size.
Here’s my code:
[lua]
local bluespaceship = display.newImageRect(“Spaceship” … tostring(rfn) … “Swipe.png”, 115, 115)
local spaceshipShape = { -20, -20, 20, -20, 20, 20, -20, 20 }
physics.addBody( bluespaceship, “dynamic”, { shape = spaceshipShape, density=5, bounce=0.2, isSensor=true } )
[/lua]
Even though I set its shape to 20x20 pixels, it is still detecting the spaceship as if it is 115 x 115 pixels.
What is wrong with the code? It would be great if anyone could help. Thanks.
Hmmm… how does the physics body appear when you view it in “hybrid” draw mode? That is the ultimate way to see what’s going on in regards to collisions…
Hi Brent,
I think I discovered my problem. I’m using a non-physics way of detecting my collisions with the hasCollided function to see if two objects overlap. The code is like this:
[lua]
local function hasCollided( obj1, obj2 )
if ( obj1 == nil ) then --make sure the first object exists
return false
end
if ( obj2 == nil ) then --make sure the other object exists
return false
end
if (obj1.contentBounds == nil) then
return false
end
if (obj2.contentBounds == nil) then
return false
end
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
if (left == nil) then
return false
end
if (right == nil) then
return false
end
if (up == nil) then
return false
end
if (down == nil) then
return false
end
return (left or right) and (up or down)
end
[/lua]
I don’t think it works for custom physics bodies.
However, I did find a thread addressing the same issue over here: http://forums.coronalabs.com/topic/53018-detecting-non-physics-collision-between-png-images/?hl=.contentbounds
Is this it? Do I have the use the SAT library mentioned in this thread to solve my issue? Is that the only workaround? Thanks.
Hi @ravipsr,
Oh, I assumed you were using physics-based collision detection. No, the non-physics method you’re using won’t work very well for custom/complex shapes, only for basic shapes… but if that’s all you need, just adjust the “left”, “right”, etc. values that you’re checking for (reduce them to inside the object’s content bounds).
I’m a little curious why you’re not using physics collision detection. Normally, if you’re using physics, you should use all of the powers of the physics engine. Using physics but then implementing non-physics collision detection seems a little odd to me. 
Brent
Hi Brent,
So all I would have to do is reduce the .contentBounds values for the spaceship? If so, would it go something like this:
[lua]
bluespaceship.contentBounds.xMin = bluespaceship.contentBounds.xMin + 50
bluespaceship.contentBounds.yMin = bluespaceship.contentBounds.yMin + 50
bluespaceship.contentBounds.xMax = bluespaceship.contentBounds.xMax - 50
bluespaceship.contentBounds.yMax = bluespaceship.contentBounds.yMax - 50
[/lua]
Or would I adjust the .contentBounds in the non-physics collision method?
And to answer your question, I have encountered problems with physics-based collisions in the past, so I tried to avoid relying on physics this time. I just used the non-physics based detection because it was perfect for my project (since I need to determine when the object overlaps with the other).
Thanks.
Never mind, I fixed it. I adjusted the values in the function and it worked perfectly.
My display object was 115 x 115 pixels, but I only wanted it to be detected in the non-physics based collision function as if it were 35 x 35 pixels. This is the revised function (in case anyone else needs a fix like this):
[lua]
local function hasCollided( obj1, obj2 )
if ( obj1 == nil ) then --make sure the first object exists
return false
end
if ( obj2 == nil ) then --make sure the other object exists
return false
end
if (obj1.contentBounds == nil) then
return false
end
if (obj2.contentBounds == nil) then
return false
end
local left = obj1.contentBounds.xMin + 40 <= obj2.contentBounds.xMin and obj1.contentBounds.xMax - 40 >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin + 40 >= obj2.contentBounds.xMin and obj1.contentBounds.xMin + 40 <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin + 40 <= obj2.contentBounds.yMin and obj1.contentBounds.yMax - 40 >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin + 40 >= obj2.contentBounds.yMin and obj1.contentBounds.yMin + 40 <= obj2.contentBounds.yMax
if (left == nil) then
return false
end
if (right == nil) then
return false
end
if (up == nil) then
return false
end
if (down == nil) then
return false
end
return (left or right) and (up or down)
end
[/lua]
Thanks for all your help, Brent.
Hi @ravipsr,
To clarify, you are using both touch events and physics? So basically, the object could be larger (to improve touch sensitivity) but you don’t want its increased size to affect physics collisions?
If so, remember that the physics body is somewhat independent of the display object that it’s placed on. For example, if you have a square display object of 40x40, you can make the physics body be 30x30, and the extra 5 pixels that pad around it (most likely they’d be transparent) will detect touch, but the physics will appear to occur only on the non-transparent part of the image.
If this isn’t what you’re attempting to do, then perhaps another workaround would be possible, but we’d need to hear more details on what you need to accomplish.
Hope this helps,
Brent
Hi Brent,
To clarify, I am using both touch events and physics. The object can be larger to improve touch sensitivity, but the increased size should not affect physics collisions. In other words, the increased size should only detect touch (for better sensitivity), and not physics collisions.
I did not know that the physics body can be independent of the display object it’s placed on. How would you go about setting this in the code? Is there something in the API?
Thanks for helping out.
Hi @ravipsr,
The following guide explains nearly everything about physics bodies:
http://docs.coronalabs.com/guide/physics/physicsBodies/index.html
If your physics body is a basic circle, you can adjust its size using the “radius” option. If it’s a polygon, you’ll need to construct it using the “shape” option with the proper vertices.
If it’s a rectangle, then you can’t just rely on Corona to auto-trace it, since it would span around the entire image (which clearly you don’t want). If that’s the case, you’ll need to construct it as a 4-sided shape using the “shape” option.
Also, remember that you can test your physics bodies by turning on “hybrid” draw mode:
http://docs.coronalabs.com/api/library/physics/setDrawMode.html
Best regards,
Brent
This solution is perfect for increasing the sensitivity, but without causing any unwanted physic events!
Thanks a lot, Brent!
Hi Brent,
I thought it was working with the sensitivity, but it’s still detecting the collision events even after I set the physics body size.
Here’s my code:
[lua]
local bluespaceship = display.newImageRect(“Spaceship” … tostring(rfn) … “Swipe.png”, 115, 115)
local spaceshipShape = { -20, -20, 20, -20, 20, 20, -20, 20 }
physics.addBody( bluespaceship, “dynamic”, { shape = spaceshipShape, density=5, bounce=0.2, isSensor=true } )
[/lua]
Even though I set its shape to 20x20 pixels, it is still detecting the spaceship as if it is 115 x 115 pixels.
What is wrong with the code? It would be great if anyone could help. Thanks.
Hmmm… how does the physics body appear when you view it in “hybrid” draw mode? That is the ultimate way to see what’s going on in regards to collisions…
Hi Brent,
I think I discovered my problem. I’m using a non-physics way of detecting my collisions with the hasCollided function to see if two objects overlap. The code is like this:
[lua]
local function hasCollided( obj1, obj2 )
if ( obj1 == nil ) then --make sure the first object exists
return false
end
if ( obj2 == nil ) then --make sure the other object exists
return false
end
if (obj1.contentBounds == nil) then
return false
end
if (obj2.contentBounds == nil) then
return false
end
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
if (left == nil) then
return false
end
if (right == nil) then
return false
end
if (up == nil) then
return false
end
if (down == nil) then
return false
end
return (left or right) and (up or down)
end
[/lua]
I don’t think it works for custom physics bodies.
However, I did find a thread addressing the same issue over here: http://forums.coronalabs.com/topic/53018-detecting-non-physics-collision-between-png-images/?hl=.contentbounds
Is this it? Do I have the use the SAT library mentioned in this thread to solve my issue? Is that the only workaround? Thanks.
Hi @ravipsr,
Oh, I assumed you were using physics-based collision detection. No, the non-physics method you’re using won’t work very well for custom/complex shapes, only for basic shapes… but if that’s all you need, just adjust the “left”, “right”, etc. values that you’re checking for (reduce them to inside the object’s content bounds).
I’m a little curious why you’re not using physics collision detection. Normally, if you’re using physics, you should use all of the powers of the physics engine. Using physics but then implementing non-physics collision detection seems a little odd to me. 
Brent
Hi Brent,
So all I would have to do is reduce the .contentBounds values for the spaceship? If so, would it go something like this:
[lua]
bluespaceship.contentBounds.xMin = bluespaceship.contentBounds.xMin + 50
bluespaceship.contentBounds.yMin = bluespaceship.contentBounds.yMin + 50
bluespaceship.contentBounds.xMax = bluespaceship.contentBounds.xMax - 50
bluespaceship.contentBounds.yMax = bluespaceship.contentBounds.yMax - 50
[/lua]
Or would I adjust the .contentBounds in the non-physics collision method?
And to answer your question, I have encountered problems with physics-based collisions in the past, so I tried to avoid relying on physics this time. I just used the non-physics based detection because it was perfect for my project (since I need to determine when the object overlaps with the other).
Thanks.