Spawning + Collision Listener

I’ve been pulling my hair out for the last couple of hours over this, so hoping somebody might eb able to shed some light.

I’m spawning balls and attaching collision listeners, like so:

function onLocalCollision( self, event )  
 print("onLocalCollision()")  
 if ( event.phase == "began" ) then  
  
 print( self.myName .. ": collision began with " .. event.other.myName )  
  
 elseif ( event.phase == "ended" ) then  
  
 print( self.myName .. ": collision ended with " .. event.other.myName )  
  
 end  
end  
  
local function newBall( params )  
 local xPos = math.random(100,300)  
 local yPos = math.random(100,200)  
  
 local ball = display.newCircle(xPos, yPos, params.radius)  
 ball.xdir = params.xdir  
 ball.ydir = params.ydir  
 ball.xspeed = params.xspeed  
 ball.yspeed = params.yspeed  
 ball.radius = params.radius  
 physics.addBody(ball, "kinematic", {density = 1, bounce = 0.3, friction = 0.1, radius = params.radius})  
  
 ball.collision = onLocalCollision  
 ball:addEventListener("collision", ball)  
 return ball  
end  
for \_,item in ipairs( params ) do  
 local ball = newBall( item )  
 balls[#balls + 1] = ball  
 group:insert(ball)  
 end  

The balls are being spawned fine with a physics body attached, however the onLocalCollision is never firing. No errors, just no call to the function.

Any help would be appreciated.
[import]uid: 33275 topic_id: 35314 reply_id: 335314[/import]

Ok this is weird, when I remove “kinematic” from the physics body (assuming the default ‘dynamic’ type) the collision event fires.

However I dont want these balls responding to gravity, I’m animating them around the screen - hence the choice of kinematic.

Am I doing this wrong, do kinematic bodies not collide with one another? [import]uid: 33275 topic_id: 35314 reply_id: 140347[/import]

Just found out you cant collide with kinematic types… [import]uid: 33275 topic_id: 35314 reply_id: 140357[/import]

I think it will trigger the event if you use isSensor http://docs.coronalabs.com/api/type/Body/isSensor.html [import]uid: 202223 topic_id: 35314 reply_id: 140372[/import]

Hi @SegaBoy,
You’re basically correct… kinematic doesn’t collide with kinematic. You need one of the colliding objects to be dynamic (or both) to trigger the collision. So, you can do kinematic+dynamic, or static+dynamic, but -not- kinematic+kinematic.

To make them dynamic but NOT respond to gravity, you can set the per-body gravity scale using this property:
http://docs.coronalabs.com/api/type/Body/gravityScale.html

Does this help?
Brent [import]uid: 200026 topic_id: 35314 reply_id: 140398[/import]

Ok this is weird, when I remove “kinematic” from the physics body (assuming the default ‘dynamic’ type) the collision event fires.

However I dont want these balls responding to gravity, I’m animating them around the screen - hence the choice of kinematic.

Am I doing this wrong, do kinematic bodies not collide with one another? [import]uid: 33275 topic_id: 35314 reply_id: 140347[/import]

Just found out you cant collide with kinematic types… [import]uid: 33275 topic_id: 35314 reply_id: 140357[/import]

I think it will trigger the event if you use isSensor http://docs.coronalabs.com/api/type/Body/isSensor.html [import]uid: 202223 topic_id: 35314 reply_id: 140372[/import]

Hi @SegaBoy,
You’re basically correct… kinematic doesn’t collide with kinematic. You need one of the colliding objects to be dynamic (or both) to trigger the collision. So, you can do kinematic+dynamic, or static+dynamic, but -not- kinematic+kinematic.

To make them dynamic but NOT respond to gravity, you can set the per-body gravity scale using this property:
http://docs.coronalabs.com/api/type/Body/gravityScale.html

Does this help?
Brent [import]uid: 200026 topic_id: 35314 reply_id: 140398[/import]