void CPhysicsObject::SetShadow( const Vector &maxVelocity, const AngularImpulse &maxAngularVelocity, bool allowPhysicsMovement, bool allowPhysicsRotation ) { if ( m_pShadow ) { m_pShadow->MaxSpeed( maxVelocity, maxAngularVelocity ); } else { // m_pObject->get_core()->rot_speed_damp_factor = IVP_U_Float_Point( 1e5f, 1e5f, 1e5f ); m_shadowTempGravityDisable = false; unsigned int flags = GetCallbackFlags() | CALLBACK_SHADOW_COLLISION; flags &= ~CALLBACK_GLOBAL_FRICTION; flags &= ~CALLBACK_GLOBAL_COLLIDE_STATIC; SetCallbackFlags( flags ); IVP_Environment *pEnv = m_pObject->get_environment(); CPhysicsEnvironment *pVEnv = (CPhysicsEnvironment *)pEnv->client_data; m_pShadow = pVEnv->CreateShadowController( this, allowPhysicsMovement, allowPhysicsRotation ); m_pShadow->MaxSpeed( maxVelocity, maxAngularVelocity ); SetRollingDrag( 0 ); EnableDrag( false ); if ( !allowPhysicsMovement ) { EnableGravity( false ); } } }
void CPhysicsObject::UpdateShadow( const Vector &targetPosition, const QAngle &targetAngles, bool tempDisableGravity, float timeOffset ) { if ( tempDisableGravity != m_shadowTempGravityDisable ) { m_shadowTempGravityDisable = tempDisableGravity; if ( !m_pShadow || m_pShadow->AllowsTranslation() ) { EnableGravity( !m_shadowTempGravityDisable ); } } if ( m_pShadow ) { m_pShadow->Update( targetPosition, targetAngles, timeOffset ); } }
void CPhysicsObject::BecomeTrigger() { if ( IsTrigger() ) return; EnableDrag( false ); EnableGravity( false ); // UNDONE: Use defaults here? Do we want object sets by default? IVP_Template_Phantom trigger; trigger.manage_intruding_cores = IVP_TRUE; // manage a list of intruded objects trigger.dont_check_for_unmoveables = IVP_TRUE; trigger.exit_policy_extra_radius = 0.1f; // relatively strict exit check [m] m_pObject->convert_to_phantom( &trigger ); // hook up events IVP_Environment *pEnv = m_pObject->get_environment(); CPhysicsEnvironment *pVEnv = (CPhysicsEnvironment *)pEnv->client_data; pVEnv->PhantomAdd( this ); }
void CPhysicsObject::BecomeTrigger() { if (IsTrigger()) return; EnableDrag(false); EnableGravity(false); m_pEnv->GetBulletEnvironment()->removeRigidBody(m_pObject); m_pGhostObject = new btGhostObject; m_pGhostObject->setCollisionShape(m_pObject->getCollisionShape()); m_pGhostObject->setUserPointer(this); m_pGhostObject->setCollisionFlags(m_pGhostObject->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE | btCollisionObject::CF_STATIC_OBJECT); m_pGhostObject->setWorldTransform(m_pObject->getWorldTransform()); m_pGhostCallback = new CGhostTriggerCallback(this); if (m_pGhostCallback) m_pGhostObject->setCallback(m_pGhostCallback); m_pEnv->GetBulletEnvironment()->addCollisionObject(m_pGhostObject, COLGROUP_WORLD, ~COLGROUP_WORLD); }
void CPhysicsObject::RemoveTrigger() { if (!IsTrigger()) return; EnableDrag(true); EnableGravity(true); m_pObject->setWorldTransform(m_pGhostObject->getWorldTransform()); if (IsStatic()) m_pEnv->GetBulletEnvironment()->addRigidBody(m_pObject, COLGROUP_WORLD, ~COLGROUP_WORLD); else m_pEnv->GetBulletEnvironment()->addRigidBody(m_pObject); m_pGhostObject->setCallback(NULL); delete m_pGhostCallback; m_pGhostCallback = NULL; m_pEnv->GetBulletEnvironment()->removeCollisionObject(m_pGhostObject); delete m_pGhostObject; m_pGhostObject = NULL; }
void InitializePhysX(vector<PhysXObject*>* &cubeList) { allActors = new vector<PhysXObject*>; PxFoundation* foundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback); gPhysicsSDK = PxCreatePhysics(PX_PHYSICS_VERSION, *foundation, PxTolerancesScale()); if(gPhysicsSDK == NULL) { exit(1); } PxInitExtensions(*gPhysicsSDK); PxSceneDesc sceneDesc(gPhysicsSDK->getTolerancesScale()); sceneDesc.gravity=PxVec3(0.0f, -9.8f, 0.0f); if(!sceneDesc.cpuDispatcher) { PxDefaultCpuDispatcher* mCpuDispatcher = PxDefaultCpuDispatcherCreate(3); sceneDesc.cpuDispatcher = mCpuDispatcher; } if(!sceneDesc.filterShader) sceneDesc.filterShader = gDefaultFilterShader; gScene = gPhysicsSDK->createScene(sceneDesc); gScene->setVisualizationParameter(PxVisualizationParameter::eSCALE, 1.0); gScene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES, 1.0f); //1) Create Planes PxMaterial* mMaterial = gPhysicsSDK->createMaterial(0.5, 0.5, 0.5); for(int i = 0; i < 1; i++) { PhysXObject* plane = new PhysXObject; plane->actor = gPhysicsSDK->createRigidStatic(planePoses[i]); PxShape* shape = plane->actor->createShape(PxPlaneGeometry(), *mMaterial); gScene->addActor(*(plane->actor)); allActors->push_back(plane); planes.push_back(plane); } //2) Create Planets PxReal planetDensity = 1.0f; PxVec3 planetDimensions(2,2,2); PxBoxGeometry planetGeom(planetDimensions); PxTransform planetTransform; for(int i = 0; i < PLANET_NUM; i++) { planetTransform = planetTransforms[i]; PhysXObject* planet = new PhysXObject; planet->actor = PxCreateStatic(*gPhysicsSDK, planetTransform, planetGeom, *mMaterial); EnableGravity(planet->actor); gScene->addActor(*(planet->actor)); allActors->push_back(planet); planets.push_back(planet); //HACK: /* Create the joint handlers for distance limiting /* We need to do this because a distance joint attached to an actor /* seems to void collisions between those two actors (i.e. "phases through") /* So we make another actor in the same position to hold the position PhysXObject* newHandle = new PhysXObject; newHandle->actor = PxCreateStatic(*gPhysicsSDK, tran, boxgeom, *mMaterial); gScene->addActor(*(newHandle->actor)); planetJointHandles.push_back(newHandle); //We also don't need to worry about drawing the joints, for obvious reasons */ } //3) Create Cubes PxReal density = 1.0f; PxTransform transform(PxVec3(0.0f, 0.0f, 0.0f), PxQuat::createIdentity()); PxVec3 dimensions(0.5, 0.5, 0.5); PxBoxGeometry geometry(dimensions); for(int i = 0; i < BLOCK_NUM; i++) { srand((time(NULL) * i) + time(NULL)); transform.p = PxVec3((float)((rand() % (2 * PLANET_HEIGHT)) - PLANET_HEIGHT), (float)((rand() % (2 * PLANET_HEIGHT)) - PLANET_HEIGHT), (float)((rand() % (2 * PLANET_HEIGHT)) - PLANET_HEIGHT)); PhysXObject* cube = new PhysXObject; cube->actor = PxCreateDynamic(*gPhysicsSDK, transform, geometry, *mMaterial, density); //Create Distance Joints between planets here //Not included for run time optimizations //End creating distance joints //Create D6 Joints between planets here //Not included for run time optimizations //End creating distance joints cube->actor->isRigidDynamic()->setAngularDamping(0.75); cube->actor->isRigidDynamic()->setLinearVelocity(PxVec3(0,0,0)); gScene->addActor(*(cube->actor)); allActors->push_back(cube); boxes.push_back(cube); } cubeList = allActors; }
void ApplyNormalGravity(PhysXObject* object) { EnableGravity(object->actor); }
void CPhysicsObject::InitFromTemplate( CPhysicsEnvironment *pEnvironment, void *pGameData, const vphysics_save_cphysicsobject_t &objectTemplate ) { m_collideType = objectTemplate.collideType; IVP_Template_Real_Object ivpObjectTemplate; IVP_U_Quat rotation; IVP_U_Point pos; ConvertRotationToIVP( objectTemplate.angles, rotation ); ConvertPositionToIVP( objectTemplate.origin, pos ); ivpObjectTemplate.mass = objectTemplate.mass; if ( objectTemplate.materialIndex >= 0 ) { ivpObjectTemplate.material = physprops->GetIVPMaterial( objectTemplate.materialIndex ); } else { ivpObjectTemplate.material = physprops->GetIVPMaterial( physprops->GetSurfaceIndex( "default" ) ); } Assert( ivpObjectTemplate.material ); // HACKHACK: Pass this name in for debug ivpObjectTemplate.set_name(objectTemplate.pName); ivpObjectTemplate.set_nocoll_group_ident( NULL ); ivpObjectTemplate.physical_unmoveable = objectTemplate.isStatic ? IVP_TRUE : IVP_FALSE; ivpObjectTemplate.rot_inertia_is_factor = IVP_TRUE; ivpObjectTemplate.rot_inertia.set( 1,1,1 ); ivpObjectTemplate.rot_speed_damp_factor.set( objectTemplate.rotSpeedDamping, objectTemplate.rotSpeedDamping, objectTemplate.rotSpeedDamping ); ivpObjectTemplate.speed_damp_factor = objectTemplate.speedDamping; IVP_U_Matrix massCenterMatrix; massCenterMatrix.init(); const Vector *massOverride = 0; if ( objectTemplate.massCenterOverride != vec3_origin ) { IVP_U_Point center; ConvertPositionToIVP( objectTemplate.massCenterOverride, center ); massCenterMatrix.shift_os( ¢er ); ivpObjectTemplate.mass_center_override = &massCenterMatrix; massOverride = &objectTemplate.massCenterOverride; } IVP_Real_Object *realObject = NULL; if ( m_collideType == COLLIDE_BALL ) { IVP_Template_Ball ballTemplate; ballTemplate.radius = ConvertDistanceToIVP( objectTemplate.sphereRadius ); realObject = pEnvironment->GetIVPEnvironment()->create_ball( &ballTemplate, &ivpObjectTemplate, &rotation, &pos ); } else { const IVP_Compact_Surface *pSurface = (const IVP_Compact_Surface *)objectTemplate.pCollide; IVP_SurfaceManager_Polygon *surman = new IVP_SurfaceManager_Polygon( pSurface ); realObject = pEnvironment->GetIVPEnvironment()->create_polygon(surman, &ivpObjectTemplate, &rotation, &pos); } Init( realObject, objectTemplate.materialIndex, objectTemplate.volume, objectTemplate.dragCoefficient, objectTemplate.dragCoefficient, massOverride ); SetInertia( objectTemplate.rotInertia ); // will wake up the object if ( objectTemplate.velocity.LengthSqr() != 0 || objectTemplate.angVelocity.LengthSqr() != 0 ) { Assert( !objectTemplate.isAsleep ); SetVelocity( &objectTemplate.velocity, &objectTemplate.angVelocity ); } SetCallbackFlags( (unsigned short) objectTemplate.callbacks ); SetGameFlags( (unsigned short) objectTemplate.gameFlags ); SetGameData( pGameData ); SetContents( objectTemplate.contentsMask ); if ( objectTemplate.dragEnabled ) { Assert( !objectTemplate.isStatic ); EnableDrag( true ); } if ( !objectTemplate.motionEnabled ) { Assert( !objectTemplate.isStatic ); EnableMotion( false ); } if ( objectTemplate.isTrigger ) { BecomeTrigger(); } if ( !objectTemplate.gravityEnabled ) { EnableGravity( false ); } if ( objectTemplate.collisionEnabled ) { EnableCollisions( true ); } if ( !objectTemplate.isAsleep ) { Assert( !objectTemplate.isStatic ); Wake(); } m_pShadow = NULL; }