Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

non-physics object collision / touching event
Started by jason_bullen May 29 2018 03:21 AM

3 replies to this topic
collision events physics
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

jason_bullen

[GLOBAL: userInfoPane.html]
jason_bullen
  • Observer

  • 5 posts
  • Corona SDK

I have two objects: a circle and a moving rectangle.

I want to trigger an event when the two objects touch.

I just want an event triggered. I do not want them to bounce off each other.

 



[TOPIC: post.html]
#2

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 2,984 posts
  • Corona SDK

Make them dynamic physics objects and set their isSensor property to true. Check the docs to see how. You will need to add a listener to each (or a global Runtime listener, but I don't recommend this) to receive the event.



[TOPIC: post.html]
#3

jason_bullen

[GLOBAL: userInfoPane.html]
jason_bullen
  • Observer

  • 5 posts
  • Corona SDK

thanks Horacebury,

that was a good step. Unfortunately my code doesn't trigger the event i want.

Here is my code:

rectCollisionFilter = { categoryBits = 8, maskBits = 16 }
rectBodyElement = { friction=0.4, bounce=0.8, filter=rectCollisionFilter }
local fireRect = display.newRect( 125, 30, 70, 230 )
physics.addBody( fireRect, "static", rectBodyElement )
fireRect.myName = "fireRect"


circleCollisionFilter = { categoryBits = 16, maskBits = 8}
circleBodyElement = { friction=0.4, bounce=0.8, filter=circleCollisionFilter }
local testCircle = display.newCircle( 200, 230, 70 )
testCircle.myName = "CIRCLE1"
testCircle.isSensor = true
physics.addBody( testCircle, "static", circleBodyElement )

local function onLocalCollision( self, event )
    print( "COLLIDE" )
    print( "target " .. event.target.myName )
    print( "other " .. event.other.myName ) 
end
testCircle.collision = onLocalCollision
testCircle:addEventListener( "collision", testCircle )


I move the fireRect object over the circle but my function is never called.

What am I doing wrong?



[TOPIC: post.html]
#4

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,203 posts
  • Enterprise

at least one body must be dynamic (as per @horacebury already) statics do not collide w each other




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: collision, events, physics