Example #1
0
void SpriteContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{

    b2WorldManifold worldManifold;
    contact->GetWorldManifold(&worldManifold);
    b2PointState state1[2], state2[2];
    b2GetPointStates(state1, state2, oldManifold, contact->GetManifold());
    if (state2[0] == b2_addState)
    {
        const b2Body* bodyA = contact->GetFixtureA()->GetBody();
        const b2Body* bodyB = contact->GetFixtureB()->GetBody();

        b2Vec2 point = worldManifold.points[0];
        b2Vec2 vA = bodyA->GetLinearVelocityFromWorldPoint(point);
        b2Vec2 vB = bodyB->GetLinearVelocityFromWorldPoint(point);
        float approachVelocity = abs(b2Dot(vB - vA, worldManifold.normal));

        Sprite* spriteA = (Sprite*)bodyA->GetUserData();
        Sprite* spriteB = (Sprite*)bodyB->GetUserData();
        spriteA->Hit(spriteB, approachVelocity);
        spriteB->Hit(spriteA, approachVelocity);

    }

    return;
}
Example #2
0
	void ContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
	{
		b2WorldManifold worldManifold;
		contact->GetWorldManifold(&worldManifold);
		b2PointState state1[2], state2[2];
		b2GetPointStates(state1, state2, oldManifold, contact->GetManifold());
		if (state2[0] == b2_addState)
		{
			const b2Body* bodyA = contact->GetFixtureA()->GetBody();
			const b2Body* bodyB = contact->GetFixtureB()->GetBody();
			b2Vec2 point = worldManifold.points[0];
			b2Vec2 vA = bodyA->GetLinearVelocityFromWorldPoint(point);
			b2Vec2 vB = bodyB->GetLinearVelocityFromWorldPoint(point);
			b2Vec2 dist = vB - vA;
			worldManifold.normal;
			float32 approachVelocity = b2Dot(dist, worldManifold.normal);
			debug_log("approachVelocity = %f\n", approachVelocity);
			if (abs(approachVelocity) > 2.0f)
			{
				float volume = MIN(abs(approachVelocity)/5.0f, 5.0);
				if (contact->GetFixtureA()->GetPhysicsSprite())
					contact->GetFixtureA()->GetPhysicsSprite()->PlaySound(volume, false);
				//SoundManager::GetInstance().PlaySound("SFX03", MIN(abs(approachVelocity)/5.0f, 5.0f), false);
			}
		}
	}
Example #3
0
void Test::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
	const b2Manifold* manifold = contact->GetManifold();

	if (manifold->m_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->m_pointCount && m_pointCount < k_maxContactPoints; ++i)
	{
		ContactPoint* cp = m_points + m_pointCount;
		cp->fixtureA = fixtureA;
		cp->fixtureB = fixtureB;
		cp->position = worldManifold.m_points[i];
		cp->normal = worldManifold.m_normal;
		cp->state = state2[i];
		++m_pointCount;
	}
}
Example #4
0
/// This is called after a contact is updated. This allows you to inspect a
/// contact before it goes to the solver. If you are careful, you can modify the
/// contact manifold (e.g. disable contact).
/// A copy of the old manifold is provided so that you can detect changes.
/// Note: this is called only for awake bodies.
/// Note: this is called even when the number of contact points is zero.
/// Note: this is not called for sensors.
/// Note: if you set the number of contact points to zero, you will not
/// get an EndContact callback. However, you may get a BeginContact callback
/// the next step.
void PhysicsController::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
	b2ContactListener::PreSolve(contact, 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 && num_contact_points_ < kMaxContactPoints; 
        ++i)
	{
		ContactPoint* cp = &contact_points_[num_contact_points_];
		cp->fixtureA = fixtureA;
		cp->fixtureB = fixtureB;
		cp->position = worldManifold.points[i];
		cp->normal = worldManifold.normal;
		cp->state = state2[i];
		++num_contact_points_;
	}	
}
void HeroContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
	b2WorldManifold wm;
	contact->GetWorldManifold(&wm);
	b2PointState state1[2], state2[2];
	b2GetPointStates(state1, state2, oldManifold, contact->GetManifold());
	if (state2[0] == b2_addState) {
		const b2Body *b = contact->GetFixtureB()->GetBody();
		b2Vec2 vel = b->GetLinearVelocity();
		float va = atan2f(vel.y, vel.x);
		float na = atan2f(wm.normal.y, wm.normal.x);
//		NSLog(@"na = %.3f",na);
		if (na - va > kMaxAngleDiff) {
			hero_->hit();
		}
	}
}
Example #6
0
void Map::PreSolve (b2Contact* contact, const b2Manifold* oldManifold)
{
	b2Fixture *fixtureA = contact->GetFixtureA();
	b2Fixture *fixtureB = contact->GetFixtureB();
	IEntity *entity1 = reinterpret_cast<IEntity*>(fixtureA->GetBody()->GetUserData());
	IEntity *entity2 = reinterpret_cast<IEntity*>(fixtureB->GetBody()->GetUserData());

	if (entity1 == nullptr)
		entity1 = reinterpret_cast<IEntity*>(fixtureA->GetUserData());
	if (entity2 == nullptr)
		entity2 = reinterpret_cast<IEntity*>(fixtureB->GetUserData());

	if (entity1 != nullptr && entity2 != nullptr) {
		entity1->onPreSolve(contact, entity2, oldManifold);
		entity2->onPreSolve(contact, entity1, oldManifold);
	}

	const b2Manifold* manifold = contact->GetManifold();

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

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

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

	for (int32_t i = 0; i < manifold->pointCount && _pointCount < MAXCONTACTPOINTS; ++i) {
		ContactPoint* cp = _points + _pointCount;
		cp->fixtureA = fixtureA;
		cp->fixtureB = fixtureB;
		cp->position = worldManifold.points[i];
		cp->normal = worldManifold.normal;
		cp->state = state2[i];
		cp->normalImpulse = manifold->points[i].normalImpulse;
		cp->tangentImpulse = manifold->points[i].tangentImpulse;
		++_pointCount;
	}
}
	void EmoPhysicsContactListener::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; ++i)
		{
			if (!this->enableNullEvent    && state2[i] == b2_nullState)    continue;
			if (!this->enableAddEvent     && state2[i] == b2_addState)     continue;
			if (!this->enablePersistEvent && state2[i] == b2_persistState) continue;
			if (!this->enableRemoveEvent  && state2[i] == b2_removeState)  continue;
			
			ContactPoint cp;
			cp.fixtureA = fixtureA;
			cp.fixtureB = fixtureB;
			cp.position = worldManifold.points[i];
			cp.normal = worldManifold.normal;
			cp.normalImpulse  = manifold->points[i].normalImpulse;
			cp.tangentImpulse = manifold->points[i].tangentImpulse;
			cp.state = state2[i];
			
			invokeContactEvent(sqvm, cp);
		}
		
	}