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