Sensors? Contact listener issue. [SOLVED]

First of all - hello everybody, it's my first post here and I hope it won't be the last 😉


I'm currently developing the Pinball game in C++ using the awesome Box2D v2.1.2. I've got the playfield, realistic ball movement, great spring for shooting the ball with different speeds, cool flippers and special objects. Everything was fine until I've decided to create some fields that should be lit when the ball is over them or to switch them on and off every time the ball passes over them. Also the case of adding a certain amount of score after hitting a specific object is troubling me.

What I need are the sensors probably. I need to recieve somehow the information telling me if the ball passed through a certain shape or not. There will be two situations to solve:

1) Ball passed over the non-colliding shape and something appeared on the map plus score is increased (I will be needing to know if the ball already left the shape or just entered or is currently over the shape).

2) Ball collided with an object and I need to add some points to the score (probably a sensor of a shape of an object is needed, just a bit bigger).

In both cases the ball won't be affected by shapes, just the score and some other static bodies or parts of the code. I browsed through the manual, testbed, this forum's search engine and I just can't understand how to apply sensors at all. Can you help me with that or maybe you can present me a simpler solution? I would really appreciete if someone could present me some kind of a mini tutorial on the sensors implementation (I'm playing with Box2D for only few days so I don't get everything well yet so a beginner-like tutorial with a very simple example would be best). If sensors can solve my case of course.


Hope you can help me up,
h3
You should probably use ray casts to determine when the ball crosses a region. Sensors can miss fast moving objects.

For regular collisions, you don't need sensors. Just use the contact listener.

So it seems like you don't need to use sensors at all.
Thanks for the response Erin. As I've mentioned this is my first time with physics engine and I'm still confused about those manifolds and stuff. Can you (or anybody else) present me some kind of simple tutorial on how to implement the contact listener in Box2D v.2.1.2? From what I understand basing on the manual, old wiki paged and testbed examples:

I need to implement a contact listener. So from the testbed I've got:
Code:
class MyCustomContactListener : public b2ContactListener
{
public:
  void BeginContact(b2Contact* contact)
  { // handle begin event }

  void EndContact(b2Contact* contact)
  { // handle end event }

  void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
  { // handle pre-solve event }

  void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
  { // handle post-solve event }

};

If I get it correctly, this is added to my code and that allowes me to overload the events, right? How do I make my world use it? Am I supposed to define some manifolds (wtf is that?!) and contacts(?) to pass them somewhere? How do I make this contact listener actually work? An example with a simple object would be appreciated so I can see the actual commented code used.

In order not to get lost in thinking, the problem I want to solve:
How do I make one object collide in a given way and second object in other way? Those events from above will be executed for every collision, right? I want some objects to collide in a standard way, like it is done automatically now (walls, simple obstacles, flippers) and other objects to make ball bounce and allow for adding score or any other piece of a code to be executed on collision or on passing through the given shape.


EDIT:

I actually managed to make this listener compile. I've added to my code the contact listener like that:
Code:
class MyContactListener : public b2ContactListener
{
public:

   MyContactListener();

   void* userData;

   void BeginContact(b2Contact* contact) {
      printf("Collision");
   }

   void EndContact(b2Contact* contact) {
   }
};


And to my main.cpp after the definition of my world:
Code:
   b2Vec2 gravity(0.0f, -10.0f);
   bool doSleep = true;
   b2World world(gravity, doSleep);

   //contact listener
   MyContactListener* pleaseWork;
   world.SetContactListener(pleaseWork);


If my little brain gets it correctly, now this custom contact listener should be used instead of the default one that works automatically. It should print "Collision" every time the collision begins but when I launch my game still the old contact listener is in use and nothing prints to console. What should I do more to make this work? Should I define the objects in some other way or add them some attributes to make them use my contact listener?
Ok guys, thanks for help anyway but I solved the problem myself thanks to the nice tutorial I've just found. If anyone in future will be facing the misunderstanding connected with the contact listener that can differentiate between objects, please refer to this tutorial:

http://www.raywenderlich.com/475/how-to ... al-part-12

It has two parts. First will help you design the objects so the contact listener can read them and the second part is about implementation of contact listener itself. Remember that this tutorial is a guidline and not pure copy-paste since the cocos2d used there and the iPhone target, but the concepts are well explained.

🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=5383