listener conflict ?

I have an array of players (10) and a ball.

I have a listener on the ball to detect collisions with players and the sidelines, the net etc…

Then I decided i would to give a bit of graphic effect if two players collide. So i’ve added another listener on the players as well. like this:

theball.collision = on\_ball\_collision theball:addEventListener("collision", theball ) for lip = 1,max\_players do       players[lip]:addEventListener("collision", on\_player\_collision )       players[lip]:addEventListener("tap",tap\_players) end Runtime:addEventListener("enterFrame",everyframe)

after I added the ‘on_player_collision’ listener I have the two bizarre and unwanted effects:

  1. the ‘on_ball_collision’ listener doesnt fire anymore

  2. the ‘on_player_collision’ only detects the collisions with the ball, NOT with the other players 

  3. Note that at this stage the ‘on_player_collision’ does nothing, just print what collides with what.

as soon as I switch the ‘on_player_collision’ listener off the ‘on_ball_collision’ works again.

I read a few forums about collisions and saw Brent answers about “better to use a global on so many objects”.

but 10 is not ‘so many’, is it ?

I have isolated the very same physic objects and listener in a little program (without the enterframe) and it seems to be working fine so i’m confused. 

help !

thanks

Ed

I would probably use a global collision listener myself,  but what kinds of objects are you dealing with? Collisions only generate with certain combination body types.

Rob

Aaaah. Previously I said that I isolated the code with ‘the very same physics objects’ but actually it wasn’t the truth. I just did a a few quick and dirty squares and a ball. 

So after your answer i really put the same objects … and …  this time the unwanted behaviour happened too.

More stripping down to remove the PE definition etc… and then I read the addBody documentation  … 

_ “static” —  Static bodies collide only with dynamic bodies, not with other static bodies or kinematic bodies. _

That is something i didn’t really understand previously … the ball is dynamic but my players are static objects (as I dont want them bouncing around !) so they wont generate a collision between them. Darn !

Is there a way around this ?

I tried to declare my players as 'Dynamic" + isSensor=true  but then they fall down through everything ! 

You can turn the gravity off on each player.

Rob

Thanks Rob, i tried that but then the ball is sending the players through the sky like snooker balls ! 

and if i declare them sensors then the ball is getting through and I have to simulate bouncing all on my own !

Ive been playing a bit with the physical parameters but unless i declare a density of 1000 they still get nudged around.

and even with 1000 they still move a bit when the ball hits them.

pe_fixture_id = “body”, density = 1000, friction = 10, bounce = 1, 

filter = { categoryBits = 1, maskBits = 65535, groupIndex = 0 },

shape = {   1.5, 85.5  ,  -0.5, -35.5  ,  39.5, -45.5  ,  71.5, 34.5  }

or am I not declaring something correctly ?

Physics isn’t my strong suit by any means.  What are your physics properties for the balls?  If you’re players are supposed to be human, you probably should set your density and bounce to be what a human would be.  I would look through this guide:

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html#object.isfixedrotation

And see if something like .isFixedRotation something that might help.

Rob

You should make the player’s bodyType “kinematic” and set ‘isSleepingAllowed’ on the player to false. 

This way the player’s display object can be moved around by manually setting x and y values, and the body will follow.  It will also collide with other objects, but not bounce or be pushed around.

roaminggamer it works !

but but but … the doco says … 

“kinematic” — kinematic bodies move under simulation only according to their velocity. Kinematic bodies will not respond to forces like gravity. They can be moved manually by the user, but normally they are moved by setting their velocities. Kinematic bodies collide only with dynamic bodies, not with other kinematic bodies or static bodies.

can you explain WHY it works ? also why the ‘isSleepingAllowed’ = false

impressed and delighted yet confused.

I’ll try…  let’s go through the body types.

"static" - This body is placed once and assumed to never move.  So moving it is a bad idea.  It may work, but I wouldn’t rely on the physics body moving with the visible part (the display object).

"kinematic" - As you noted, these bodies can move at a fixed velocity, but they do not respond to forces nor do they exhibit a collision response.  That is objects colliding with them do not have any impact on the kinematic object’s body motion. 

Additionally, although it is generally frowned upon (although sometimes it is the only solution), you can move the display object by changing the x and y values.  

The reason setting x,y is frowned upon is because it moves the display object only.  The physics engine must detect this and adjust.  It isn’t the same as the body being moved by the physics system.  

