How to Implement radar and physical collisions using addBody?

Hi,

I’m trying to get  collision working so that when my player comes within range of an enemy they will fire and respond. 

I’m also trying to make sure that when my player collides with an enemy a collision occurs (for damage)

I’m setting up the enemy as:

physics.addBody( object, “static”,  { density = 3.0, friction = 1.0, bounce = 0.1 } ) – real body of sprite
physics.addBody( object,   { density = 1, friction = 0, bounce = 0, radius=300, isSensor = true} ) – radar circle

object.collision = onLocalCollision
object:addEventListener( “collision”, object )

It looks like you can only have one addBody listening at a time.  Are there any tried and true ways to implement this?

Thanks for any help, Greg

For my game, I actually set up two objects with physics. The displayed object has the shape of the object itself, but I also create a hidden object with a large circle around it.  The hidden object would follow the displayed object everywhere, and if there is a collision detection between the player and the hidden object’s shape, then I know it’s nearby.

Hi Beyond… I guess that is my last resort.  It would be nice to only have one object to move - I have around 100 enemies.  I went ahead and welded the two bodies together:

physics.addBody( object, “static”,  { density = 3.0, friction = 1.0, bounce = 0.1 } )

physics.addBody( radar,  “static”,  { density = 1, friction = 0, bounce = 0, radius=300, isSensor = true} )
physics.newJoint( “weld”, object, radar, params.itemx, params.itemy )

now I need to figure out if there a way to tell which body has the collision event?  (or does the weld actually join them into one physics body)

I added :

   radar.collision = onRadarCollision
   radar:addEventListener( “collision”, radar )

so I do get a collision that is separate from the enemy itself.  So far so good, now I’m going to try to shoot!

Thanks for the response, Greg

I suggest adding an “ID” to each object so that way it can be properly tested and processed in the collision event.

object.id = “object”

radar.id = “radar”

Hi Greg,

Instead of welding bodies together, I’d recommend that you just use a multi-element physics body where one (or more) parts is the body, and one part is the sensor circle. Then, for collisions, you can sense whether the part that was collided with is the radar circle or not.

This tutorial should outline most of the process:

http://www.coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Take care,

Brent

Hi Brent,  Great tutorial! I’ll give it a shot.

… that sure simplified things!

physics.addBody( object, “static”,  { density = 3.0, friction = 1.0, bounce = 0.1 } , {radius=256, isSensor = true})

Thanks, Greg :slight_smile:

For my game, I actually set up two objects with physics. The displayed object has the shape of the object itself, but I also create a hidden object with a large circle around it.  The hidden object would follow the displayed object everywhere, and if there is a collision detection between the player and the hidden object’s shape, then I know it’s nearby.

Hi Beyond… I guess that is my last resort.  It would be nice to only have one object to move - I have around 100 enemies.  I went ahead and welded the two bodies together:

physics.addBody( object, “static”,  { density = 3.0, friction = 1.0, bounce = 0.1 } )

physics.addBody( radar,  “static”,  { density = 1, friction = 0, bounce = 0, radius=300, isSensor = true} )
physics.newJoint( “weld”, object, radar, params.itemx, params.itemy )

now I need to figure out if there a way to tell which body has the collision event?  (or does the weld actually join them into one physics body)

I added :

   radar.collision = onRadarCollision
   radar:addEventListener( “collision”, radar )

so I do get a collision that is separate from the enemy itself.  So far so good, now I’m going to try to shoot!

Thanks for the response, Greg

I suggest adding an “ID” to each object so that way it can be properly tested and processed in the collision event.

object.id = “object”

radar.id = “radar”

Hi Greg,

Instead of welding bodies together, I’d recommend that you just use a multi-element physics body where one (or more) parts is the body, and one part is the sensor circle. Then, for collisions, you can sense whether the part that was collided with is the radar circle or not.

This tutorial should outline most of the process:

http://www.coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

Take care,

Brent

Hi Brent,  Great tutorial! I’ll give it a shot.

… that sure simplified things!

physics.addBody( object, “static”,  { density = 3.0, friction = 1.0, bounce = 0.1 } , {radius=256, isSensor = true})

Thanks, Greg :slight_smile: