Pergunta

I was confused in using contactlistener of box2d.Now I have body1 & body2.When 2 bodies had contact,how can I ensure the body of fixtureA is body1 or body2?Or how can I figure out which body belongs to fixtureA?

I have made the experiment and ensured body of fixtureA is not random.That's all I can ensure now.

Ok.Maybe I should ask in another way.Now I can figure out body of fixtureA is body1 and every time it can be body1.But why?Why not body2?(body1 & body2 are just names as this.No sequence.)

Foi útil?

Solução

You cannot ensure or assume any ordering of fixtures in the contact listener. In a very simple scene usually it can be quite predictable which fixture will be which, but you should not rely on it always being so, rather you should write your program as if the order will be random.

To check what you are dealing with in the contact listener callback you will need to either keep a pointer to the fixture/body when you create it to compare with, or set user data in the fixture/body to check against. You could also do other checks, for example check what kind of shape the fixture has, or what type of body it belongs to... whatever you need to know about the fixture in order to figure out what to do with it, depends on your game.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top