virtual void do_simulation_controller(IVP_Event_Sim *event,IVP_U_Vector<IVP_Core> *core_list)
	{
		int i;
		for( i = core_list->len()-1; i >=0; i--) 
		{
			IVP_Core *pCore = core_list->element_at(i);

			IVP_Real_Object *pivp = pCore->objects.element_at(0);
			CPhysicsObject *pPhys = static_cast<CPhysicsObject *>(pivp->client_data);
			
			float dragForce = -0.5 * pPhys->GetDragInDirection( pCore->speed ) * m_airDensity * event->delta_time;
			if ( dragForce < -1.0f )
				dragForce = -1.0f;
			if ( dragForce < 0 )
			{
				IVP_U_Float_Point dragVelocity;
				dragVelocity.set_multiple( &pCore->speed, dragForce );
				pCore->speed.add( &dragVelocity );
			}
			float angDragForce = -pPhys->GetAngularDragInDirection( pCore->rot_speed ) * m_airDensity * event->delta_time;
			if ( angDragForce < -1.0f )
				angDragForce = -1.0f;
			if ( angDragForce < 0 )
			{
				IVP_U_Float_Point angDragVelocity;
				angDragVelocity.set_multiple( &pCore->rot_speed, angDragForce );
				pCore->rot_speed.add( &angDragVelocity );
			}
		}
	}
	void UpdateSleepObjects( void )
	{
		int i;

		CUtlVector<CPhysicsObject *> sleepObjects;

		for ( i = 0; i < m_activeObjects.Count(); i++ )
		{
			CPhysicsObject *pObject = m_activeObjects[i];
			
			if ( pObject->GetSleepState() != OBJ_AWAKE )
			{
				sleepObjects.AddToTail( pObject );
			}
		}

		for ( i = sleepObjects.Count()-1; i >= 0; --i )
		{
			// put fully to sleep
			sleepObjects[i]->NotifySleep();

			// remove from the active list
			DeleteObject( sleepObjects[i] );
		}
	}
