b2GetPointStates

Hi,

What is the practical use of b2GetPointStates? Also, what is the *oldManifold passed into the PreSolve event, and when would you want to access it?

I am seeing the pre-solve event firing continuously and am wondering if these are the key to processing the post solve event only once per collision between two bodies.



Thanks

i
From the testbed:
Code:
void Test::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
   const b2Manifold* manifold = contact->GetManifold();

   if (manifold->pointCount == 0)
   {
      return;
   }

   b2Fixture* fixtureA = contact->GetFixtureA();
   b2Fixture* fixtureB = contact->GetFixtureB();

   b2PointState state1[b2_maxManifoldPoints], state2[b2_maxManifoldPoints];
   b2GetPointStates(state1, state2, oldManifold, manifold);

   b2WorldManifold worldManifold;
   contact->GetWorldManifold(&worldManifold);

   for (int32 i = 0; i < manifold->pointCount && m_pointCount < k_maxContactPoints; ++i)
   {
      ContactPoint* cp = m_points + m_pointCount;
      cp->fixtureA = fixtureA;
      cp->fixtureB = fixtureB;
      cp->position = worldManifold.points[i];
      cp->normal = worldManifold.normal;
      cp->state = state2[i];
      ++m_pointCount;
   }
}


You can use the state of a contact point for game play logic. For example, you may only want to play collision sounds on new contact points.
Hi Erin,

Not sure I understand the codes purposes, or how the state gets set.

Does the state get set automatically, and what state transitions are there? Or do I set the state, i.e. after I have proccessed something in PreSolve set it to b2_Persist myself? How would you use it to process the collision just once?

Code:

   // only proceed if this collision hasnt been processed before
      for( int i=0; i<kMaxContactPoints;i++ ) {
         ContactPoint* cp = contactPoints + i;
         
         if( cp && cp->otherFixture == otherFixture ) {
            b2Vec2 point = worldManifold.points[0];
            b2Vec2 va = bodyA->GetLinearVelocityFromWorldPoint(point);
            b2Vec2 vb = bodyB->GetLinearVelocityFromWorldPoint(point);
            float32 approachVelocity = b2Dot(vb - va, worldManifold.normal);
            
            if(cp->state == b2_addState) {
               if (approachVelocity>1.f) {
                  CCLOG(@"approachVelocity calculated to be > 1: %f", approachVelocity);
               } else {
                  CCLOG(@"approachVelocity calculated to be <= 1: %f", approachVelocity);
                  
               }
                                        // begingContact I set this to b2_AddState....but setting it to persist so I can ignore it doesnt feel right?
               cp->state = b2_persistState;
               
            }
            break;
         }
      }



What is the purpose of the code sample above, it just seems to be filling a pre-existing array of type ContactPoints with information about the current contacts, but then doesn't perform any action? Also, whats the purpose of old and new manifold, and is the sequence important?

Regards (and thanks)

i
OK,

I sort of get it...you should set the state yourself you have to call b2GetPointStates yourself....

My solution is below. My question now is why do we create a state1 and state2 which hold only 2 elements:

Code:
b2PointState state1[b2_maxManifoldPoints], state2[b2_maxManifoldPoints];


...but later we iterate as far as potentially 32 points into the array (whatever kMaxContactPoints is set to)....this is very similar to your code sample Erin?

Code:

for( int i=0; i<kMaxContactPoints;i++ ) {
         ContactPoint* cp = contactPoints + i;
         
         if( cp && cp->otherFixture == otherFixture ) {
            

            // HOW CAN WE BE INDEXING Into state2[i] HERE WHEN WE DEFINED IT AS ONLY HOLDING 2 ITEMS????
            if(state2[i] == b2_addState) {  //??????????????????????     i could be 32, 64??


               if (approachVelocity>1.f) {
                  CCLOG(@"approachVelocity calculated to be > 1: %f", approachVelocity);
               } else {
                  CCLOG(@"approachVelocity calculated to be <= 1: %f", approachVelocity);
                  
               }
               
               
            }
            break;
         }
      }






My final code here...


