How to change PhysicFilter after create body

 Hi everyone.

 I have a problem with Corona’s physic engine. Say I have a character who can move left or right. I want his physic body to respond to his direction. But physic body doesn’t scale or flip with the character. So I created two bodies, one for the LeftDirection and the other for the Right. At first, the character turns left, so I set LeftBody’s filter to Everything (means it can collide with something), and the RightBody’s filter to Nothing (so it can’t collide). When the character turns right, I want to swap those filters, so RightBody can collide and LeftBody can’t. How can I do that?

 Or is there any other method to make a character with multiple bodies which can be turned on/off collision? I can create multiple bodies and detect the element on collision event, but it’s too complicated and I think it causes more work to the system.

 Thanks for reading,

Do you really need filters for this? Why not just create 2 physics bodies and toggle their collision on/off with object.isSensor?

http://docs.coronalabs.com/daily/guide/physics/physicsBodies/index.html#object.isbodyactive

object.isBodyActive is used to set or get the body’s current active state. Inactive bodies are not destroyed, but they are removed from the physics simulation and cease to interact with other bodies. Note that you cannot change the body’s active state during a collision event, so you must queue this event after a slight, imperceptible delay:

Hi @khanh.dq,

The above suggestions will probably solve your issue, but if you need to handle this at a slightly deeper level, you can also use a “preCollision” collision handler and then use the physics contact methods to outright ignore the collision with the object that it’s about to collide with. However, this is more intense, and you should attempt the other suggestions before considering it.

Take care,

Brent

 

 Sorry for the mistake. I want my character has two body fixtures ( or elements ). One for use when it turns to left and the other for the right, because my character doesn’t have horizontal symmentry. It likes this:

physics.addBody( goal, "dynamic", { isSensor=true, filter = everything }, {isSensor = true, filter = nothing} )

 I know about isBodyActive and I’m still using it. But it will affect all body elements, so can’t use it in here.

 

 

 As I known about that, isSensor doesn’t turn off their collisions. It only turns off physically interact, still dispatchs collision event.

 

 

 

 Thanks. But those didn’t :slight_smile:

 As I said, I have a method: mark index for body element, example: 1 for leftbody, 2 for rightbody. On collision event, I only progress collision which has right index element ( 2 if character turn right, and 1 if character turn left ). But it still causes system to detects collision, and makes my code’s too complex to check all collisons. Because I have so much characters, it will be a problem. I take a look at box2d and see some ways to get physic fixture by it’s body element index and change it filter realtime. If I can do it with corona, it will be perfect for me.

 Say that you have 100 characters. Every has two body elements. Maybe like system must checks ( 200*199 / 2 ) collision chances. But if I can turn off a half of them by set their filter to nothing, the system only need to checks ( 100*99 /2 ) collision chances. It isn’t exactly number, but approximately.

 Thanks for all :) . Sorry for my bad English

Hi @khanh.dq,

Thanks for the clarity. Can you possibly upload an image that shows what your object looks like, and what the 2 body parts look like?

Corona doesn’t let you change the collision filter dynamically, but you could accomplish what you need using preCollision. However, there may be an easier way, but I’ll need to see what your object looks like. Also, are you turning or flipping the object when it goes left or right?

Brent

stand_bite_stree.png

 Let’s see that. I want to have two elements. ( The Black for RightDirection, and the Blue for LeftDirection). Of course, when it turns left, I need flip it, but the body doesn’t go with him

Hi @khanh.dq,

For this more advanced collision detection, you may need to use the pre-collision. However, pre-collisions can be “noisy” and the performance might actually be worse than just making your two body parts into sensors and detecting which (by the index) should actually perform an action depending on the direction of the character (meaning, you would still detect all collisions, but you would not take any action unless the character was facing the correct direction).

Anyway, here’s a tutorial on multi-element physics bodies and pre-collision:

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

Also, note that you may encounter some general performance issues if you have 200 physics bodies at one time… that is a heavy load on the engine, no matter how you’re handling collisions.

Take care,

Brent

Thank, Brent.

So I must use multi-element :frowning: . I did it, but I’m still trying to find better way.

My game already has ~ 200 physic bodies, but I use collision filter to limit collision chances, and it’s still running well :slight_smile:

Hi @khanh.dq,