bool CPlayerController::IsInContact( void )
{
	IVP_Real_Object *pivp = m_pObject->GetObject();
	if ( !pivp->flags.collision_detection_enabled )
		return false;

	IVP_Synapse_Friction *pfriction = pivp->get_first_friction_synapse();
	while ( pfriction )
	{
		extern IVP_Real_Object *GetOppositeSynapseObject( IVP_Synapse_Friction *pfriction );

		IVP_Real_Object *pobj = GetOppositeSynapseObject( pfriction );
		if ( pobj->flags.collision_detection_enabled )
		{
			// skip if this is a static object
			if ( !pobj->get_core()->physical_unmoveable && !pobj->get_core()->pinned )
			{
				CPhysicsObject *pPhys = static_cast<CPhysicsObject *>(pobj->client_data);
				
				// skip if this is a shadow object
				// UNDONE: Is this a hack? need a better way to detect shadows?
				if ( !(pPhys->GetCallbackFlags() & CALLBACK_SHADOW_COLLISION) )
					return true;
			}
		}

		pfriction = pfriction->get_next();
	}

	return false;
}
Exemplo n.º 4
0
void GameObject::tick() {

	if (!m_HasStarted) {
		start();
		m_HasStarted = true;
	}
	
	if (m_GoToNewPoint) {
		
		CPhysicsObject* physicsComponent = (CPhysicsObject*)getComponent(CPhysicsObject::classTypeID());
		if (physicsComponent != 0) {
			b2Body* body = physicsComponent->getBody();
			float angle = body->GetAngle();
			body->SetTransform(m_NewPoint, angle);
		}
		
		m_GoToNewPoint = false;
	}

	for (map<int, Component*>::iterator it = m_Components->begin(); 
		it != m_Components->end(); 
		it++) {
		Component* component = it->second;
		component->tick();
	}

	for (set<GameObject*>::iterator it = m_Children->begin(); 
		it != m_Children->end(); 
		it++) {
		GameObject* child = *it;
		child->tick();
	}
}
Exemplo n.º 5
0
CPhysicsObject* CreatePhysicsSphere(CPhysicsEnvironment *pEnvironment, float radius, int materialIndex, const Vector &position, const QAngle &angles, objectparams_t *pParams, bool isStatic) {
	btSphereShape* shape = new btSphereShape(ConvertDistanceToBull(radius));
	
	btVector3 vector;
	btMatrix3x3 matrix;
	ConvertPosToBull(position, vector);
	ConvertRotationToBull(angles, matrix);
	btTransform transform(matrix, vector);

	float mass = pParams->mass;
	if (isStatic) mass = 0;

	btMotionState* motionstate = new btMassCenterMotionState(transform);
	btRigidBody::btRigidBodyConstructionInfo info(mass,motionstate,shape);

	btRigidBody* body = new btRigidBody(info);

	if (mass > 0)
		pEnvironment->GetBulletEnvironment()->addRigidBody(body);
	else
		pEnvironment->GetBulletEnvironment()->addRigidBody(body, 2, ~2);

	float volume = pParams->volume;
	if (volume <= 0) {
		volume = 4.0f * radius * radius * radius * M_PI / 3.0f;
	}

	CPhysicsObject *pObject = new CPhysicsObject();
	pObject->Init(pEnvironment, body, materialIndex, volume, 0, 0, NULL);
	pObject->SetGameData(pParams->pGameData);
	pObject->EnableCollisions(pParams->enableCollisions);

	return pObject;
}
int CPlayerController::TryTeleportObject( void )
{
	if ( m_handler )
	{
		Vector hlPosition;
		ConvertPositionToHL( m_targetPosition, hlPosition );
		if ( !m_handler->ShouldMoveTo( m_pObject, hlPosition ) )
			return 0;
	}

	IVP_Real_Object *pivp = m_pObject->GetObject();
	IVP_U_Quat targetOrientation;
	IVP_U_Point outPosition;

	pivp->get_quat_world_f_object_AT( &targetOrientation, &outPosition );

	if ( pivp->is_collision_detection_enabled() )
	{
		m_pObject->EnableCollisions( false );
		pivp->beam_object_to_new_position( &targetOrientation, &m_targetPosition, IVP_TRUE );
		m_pObject->EnableCollisions( true );
	}
	else
	{
		pivp->beam_object_to_new_position( &targetOrientation, &m_targetPosition, IVP_TRUE );
	}

	return 1;
}
void CShadowController::AttachObject( void )
{
	IVP_Real_Object *pivp = m_pObject->GetObject();
	IVP_Core *pCore = pivp->get_core();
	m_saveRot = pCore->rot_speed_damp_factor;
	m_savedRI = *pCore->get_rot_inertia();
	m_savedMass = pCore->get_mass();
	m_savedMaterialIndex = m_pObject->GetMaterialIndexInternal();
	
	m_pObject->SetMaterialIndex( MATERIAL_INDEX_SHADOW );

	pCore->rot_speed_damp_factor = IVP_U_Float_Point( 100, 100, 100 );

	if ( !m_allowPhysicsRotation )
	{
		IVP_U_Float_Point ri( 1e15f, 1e15f, 1e15f );
		pCore->set_rotation_inertia( &ri );
	}
	if ( !m_allowPhysicsMovement )
	{
		m_pObject->SetMass( 1e6f );
		//pCore->inv_rot_inertia.hesse_val = 0.0f;
		m_pObject->EnableGravity( false );
	}

	pCore->calc_calc();
	pivp->get_environment()->get_controller_manager()->add_controller_to_core( this, pCore );
	m_shadow.lastPosition.set_to_zero();
}
Exemplo n.º 8
0
void CPhysicsActor::RemoveVisualization()
{
	// get the CPhysicsObject's name
	PHYSICSUSERDATA* userData = (PHYSICSUSERDATA*)m_Actor->userData;
	
	if( userData == NULL )
		return;

	CPhysicsObject* physObj = userData->physObj;
	IHashString* cpoName = physObj->GetParentName();

	// Remove oobb visualizations
	REMOVEOBJECTORIENTEDBOXPARAMS oobbParams;
	oobbParams.name = cpoName;
	static DWORD msgHash_RemoveObjectOrientedBox = CHashString(_T("RemoveObjectOrientedBox")).GetUniqueID();
	m_ToolBox->SendMessage(msgHash_RemoveObjectOrientedBox, sizeof(REMOVEOBJECTORIENTEDBOXPARAMS), &oobbParams );
	
	// Remove aabb visualizations
	REMOVEAXISALIGNEDBOXPARAMS aabbParams;
	aabbParams.name = cpoName;
	static DWORD msgHash_RemoveAxisAlignedBox = CHashString(_T("RemoveAxisAlignedBox")).GetUniqueID();
	m_ToolBox->SendMessage(msgHash_RemoveAxisAlignedBox, sizeof(REMOVEAXISALIGNEDBOXPARAMS), &oobbParams );
	
	// Remove sphere visualizations
	REMOVESPHEREPARAMS sphereParams;
	sphereParams.name = cpoName;
	static DWORD msgHash_RemoveSphere = CHashString(_T("RemoveSphere")).GetUniqueID();
	m_ToolBox->SendMessage(msgHash_RemoveSphere, sizeof(REMOVESPHEREPARAMS), &sphereParams );
}
Exemplo n.º 9
0
void GOBall::updateVelocity() {
	// get desired move velocity
	CController* controller = (CController*)getComponent(CController::classTypeID());
	float moveVelocity = controller->getMoveDirection() * VEL_MAX;
	
	// get actual velocity
	CPhysicsObject* physicsComponent = (CPhysicsObject*)getComponent(CPhysicsObject::classTypeID());
	b2Body* body = physicsComponent->getBody();
	b2Vec2 position = body->GetPosition();
	b2Vec2 velocity = body->GetLinearVelocity();
	
	// calculate difference and product
	float product = velocity.x * moveVelocity;
	float difference = moveVelocity - velocity.x;
	
	// if moving, change move velocity
    /*if (moveVelocity != 0) {
        std::cout << "MOVING\n" << std::flush;
    }
    else {
        std::cout << "NOT MOVING\n" << std::flush;
        }*/

	if (moveVelocity != 0.0f &&
		(product < 0.0f || fabs(velocity.x) < fabs(moveVelocity) )) {
		float force = difference * FORCE_CONST;
		body->ApplyForce(b2Vec2(force, 0.0f), position);
	}
	else {
		float force = -velocity.x * FORCE_CONST * 0.2f;
		body->ApplyForce(b2Vec2(force, 0.0f), position);
	}
}
Exemplo n.º 10
0
CPhysicsObject *CreatePhysicsSphere( CPhysicsEnvironment *pEnvironment, float radius, int materialIndex, const Vector &position, const QAngle &angles, objectparams_t *pParams, bool isStatic )
{
	IVP_U_Quat rotation;
	IVP_U_Point pos;

	ConvertRotationToIVP( angles, rotation );
	ConvertPositionToIVP( position, pos );

	IVP_Template_Real_Object objectTemplate;
	InitObjectTemplate( objectTemplate, materialIndex, pParams, isStatic );

	IVP_Template_Ball ballTemplate;
	ballTemplate.radius = ConvertDistanceToIVP( radius );
	IVP_Ball *realObject = pEnvironment->GetIVPEnvironment()->create_ball( &ballTemplate, &objectTemplate, &rotation, &pos );

	float volume = pParams->volume;
	if ( volume <= 0 )
	{
		volume = 4.0f * radius * radius * radius * M_PI / 3.0f;
	}
	CPhysicsObject *pObject = new CPhysicsObject();
	pObject->Init( realObject, materialIndex, volume, 0, 0, NULL ); //, pParams->dragCoefficient, pParams->dragCoefficient
	pObject->SetGameData( pParams->pGameData );

	if ( pParams->enableCollisions )
	{
		pObject->EnableCollisions( true );
	}
	// drag is not supported on spheres
	//pObject->EnableDrag( false );
	// no rolling drag by default
	pObject->SetRollingDrag( 0 );

	return pObject;
}
Exemplo n.º 11
0
void GOBall::kill() {
    m_Alive = false;
    CPhysicsObject* physicsComponent = (CPhysicsObject*)getComponent(CPhysicsObject::classTypeID());
    physicsComponent->kill();
    CGraphicsObject* graphicsComponent = (CGraphicsObject*)getComponent(CGraphicsObject::classTypeID());
    graphicsComponent->kill();
}
bool CPlayerController::IsInContact() {
	CPhysicsEnvironment *pEnv = m_pObject->GetVPhysicsEnvironment();

	int numManifolds = pEnv->GetBulletEnvironment()->getDispatcher()->getNumManifolds();
	for (int i = 0; i < numManifolds; i++) {
		btPersistentManifold *contactManifold = pEnv->GetBulletEnvironment()->getDispatcher()->getManifoldByIndexInternal(i);
		const btCollisionObject *obA = contactManifold->getBody0();
		const btCollisionObject *obB = contactManifold->getBody1();
		CPhysicsObject *pPhysUs = NULL;
		CPhysicsObject *pPhysOther = NULL;

		if (contactManifold->getNumContacts() > 0 && (obA == m_pObject->GetObject() || obB == m_pObject->GetObject())) {
			if (obA == m_pObject->GetObject()) {
				pPhysUs = (CPhysicsObject *)obA->getUserPointer();
				pPhysOther = (CPhysicsObject *)obB->getUserPointer();
			} else if (obB == m_pObject->GetObject()) {
				pPhysUs = (CPhysicsObject *)obB->getUserPointer();
				pPhysOther = (CPhysicsObject *)obA->getUserPointer();
			}

			if (pPhysOther->IsStatic() || !pPhysOther->IsMotionEnabled() || (pPhysOther->GetCallbackFlags() & CALLBACK_SHADOW_COLLISION))
				continue;

			return true;
		}
	}

	return false;
}
void CPhysicsMotionController::DetachObject( IPhysicsObject *pObject )
{
	CPhysicsObject *pPhys = static_cast<CPhysicsObject *>(pObject);
	IVP_Real_Object *pIVP = pPhys->GetObject();
	IVP_Core *core = pIVP->get_core();

	RemoveCore(core);
}
Exemplo n.º 14
0
void GOBall::jump() {
	CController* controller = (CController*)getComponent(CController::classTypeID());
	if (controller->getJump()) {
		CPhysicsObject* physicsComponent = (CPhysicsObject*)getComponent(CPhysicsObject::classTypeID());
		b2Body* body = physicsComponent->getBody();
		b2Vec2 velocity = body->GetLinearVelocity();
		body->SetLinearVelocity(b2Vec2(velocity.x, JUMP_CONST));
	}
}
    void event_object_frozen( IVP_Event_Object *pEvent )
	{
		CPhysicsObject *pObject = static_cast<CPhysicsObject *>(pEvent->real_object->client_data);
		if ( !pObject )
			return;

		pObject->NotifySleep();
		if ( m_pCallback )
		{
			m_pCallback->ObjectSleep( pObject );
		}
	}
