Collision Basics

I’m trying to set up a simple game where I have several objects on screen that I want to be able to attack. They don’t move. I have one object, the attacker that I will touch, drag to one of the targets and destroy. Once I let up from the touch, I will to a transition.to(attacker, {x = event.x, y = event.y, time=1000}).

I have all of this working. The attacker moves to where I have told it to go to. But I’m not detecting that it collided with the destination (or any other targets in between). The event handler has a print() at the top of it and I’m not getting any output.

I was hoping I could do this without the physics engine, just having the transition.to interrupted and trip my event handler (I don’t need physics for the game)

But I’ve added physics:

[blockcode]
local physics = require(“physics”)
physics.start()

function onAttackerCollision( self, event )
print("collision detected between " … self.myName … " and " … event.other.myName)
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

attacker = display.newImage(“attacker.png”);
attacker.x = display.contentWidth - 15;
attacker.y = math.random((display.contentHeight - 45)) + 45;
physics.addBody( attacker, “static”, { density=1.6, friction=1.0, bounce=0.0 } )
attacker:addEventListener( “touch”, onArmyTouch )
attacker.collision = onAttackerCollision
attacker:addEventListener( “collision”, attacker)
attacker.myName = “attacker”
[/blockcode]

After looking at various forum posts and reading through the docs more and more, I ended up adding the targets in as well:

[blockcode]
target = display.newImage(“target.png”);
– has to be on left third of the board and on screen
target.x = math.random((display.contentWidth / 3) - target.width) + target.width;
target.y = math.random((display.contentHeight - 30) - target.height) + target.height;
target.myName = “target”
physics.addBody( target, “static”, { density=1.6, friction=1.0, bounce=0.0, radius=24, isSensor = true } )
[/blockcode]

so I’m not sure why this isn’t working. I’ve not tried turning the attacker into a bullet yet, but my event handler isn’t firing.

Any ideas?
[import]uid: 19626 topic_id: 6281 reply_id: 306281[/import]

Unless I am missing something,

[lua]attacker.collision = onAttackerCollision
attacker:addEventListener( “collision”, attacker)[/lua]

(on line 20) should be changed to

[lua]attacker:addEventListener( “collision”, onAttackerCollision)[/lua]

[import]uid: 7581 topic_id: 6281 reply_id: 21637[/import]

I’ve noticed that collisions are only reported when at least one of the objects has a body type of “dynamic”.
So in your case, try making your attacker “dynamic” rather than “static”. [import]uid: 32962 topic_id: 6281 reply_id: 21673[/import]

or just use isSensor=true [import]uid: 22024 topic_id: 6281 reply_id: 21700[/import]

as soon as I make attacker dynamic, it falls off the screen. I set its density to 0.0 and its friction to 1.0 and bounce to 0.0 to try and make it stick. I guess I’ll need to also set gravity to 0.0.
As for the isSensor option, I’ve already tried that with “target”, no effect. [import]uid: 19626 topic_id: 6281 reply_id: 21704[/import]

@mappum, I would think your way should be the right way too (and I tried it). Most of the examples in the forum and in the docs does it the way I currently have it.

Rob [import]uid: 19626 topic_id: 6281 reply_id: 21706[/import]

I have it detecting the collision. I had to setGravity to 0,0 and make attacker dynamic.

Now my next issue is in the event handler for the collision I’ve added:

[blockcode]
elseif ( event.phase == “ended” ) then
print( self.myName … ": collision ended with " … event.other.myName )
transition.cancel(attacker)
end
[/blockcode]

But the transition continues past the object it collided with. I see the message above, so I know its getting to that block of code. I also tried: transition.cancel(self).

Ideas?
[import]uid: 19626 topic_id: 6281 reply_id: 21717[/import]