Code:
-(void) preSolveContact:(b2Contact*)contact  manifold:(const b2Manifold*) oldManifold
{
//   CCLOG(@"Inside Heroegg preSolveContact");
   b2WorldManifold worldManifold;
   contact->GetWorldManifold(&worldManifold);
   b2Fixture *fixtureA = contact->GetFixtureA();
   b2Fixture *fixtureB = contact->GetFixtureB();
   NSAssert( fixtureA != fixtureB, @"preSolveContact: BOX2D bug");
   
   b2Body   *bodyA = fixtureA->GetBody();
   b2Body   *bodyB = fixtureB->GetBody();
   NSAssert( bodyA != bodyB, @"preSolveContact: BOX2D bug");
   
   BodyNode *dataA = (BodyNode*) bodyA->GetUserData();
   BodyNode *dataB = (BodyNode*) bodyB->GetUserData();
   
   
   // Box2d doesn't guarantees the order of the fixtures
   BOOL heroIsA = (bodyA == body_) ? YES : NO;
   b2Fixture *otherFixture = heroIsA ? fixtureB : fixtureA;
   
   
   // check if the other fixture is a one-sided platform.
   
   Class p1 = [Platform1 class];
   if( [dataA isKindOfClass:p1] ||
      [dataB isKindOfClass:p1] ) {
      
      
      // check for normal
      if( (!heroIsA && worldManifold.normal.y < 0) || (heroIsA && worldManifold.normal.y > 0) )
      {
         contact->SetEnabled(false);
         
      } else {
         // update contact. Why ?
         // Because if the sprite is passing through a "one sided platform" probably it will
         // have a normal.y < 0, but after passing through, the normal.y won't be < 0 anymore since it
         // has already passed trhough, and the "jump" logic depends on the normal
         for( int i=0; i<kMaxContactPoints;i++ ) {
            ContactPoint* point = contactPoints + i;
            if( point && point->otherFixture == otherFixture ) {
               point->normal = heroIsA ? -worldManifold.normal : worldManifold.normal;
               break;
            }
         }
      }
   } else {


              // SOLUTION HERE....CHECK APPROACHING VELOCITY BUT ONLY DO IT ONCE DURING THE ADD STATE

      
      b2Vec2 point = worldManifold.points[0];
      b2Vec2 va = bodyA->GetLinearVelocityFromWorldPoint(point);
      b2Vec2 vb = bodyB->GetLinearVelocityFromWorldPoint(point);
      float32 approachVelocity = b2Dot(vb - va, worldManifold.normal);
      
      b2PointState state1[b2_maxManifoldPoints], state2[b2_maxManifoldPoints];
      
      const b2Manifold* manifold = contact->GetManifold();
      
      b2GetPointStates(state1, state2, oldManifold, manifold);
      

      
      // only proceed if this collision hasnt been processed before
      for( int i=0; i<kMaxContactPoints;i++ ) {
         ContactPoint* cp = contactPoints + i;
         
         if( cp && cp->otherFixture == otherFixture ) {
                        
            if(state2[i] == b2_addState) {
               if (approachVelocity>1.f) {
                  CCLOG(@"approachVelocity calculated to be > 1: %f", approachVelocity);
               } else {
                  CCLOG(@"approachVelocity calculated to be <= 1: %f", approachVelocity);
                  
               }
               
               
            }
            break;
         }
      }
      
      

   
   }

   
}




Same in your sample code

Code:
for (int32 i = 0; i < manifold->pointCount && m_pointCount < k_maxContactPoints; ++i)
   {
      ContactPoint* cp = m_points + m_pointCount;
      cp->fixtureA = fixtureA;
      cp->fixtureB = fixtureB;
      cp->position = worldManifold.points[i];
      cp->normal = worldManifold.normal;

     // COULDNT i REACH VERY HIGH VALUES COMPARED TO THE state2 ARRAY SIZE???
      cp->state = state2[i];
      ++m_pointCount;
   }


Regards

i
No, in my code i < manifold->pointCount. For a single manifold the point count is 0, 1, or 2.