Exemplo n.º 16
0
void CPhysicsSpring::AttachListener()
{
	if ( !(m_pObjStart->GetCallbackFlags() & CALLBACK_NEVER_DELETED) )
	{
		m_pObjStart->GetObject()->add_listener_object( this );
	}

	if ( !(m_pObjEnd->GetCallbackFlags() & CALLBACK_NEVER_DELETED) )
	{
		m_pObjEnd->GetObject()->add_listener_object( this );
	}
}
Exemplo n.º 17
0
		void Tick() {
			btDiscreteDynamicsWorld *pBulletEnv = m_pEnv->GetBulletEnvironment();
			btCollisionObjectArray &colObjArray = pBulletEnv->getCollisionObjectArray();
			for (int i = 0; i < colObjArray.size(); i++) {
				CPhysicsObject *pObj = (CPhysicsObject *)colObjArray[i]->getUserPointer();
				if (!pObj) continue; // Internal object that the game doesn't need to know about

				Assert(*(char *)pObj != 0xDD); // Make sure the object isn't deleted (only works in debug builds)
 
				// Don't add objects marked for delete
				if (pObj->GetCallbackFlags() & CALLBACK_MARKED_FOR_DELETE) {
					continue;
				}

				if (colObjArray[i]->getActivationState() != pObj->GetLastActivationState()) {
					int newState = colObjArray[i]->getActivationState();

					// Not a state we want to track.
					if (newState == WANTS_DEACTIVATION)
						continue;

					if (m_pObjEvents) {
						switch (newState) {
							// FIXME: Objects may call objectwake twice if they go from disable_deactivation -> active_tag
							case DISABLE_DEACTIVATION:
							case ACTIVE_TAG:
								m_pObjEvents->ObjectWake(pObj);
								break;
							case ISLAND_SLEEPING: // Don't call ObjectSleep on DISABLE_SIMULATION on purpose.
								m_pObjEvents->ObjectSleep(pObj);
								break;
						}
					}

					switch (newState) {
						case DISABLE_DEACTIVATION:
						case ACTIVE_TAG:
							// Don't add the object twice!
							if (m_activeObjects.Find(pObj) == -1)
								m_activeObjects.AddToTail(pObj);

							break;
						case DISABLE_SIMULATION:
						case ISLAND_SLEEPING:
							m_activeObjects.FindAndRemove(pObj);
							break;
					}

					pObj->SetLastActivationState(newState);
				}
			}
		}