By default, physics bodies are allowed to sleep.  This reduces the processing load of the physics engine.

If the body falls asleep and you manually update the display object position, the body may be stuck in the old position.

When you know you may be manually adjusting the position of kinematic bodied display object, you should set obj.isSleepingAllowed = false.  i.e. Don’t go to sleep.

Warning: DO NOT do this on all objects or your performance may tank.

"dynamic" - This is the most common body type.  However because they respond to forces and exhibit collision responses, they are not suitable for things like pong paddles, etc.  i.e. Objects that you wish to have NOT move on collision simply cannot be dynamic.

thank you that was very useful.

now I don’t really want Corona developers throwing stones at me in the streets of Sydney simply because I set x and y on a kinetic object so how would YOU do it ?

as in a few volleyball players on a 2d field (vertical where the gravity changes everytime one side touches the ball) where most collisions occur between the ball and the players. but sometimes 2 players collide and I would like to make them fall over

Hi.  Sorry, but I have very limited time this week so I’m going to have to let someone else answer your extended question.

I will say however, “experiment and find what works best for you.”  The beauty of Corona is it is flexible.  

no no thank you ! I was not expecting you to develop code for me ! :slight_smile:

After my first post i did manage to get it to work by declaring another object/player that’s dynamic and that follows the static. so it would detect other players. very ugly !

so I’ll stick to the insomniac kinematic object !  

thank you again.

Hi.  I wasn’t implying that you wanted me to code it for you.  :)  I should have been more clear.  

I think that the solution will be somewhat custom to your situation and thus any advice I might give on specific implementation may be counter-productive.  However, if someone who better understood your question or had better insights wanted to answer, then they should.

Thanks,

Ed

PS - Also, remember, that I said moving manually is frowned upon, but in fact that is sometimes the exact right solution.  As I have said in the past, “The right solution is the one that you understand and that works best for the problem you are trying to solve.”

I would probably use a global collision listener myself,  but what kinds of objects are you dealing with? Collisions only generate with certain combination body types.

Rob

Aaaah. Previously I said that I isolated the code with ‘the very same physics objects’ but actually it wasn’t the truth. I just did a a few quick and dirty squares and a ball. 

So after your answer i really put the same objects … and …  this time the unwanted behaviour happened too.

More stripping down to remove the PE definition etc… and then I read the addBody documentation  … 

_ “static” —  Static bodies collide only with dynamic bodies, not with other static bodies or kinematic bodies. _

That is something i didn’t really understand previously … the ball is dynamic but my players are static objects (as I dont want them bouncing around !) so they wont generate a collision between them. Darn !

Is there a way around this ?

I tried to declare my players as 'Dynamic" + isSensor=true  but then they fall down through everything ! 

You can turn the gravity off on each player.

Rob

Thanks Rob, i tried that but then the ball is sending the players through the sky like snooker balls ! 

and if i declare them sensors then the ball is getting through and I have to simulate bouncing all on my own !

Ive been playing a bit with the physical parameters but unless i declare a density of 1000 they still get nudged around.

and even with 1000 they still move a bit when the ball hits them.

pe_fixture_id = “body”, density = 1000, friction = 10, bounce = 1, 

filter = { categoryBits = 1, maskBits = 65535, groupIndex = 0 },

shape = {   1.5, 85.5  ,  -0.5, -35.5  ,  39.5, -45.5  ,  71.5, 34.5  }

or am I not declaring something correctly ?

Physics isn’t my strong suit by any means.  What are your physics properties for the balls?  If you’re players are supposed to be human, you probably should set your density and bounce to be what a human would be.  I would look through this guide:

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html#object.isfixedrotation

And see if something like .isFixedRotation something that might help.

Rob

You should make the player’s bodyType “kinematic” and set ‘isSleepingAllowed’ on the player to false. 

This way the player’s display object can be moved around by manually setting x and y values, and the body will follow.  It will also collide with other objects, but not bounce or be pushed around.

roaminggamer it works !

but but but … the doco says … 

“kinematic” — kinematic bodies move under simulation only according to their velocity. Kinematic bodies will not respond to forces like gravity. They can be moved manually by the user, but normally they are moved by setting their velocities. Kinematic bodies collide only with dynamic bodies, not with other kinematic bodies or static bodies.

can you explain WHY it works ? also why the ‘isSleepingAllowed’ = false

impressed and delighted yet confused.