コード例 #1
0
void CRigidBody::UpdateCollisionReaction()
{
	CVehicle* Car = (CVehicle*)this;

	Vector3f OldPosition = m_vPosition;

	// update reflection motion
	m_vReflection *= RIGIDBODY_REFLECTION_ENTROPY_FACTOR;
	m_vPosition = m_translate + m_vReflection;
	m_vDirectionWhenDisturbed = m_vPosition - OldPosition;
	m_translate = m_vPosition;
	m_box.Center() = m_vPosition;
	m_sphere.Center() = m_vPosition;

	Car->SetVehiclePositionLC(Vector3f(m_vPosition.X(), m_vPosition.Z(), m_vPosition.Y()));

	// update angular velocity
	m_vRotation *= RIGIDBODY_ROTATION_ENTROPY_FACTOR;
	Car->GetRotationLC().Z() += m_vRotation.Y();

	// see if reflection has dissipated enough to stop
	if (m_vReflection.Length() < RIGIDBODY_REFLECTION_DEATH_FACTOR) {
		disturbed = false;
	}

}
コード例 #2
0
void CRigidBody::DeliverCollisionMessage(CCollisionMessage* ColMsg)
{
	if (!ColMsg) return;

	if (ColMsg->GetCollisionType() == SPHERE_TO_SPHERE) {
		HandleSphereToSphereCollision(ColMsg);
		return;
	}

	/****** Deploy to HandlePushCollision() if this is the case ******/
	// Push collision not working. Commenting this out for now.
	/*	if (ColMsg->GetCollisionType() == PUSHED) {
		HandlePushCollision(ColMsg);
		return;
	}
*/ 

	CVehicle* Car = (CVehicle*)ColMsg->GetEntity();

	/************** set reflection motion ************/

	m_vPosition = m_translate + (*ColMsg->GetReverse())*RIGIDBODY_SPACING_FACTOR;

	Vector3f velocityWC;
	if (disturbed) {
		velocityWC = m_vDirectionWhenDisturbed*80.0f;
	}
	else {
		velocityWC = Car->GetVehicleVelocityWC();
		// if reversing, velocityWC will point in opposite direction as actual velocity
		if (Car->GetVehicleVelocityLC().X() < 0.0f)
			velocityWC *= -1.0f;
	}

	m_vReflection = velocityWC - 2.0f*(velocityWC.Dot(*ColMsg->GetNormal()))*(*ColMsg->GetNormal());
	m_vReflection *= RIGIDBODY_REFLECTION_FACTOR;
	m_vDirectionWhenDisturbed = m_vReflection;

	Car->SetVehicleVelocityLC(0.0f);
	Car->SetVehiclePositionLC(Vector3f(m_vPosition.X(), m_vPosition.Z(), m_vPosition.Y()));

	/*************** set angular velocity ******************/

	// Compute angle between vehicle heading and plane
	Vector3f PlaneEdge = ColMsg->GetPlane()->Edge0();
	Vector3f CarHeading = Car->GetVehicleHeadingWC();
	float angle = acos(CarHeading.Dot(PlaneEdge)/(PlaneEdge.Length()*CarHeading.Length()));

	// spin CW or CCW?
	float spin; // magnitude of spin about the y-axis
	if (angle > Math<float>::PI/2.0f) { // CCW
		spin = Math<float>::PI - angle;
	}
	else { // CW
		spin = -angle;
	}
	spin *= RIGIDBODY_SPIN_FACTOR;
	m_vRotation = Vector3f(0.0f, spin, 0.0f);

	/*************** set vectors that are actually used in renderer ************/
	/*********************** to the values computed above **********************/

	m_translate = m_vPosition;
	m_box.Center() = m_vPosition;
	m_sphere.Center() = m_vPosition;
	Car->GetRotationLC().Z() += spin;

	disturbed = true;

	/*
	CLog::GetLog().Write(LOG_MISC, "\n\n\nreflection = (%f, %f, %f)",
		m_vReflection.X(), m_vReflection.Y(), m_vReflection.Z());
	CLog::GetLog().Write(LOG_MISC, "direction = (%f, %f, %f)\n\n",
		m_vDirectionWhenDisturbed.X(), m_vDirectionWhenDisturbed.Y(), m_vDirectionWhenDisturbed.Z());
*/
}