Exemplo n.º 18
0
CPhysicsObject* CreatePhysicsObject(CPhysicsEnvironment *pEnvironment, const CPhysCollide *pCollisionModel, int materialIndex, const Vector &position, const QAngle& angles, objectparams_t *pParams, bool isStatic) {
	btCollisionShape* shape = (btCollisionShape*)pCollisionModel;
	
	btVector3 vector;
	btMatrix3x3 matrix;
	ConvertPosToBull(position, vector);
	ConvertRotationToBull(angles, matrix);
	btTransform transform(matrix, vector);

	PhysicsShapeInfo *shapeInfo = (PhysicsShapeInfo*)shape->getUserPointer();
	btTransform masscenter(btMatrix3x3::getIdentity());
	if (shapeInfo) masscenter.setOrigin(shapeInfo->massCenter);

	float mass = pParams->mass;
	if (isStatic) mass = 0;

	btVector3 inertia;

	shape->calculateLocalInertia(mass, inertia);
	btMotionState* motionstate = new btMassCenterMotionState(transform, masscenter);
	btRigidBody::btRigidBodyConstructionInfo info(mass,motionstate,shape,inertia);

	info.m_linearDamping = pParams->damping;
	info.m_angularDamping = pParams->rotdamping;
	//info.m_localInertia = btVector3(pParams->inertia, pParams->inertia, pParams->inertia);

	btRigidBody* body = new btRigidBody(info);

	if (mass > 0)
		pEnvironment->GetBulletEnvironment()->addRigidBody(body);
	else
		pEnvironment->GetBulletEnvironment()->addRigidBody(body, 2, ~2);

	CPhysicsObject *pObject = new CPhysicsObject();
	pObject->Init(pEnvironment, body, materialIndex, pParams->volume, pParams->dragCoefficient, pParams->dragCoefficient, pParams->massCenterOverride);
	pObject->SetGameData(pParams->pGameData);
	pObject->EnableCollisions(pParams->enableCollisions);
	if (!isStatic && pParams->dragCoefficient != 0.0f) pObject->EnableDrag(true);

	/*if (mass > 0)
	{
		btVector3 mins, maxs;
		shape->getAabb(btTransform::getIdentity(), mins, maxs);
		float maxradius = min(min(abs(maxs.getX()), abs(maxs.getY())), abs(maxs.getZ()));
		float minradius = min(min(abs(mins.getX()), abs(mins.getY())), abs(mins.getZ()));
		float radius = min(maxradius,minradius)/2.0f;
		body->setCcdMotionThreshold(radius*0.5f);
		body->setCcdSweptSphereRadius(0.2f*radius);
	}*/
	
	return pObject;
}
Exemplo n.º 19
0
void CPhysicsSpring::DetachListener()
{
	if ( !(m_pObjStart->GetCallbackFlags() & CALLBACK_NEVER_DELETED) )
	{
		m_pObjStart->GetObject()->remove_listener_object( this );
	}

	if ( !(m_pObjEnd->GetCallbackFlags() & CALLBACK_NEVER_DELETED) )
	{
		m_pObjEnd->GetObject()->remove_listener_object( this );
	}

	m_pObjStart = NULL;
	m_pObjEnd = NULL;
	m_pSpring = NULL;
}
Exemplo n.º 20
0
CPhysicsObject *CreatePhysicsObject(CPhysicsEnvironment *pEnvironment, const CPhysCollide *pCollisionModel, int materialIndex, const Vector &position, const QAngle &angles, objectparams_t *pParams, bool isStatic) {
	if (!pCollisionModel) return NULL;

	// Some checks
	Assert(position.IsValid() && angles.IsValid());

	btCollisionShape *pShape = (btCollisionShape *)pCollisionModel->GetCollisionShape();

	btTransform massCenterTrans = btTransform::getIdentity();
	massCenterTrans.setOrigin(pCollisionModel->GetMassCenter());
	btMassCenterMotionState *pMotionState = new btMassCenterMotionState(massCenterTrans);

	btVector3 bullPos;
	btMatrix3x3 bullMatrix;
	ConvertPosToBull(position, bullPos);
	ConvertRotationToBull(angles, bullMatrix);

	btTransform initialWordTrans(bullMatrix, bullPos);
	pMotionState->setGraphicTransform(initialWordTrans);

	// Grab some parameters
	btScalar mass = 0.f;
	btVector3 inertia(0, 0, 0);
	btVector3 inertiaCoeff(1, 1, 1);

	if (pParams && !isStatic) {
		mass = pParams->mass;

		// TODO: Grab massCenterOverride and set it up correctly.

		// Don't allow the inertia coefficient to be less than 0!
		if (pParams->inertia >= 0)
			inertiaCoeff.setValue(pParams->inertia, pParams->inertia, pParams->inertia);

		pShape->calculateLocalInertia(mass, inertia);
		//inertia = pCollisionModel->GetRotationInertia();
		//inertia *= inertiaCoeff * mass;
	}

	btRigidBody::btRigidBodyConstructionInfo info(mass, pMotionState, pShape, inertia);
	btRigidBody *pBody = new btRigidBody(info);

	CPhysicsObject *pObject = new CPhysicsObject();
	pObject->Init(pEnvironment, pBody, materialIndex, pParams, isStatic);

	return pObject;
}
    void event_object_revived( IVP_Event_Object *pEvent )
	{
		CPhysicsObject *pObject = static_cast<CPhysicsObject *>(pEvent->real_object->client_data);
		if ( !pObject )
			return;

		int sleepState = pObject->GetSleepState();
		
		pObject->NotifyWake();

		// asleep, but already in active list
		if ( sleepState == OBJ_STARTSLEEP )
			return;

		// don't track static objects (like the world).  That way we only track objects that will move
		if ( pObject->GetObject()->get_movement_state() != IVP_MT_STATIC )
		{
			Assert(pObject->GetActiveIndex()==0xFFFF);
			if ( pObject->GetActiveIndex()!=0xFFFF)
				return;

			int index = m_activeObjects.AddToTail( pObject );
			pObject->SetActiveIndex( index );
		}
		if ( m_pCallback )
		{
			m_pCallback->ObjectWake( pObject );
		}
	}