Is there any way you can make the “head” of your character into a polygon that is centered? For example, an octagon? If so, you could center this body within the image (you’d need to increase some transparent padding on the image, but not too much), and then rotate the entire object 180 degrees and flip it vertically. This would make it appear that the character has turned around, and the physics body would continue to be aligned (and it wouldn’t need to be multi-element).

Brent

Oh, that’s a nice trick :slight_smile:
Not all of my characters can use it, but I will try to use it as much as I can.

Can I ask why box 2d doesn’t support us to flip body? And it would be great if Corona provides more things to do with physic.

Do you really need filters for this? Why not just create 2 physics bodies and toggle their collision on/off with object.isSensor?

http://docs.coronalabs.com/daily/guide/physics/physicsBodies/index.html#object.isbodyactive

object.isBodyActive is used to set or get the body’s current active state. Inactive bodies are not destroyed, but they are removed from the physics simulation and cease to interact with other bodies. Note that you cannot change the body’s active state during a collision event, so you must queue this event after a slight, imperceptible delay:

Hi @khanh.dq,

The above suggestions will probably solve your issue, but if you need to handle this at a slightly deeper level, you can also use a “preCollision” collision handler and then use the physics contact methods to outright ignore the collision with the object that it’s about to collide with. However, this is more intense, and you should attempt the other suggestions before considering it.

Take care,

Brent

 

 Sorry for the mistake. I want my character has two body fixtures ( or elements ). One for use when it turns to left and the other for the right, because my character doesn’t have horizontal symmentry. It likes this:

physics.addBody( goal, "dynamic", { isSensor=true, filter = everything }, {isSensor = true, filter = nothing} )

 I know about isBodyActive and I’m still using it. But it will affect all body elements, so can’t use it in here.

 

 

 As I known about that, isSensor doesn’t turn off their collisions. It only turns off physically interact, still dispatchs collision event.

 

 

 

 Thanks. But those didn’t :slight_smile:

 As I said, I have a method: mark index for body element, example: 1 for leftbody, 2 for rightbody. On collision event, I only progress collision which has right index element ( 2 if character turn right, and 1 if character turn left ). But it still causes system to detects collision, and makes my code’s too complex to check all collisons. Because I have so much characters, it will be a problem. I take a look at box2d and see some ways to get physic fixture by it’s body element index and change it filter realtime. If I can do it with corona, it will be perfect for me.

 Say that you have 100 characters. Every has two body elements. Maybe like system must checks ( 200*199 / 2 ) collision chances. But if I can turn off a half of them by set their filter to nothing, the system only need to checks ( 100*99 /2 ) collision chances. It isn’t exactly number, but approximately.

 Thanks for all :) . Sorry for my bad English

Hi @khanh.dq,

Thanks for the clarity. Can you possibly upload an image that shows what your object looks like, and what the 2 body parts look like?

Corona doesn’t let you change the collision filter dynamically, but you could accomplish what you need using preCollision. However, there may be an easier way, but I’ll need to see what your object looks like. Also, are you turning or flipping the object when it goes left or right?

Brent

stand_bite_stree.png

 Let’s see that. I want to have two elements. ( The Black for RightDirection, and the Blue for LeftDirection). Of course, when it turns left, I need flip it, but the body doesn’t go with him

Hi @khanh.dq,

For this more advanced collision detection, you may need to use the pre-collision. However, pre-collisions can be “noisy” and the performance might actually be worse than just making your two body parts into sensors and detecting which (by the index) should actually perform an action depending on the direction of the character (meaning, you would still detect all collisions, but you would not take any action unless the character was facing the correct direction).

Anyway, here’s a tutorial on multi-element physics bodies and pre-collision:

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

Also, note that you may encounter some general performance issues if you have 200 physics bodies at one time… that is a heavy load on the engine, no matter how you’re handling collisions.

Take care,

Brent

Thank, Brent.

So I must use multi-element :frowning: . I did it, but I’m still trying to find better way.

My game already has ~ 200 physic bodies, but I use collision filter to limit collision chances, and it’s still running well :slight_smile:

Hi @khanh.dq,

Is there any way you can make the “head” of your character into a polygon that is centered? For example, an octagon? If so, you could center this body within the image (you’d need to increase some transparent padding on the image, but not too much), and then rotate the entire object 180 degrees and flip it vertically. This would make it appear that the character has turned around, and the physics body would continue to be aligned (and it wouldn’t need to be multi-element).

Brent