Do I need to add a collision event listener to both a moving object and its target?

Hi guys,

I start using physics ( mostly for collision detection and to move bunch of objects toward couple targets using impulses) The target are fix in position ( but they can rotate)

Anyway my question is: do I need to set a local collision event listerner for both the moving objects and the fixed targets? Right now Iam spawning bunch of objects (dynamic) that need to collide with the targets. I only have a collision listerner attached to each of those objects but none to the targets. It seems to work ok ( to a point since if the spawn rate is little higher the objects go through the targets...another issue I think)

Is this is the correct way to proceed or do I need to attach an event listener to the targets as well? In that case what do I need to put in that event listerner function? I am already dealing with the collision to the target on the moving objects event listerner function. It seems redandant to have both moving objects and the targets to have each a collision event listerner? Maybe I am wrong and that is why sometimes objects are going through the static targets

As you can see I fully confused!

Thanks for any pointers.

Mo

Ps: if I need only need to attach event listerners to one body, maybe I should attach it to the non moving targets since they are a fix ( and low number of them during the game play) unlike the moving objects ( you can view them as some sort of bullets) are spawned all during the game. And remove at impact with the fix targets....just curious what you think. Maybe attaching event listerners to the targets ( max of 3) would be less costly in term of physics since the engine will only need to do collision detection on maximum 3 objects.... Thanks.

Yes you do.

My suggestion is to use one collision function to handle all objects. You differentiate between objects using id's

1
2
3
4
5
6
7
8
9
10
11
12
13
myImage.id = "myImg"
otherImage.id = "otherImg"
 
local function onCollision(event)
        local obj1 = event.object1
        local obj2 = event.object2
        
        if event.phase == "began" then
                if obj1.id == "myImg" and obj2.id == "otherImg" then
                        --Do something
                end
        end
end

Thank so much Danny for taking the time. I did not know I had to attach a collision listener to each objects I want to check for collision. It seems to "work" with one listener attached to one object and not the other. I was still having problem with bullets going through targets (no collision) if the bullets spwaning rate was say 1 every 1000-500 ms...I think I read something about some issues like that on the forum.

In any event, I am curious, how object1 and object2 are set? I mean, is for instance the first object that collide with another is automatically labeled object1 and the other object2?

The reason I ask, is that I am using a table of of objects (let's called them targets and bullets)

Targets are fixed on the screen and get hits by bullets who actually moves (toward the targets) In my old code (where I had attached a listener to bullets) I could use "event.other" and "self" since i knew that self was always referring to the bullets (since it has the listener attach to it) and the event.other was the targets.

This is important to me because I need to be doing things like a setting different type of explosions (Particle Candy) at the location of either the bullet or target (bullet is not really the correct word here but it is simply to suggest something that moves toward a target..)

I guess my question is how would you approach something like this where object1 (or object2 for that matter) can be either a target or bullet?

Sorry if I confused you. I think I confused myself!

Thank you again.

Mo

views:1461 update:2012/1/3 13:02:13
corona forums © 2003-2011