Exemplo n.º 22
0
void CPhysicsSpring::GetEndpoints( Vector* worldPositionStart, Vector* worldPositionEnd )
{
	IVP_U_Point world_coords;

	if ( worldPositionStart )
	{
		const IVP_Anchor *anchor = m_pSpring->get_actuator_anchor(0);
		TransformLocalToIVP( anchor->object_pos, world_coords, m_pObjStart->GetObject(), true );
		ConvertPositionToHL( world_coords, *worldPositionStart );
	}

	if ( worldPositionEnd )
	{
		const IVP_Anchor *anchor = m_pSpring->get_actuator_anchor(1);
		TransformLocalToIVP( anchor->object_pos, world_coords, m_pObjEnd->GetObject(), true );
		ConvertPositionToHL( world_coords, *worldPositionEnd );
	}
}
Exemplo n.º 23
0
void CPlayerController::AttachObject( void )
{
	IVP_Real_Object *pivp = m_pObject->GetObject();
	IVP_Core *pCore = pivp->get_core();
	m_saveRot = pCore->rot_speed_damp_factor;
	pCore->rot_speed_damp_factor = IVP_U_Float_Point( 100, 100, 100 );
	pCore->calc_calc();
	pivp->get_environment()->get_controller_manager()->add_controller_to_core( this, pCore );
}
Exemplo n.º 24
0
void CShadowController::DetachObject( void )
{
	IVP_Real_Object *pivp = m_pObject->GetObject();
	IVP_Core *pCore = pivp->get_core();
	pCore->rot_speed_damp_factor = m_saveRot;
	pCore->set_mass( m_savedMass );
	pCore->set_rotation_inertia( &m_savedRI ); // this calls calc_calc()
	m_pObject = NULL;
	pivp->get_environment()->get_controller_manager()->remove_controller_from_core( this, pCore );
}
Exemplo n.º 25
0
void CPlayerController::do_simulation_controller( IVP_Event_Sim *es,IVP_U_Vector<IVP_Core> *)
{
	if ( !m_enable )
		return;

	IVP_Real_Object *pivp = m_pObject->GetObject();
	IVP_Environment *pIVPEnv = pivp->get_environment();
	CPhysicsEnvironment *pVEnv = (CPhysicsEnvironment *)pIVPEnv->client_data;

	float psiScale = pVEnv->GetInvPSIScale();
	if ( !psiScale )
		return;

	IVP_Core *pCore = pivp->get_core();
	// current situation
	const IVP_U_Matrix *m_world_f_core = pCore->get_m_world_f_core_PSI();
	const IVP_U_Point *cur_pos_ws = m_world_f_core->get_position();

	// ---------------------------------------------------------
	// Translation
	// ---------------------------------------------------------

	IVP_U_Float_Point delta_position;  delta_position.subtract( &m_targetPosition, cur_pos_ws);


	if (!pivp->flags.shift_core_f_object_is_zero)
	{
		IVP_U_Float_Point shift_cs_os_ws;
		m_world_f_core->vmult3( pivp->get_shift_core_f_object(), &shift_cs_os_ws);
		delta_position.subtract( &shift_cs_os_ws );
	}


	IVP_DOUBLE qdist = delta_position.quad_length();

	// UNDONE: This is totally bogus!  Measure error using last known estimate
	// not current position!
	if ( qdist > m_maxDeltaPosition * m_maxDeltaPosition )
	{
		if ( TryTeleportObject() )
			return;
	}

	// float to allow stepping
	if ( m_onground )
	{
		const IVP_U_Point *pgrav = es->environment->get_gravity();
		IVP_U_Float_Point gravSpeed;
		gravSpeed.set_multiple( pgrav, es->delta_time );
		pCore->speed.subtract( &gravSpeed );
	}

	ComputeController( pCore->speed, delta_position, m_maxSpeed, psiScale / es->delta_time, m_dampFactor );
}
Exemplo n.º 26
0
void CPhysicsDragController::Tick(btScalar dt)
{
    for(int i = 0; i < m_ents.Size(); i++)
    {
        CPhysicsObject * object = (CPhysicsObject *)m_ents[i];

        Vector dragLinearFinal(0,0,0);
        AngularImpulse dragAngularFinal(0,0,0);

        Vector vel;
        AngularImpulse ang;
        object->GetVelocity(&vel, &ang);

        btVector3 bull_vel;

        ConvertPosToBull(vel, bull_vel);

        float dragForce = -0.5 * object->GetDragInDirection( &bull_vel ) * m_airDensity * dt;
        if ( dragForce < -1.0f )
        {
            dragForce = -1.0f;
        }
        if ( dragForce < 0 )
        {
            Vector dragLinearFinal = vel * dragForce;
        }
        btVector3 bull_angimpulse;
        ConvertAngularImpulseToBull(ang, bull_angimpulse);

        float angDragForce = -object->GetAngularDragInDirection(&bull_angimpulse) * m_airDensity * dt;
        if ( angDragForce < -1.0f )
        {
            angDragForce = -1.0f;
        }
        if( angDragForce < 0)
        {
            dragAngularFinal = ang * angDragForce;
        }
        object->AddVelocity(&dragLinearFinal, &dragAngularFinal);
    }
}
Exemplo n.º 27
0
void CPlayerController::DetachObject( void )
{
	if ( !m_pObject )
		return;

	IVP_Real_Object *pivp = m_pObject->GetObject();
	IVP_Core *pCore = pivp->get_core();
	pCore->rot_speed_damp_factor = m_saveRot;
	pCore->calc_calc();
	m_pObject = NULL;
	pivp->get_environment()->get_controller_manager()->remove_controller_from_core( this, pCore );
}
Exemplo n.º 28
0
void CShadowController::StepUp( float height )
{
	Vector step( 0, 0, height );

	IVP_Real_Object *pIVP = m_pObject->GetObject();
	IVP_U_Quat world_f_object;
	IVP_U_Point positionIVP, deltaIVP;
	ConvertPositionToIVP( step, deltaIVP );
	pIVP->get_quat_world_f_object_AT( &world_f_object, &positionIVP );
	positionIVP.add( &deltaIVP );
	pIVP->beam_object_to_new_position( &world_f_object, &positionIVP, IVP_TRUE );
}
Exemplo n.º 29
0
CPhysicsObject *CreatePhysicsSphere(CPhysicsEnvironment *pEnvironment, float radius, int materialIndex, const Vector &position, const QAngle &angles, objectparams_t *pParams, bool isStatic) {
	if (!pEnvironment) return NULL;

	// Some checks
	Assert(position.IsValid() && angles.IsValid());

	// Conversion unnecessary as this is an exposed function.
	btSphereShape *shape = (btSphereShape *)g_PhysicsCollision.SphereToConvex(radius);
	
	btVector3 vector;
	btMatrix3x3 matrix;
	ConvertPosToBull(position, vector);
	ConvertRotationToBull(angles, matrix);
	btTransform transform(matrix, vector);

	float mass = 0;
	float volume = 0;

	if (pParams) {
		mass = isStatic ? 0 : pParams->mass;

		volume = pParams->volume;
		if (volume <= 0) {
			pParams->volume = (4 / 3) * M_PI * radius * radius * radius;
		}
	}

	btMassCenterMotionState *motionstate = new btMassCenterMotionState();
	motionstate->setGraphicTransform(transform);
	btRigidBody::btRigidBodyConstructionInfo info(mass, motionstate, shape);

	btRigidBody *body = new btRigidBody(info);

	CPhysicsObject *pObject = new CPhysicsObject;
	pObject->Init(pEnvironment, body, materialIndex, pParams, isStatic, true);

	return pObject;
}
void CPhysicsMotionController::AttachObject( IPhysicsObject *pObject )
{
	Assert(pObject);
	// BUGBUG: Sometimes restore comes back with a NULL, REVISIT
	if ( !pObject || pObject->IsStatic() )
		return;

	CPhysicsObject *pPhys = static_cast<CPhysicsObject *>(pObject);
	IVP_Real_Object *pIVP = pPhys->GetObject();
	IVP_Core *pCore = pIVP->get_core();

#if DEBUG
	int index = m_coreList.Find(pCore);
	if ( m_coreList.IsValidIndex(index) )
	{
		Msg("Attached core twice!!!\n");
		return;
	}
#endif

	m_coreList.AddToTail( pCore );
	pCore->add_core_controller( (IVP_Controller *)this );
}