If you don't understand some code, you should step through it in the debugger and see how the variables change.
Hi Thanks

So whats this additional condition below...just in case k_maxContactPoints is lower than m_pointCount? i.e. kMaxContactPoints is 1, where m_pointCount is 2? kMaxContactPoints is generally always larger than 0,1,2 to be useful isnt it?

Code:
&& m_pointCount < k_maxContactPoints


Regards

i
No, this is basic programming. I have a large array of contact points that I'm filling from many small arrays of points from each manifold. The check prevents me from overflowing the large array.

Suppose I have 200 manifolds each with 2 points. If k_maxContactPoints is 50, I would overflow m_points without that check.
Aaaahhh....so the for loop only gets executed once or twice each preSolve (based on the points in the manifold), but the loop itself gets called once for every preSolve called for each contact, and you increment m_pointCount in the loop ready for the next call to preSolve - rather than for the next iteration of the loop...and you reset m_pointCount before the next call to world->Step...?

One last question (thanks for you patience by the way)...if there are two points in the manifold, doesnt the for loop go round twice, in which case are you processing two separate contact points in the for loop, (ContactPoint* cp = m_points + m_pointCount;)
when I thought you would want to just be processing both points in the same manifold across both iterations of the for loop?
indy2005 wrote:
Aaaahhh....so the for loop only gets executed once or twice each preSolve (based on the points in the manifold), but the loop itself gets called once for every preSolve called for each contact, and you increment m_pointCount in the loop ready for the next call to preSolve - rather than for the next iteration of the loop...and you reset m_pointCount before the next call to world->Step...?


Yup, it is all in there. Try stepping through with a debugger.

indy2005 wrote:
One last question (thanks for you patience by the way)...if there are two points in the manifold, doesnt the for loop go round twice, in which case are you processing two separate contact points in the for loop, (ContactPoint* cp = m_points + m_pointCount;)
when I thought you would want to just be processing both points in the same manifold across both iterations of the for loop?


The following code loops over all points in a single manifold. There can be 0, 1, or 2 points. The looping terminates early if m_points becomes full.

Code:
for (int32 i = 0; i < manifold->pointCount && m_pointCount < k_maxContactPoints; ++i)
   {
      ContactPoint* cp = m_points + m_pointCount;
      cp->fixtureA = fixtureA;
      cp->fixtureB = fixtureB;
      cp->position = worldManifold.points[i];
      cp->normal = worldManifold.normal;
      cp->state = state2[i];
      ++m_pointCount;
   }


Again, none of this is Box2D specific. This is just basic programming. I recommend taking a break from Box2D and working through a couple books on programming. You should start with a book on C programming (Ritchie) and then read a book on C++ programming (Stroustrup). You also appear to be doing some Objective C programming, I recommend reading Xcode 3 Unleashed to learn how to develop and debug in Xcode.
Thanks,

I think I get it now...you are peeling off the points from each manifold and storing them in an array of contact points for processing where needed...

I cant just learn C++ for the sake of it, I have to have 'something to do" in order to learn. Its not so much the code which is throwing me, but largely the box2D specifics of contact points, manifolds and the collision handler....This is whats hindering me more than the C++ (honestly).

As an example of one area of confusion, if two polygon shapes collide I understand that 2 contact points and one normal are created. In b2GetPointStates there seems to be some checking of the old manifolds points and the new manifolds points, to check to see if any of the points persist or not....but in my mind (!), this manifold wont "lose a point", as long as the shapes are in contact there should always be 2 points and one normal....one point shouldnt dissapear from the manifold?? Also, the concept of passing in the oldManifold....if this is the first collision of the two shapes, where does the oldManifold come from that gets passed in to b2GetPointStates??

Also, the method getPointStates seems to actually "set" the states as part of the process (rather than just get) and this threw me. I thought perhaps the contact point states would have been set as part of normal collision processing, but it seems that unless you call b2GetPointStates this doesnt happen...?

I guess any one contact point can be part of more than one manifold?

A lot of Box2D is still mysterious, even with the great samples and docs!

Regards

i

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