Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Increased Sensitivity of Object
Started by ravipsr Dec 02 2014 10:24 PM

10 replies to this topic
swipe touch tap sensitivity display object contentbounds image bounds collision
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ravipsr

[GLOBAL: userInfoPane.html]
ravipsr
  • Enthusiast

  • 35 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#3

ravipsr

[GLOBAL: userInfoPane.html]
ravipsr
  • Enthusiast

  • 35 posts
  • Corona SDK

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.



[TOPIC: post.html]
#4

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

ravipsr

[GLOBAL: userInfoPane.html]
ravipsr
  • Enthusiast

  • 35 posts
  • Corona SDK

This solution is perfect for increasing the sensitivity, but without causing any unwanted physic events!

 

Thanks a lot, Brent! 



[TOPIC: post.html]
#6

ravipsr

[GLOBAL: userInfoPane.html]
ravipsr
  • Enthusiast

  • 35 posts
  • Corona SDK

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:

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 } )

 

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.



[TOPIC: post.html]
#7

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#8

ravipsr

[GLOBAL: userInfoPane.html]
ravipsr
  • Enthusiast

  • 35 posts
  • Corona SDK

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:

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

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.



[TOPIC: post.html]
#9

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#10

ravipsr

[GLOBAL: userInfoPane.html]
ravipsr
  • Enthusiast

  • 35 posts
  • Corona SDK

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:

 

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

 

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.



[TOPIC: post.html]
#11

ravipsr

[GLOBAL: userInfoPane.html]
ravipsr
  • Enthusiast

  • 35 posts
  • Corona SDK

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):

 

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

 

Thanks for all your help, Brent.




[topic_controls]
[/topic_controls]