// // TeapotWarsGame::VOnUpdate - Chapter 19, page 709 // void CometConquestGame::VOnUpdate(float time, float elapsedTime) { int deltaMilliseconds = int(elapsedTime * 1000.0f); m_Lifetime += elapsedTime; unsigned int currentTime = timeGetTime(); BaseGameLogic::VOnUpdate(time, elapsedTime); if (m_bProxy) return; switch(m_State) { case BGS_LoadingGameEnvironment: break; case BGS_MainMenu: break; case BGS_WaitingForPlayers: if (m_ExpectedPlayers + m_ExpectedRemotePlayers == m_HumanPlayersAttached ) { VChangeState(BGS_LoadingGameEnvironment); } break; case BGS_Running: if(currentTime > (m_data.m_lastCometTime + 5000)) { Vec4 at = -g_Right4 * 2.0f; Vec4 atWorld = Mat4x4::g_Identity.Xform(at); int randVertical = m_random.Random(115) + 1 - 60; Vec3 normalDir(atWorld); normalDir.Normalize(); Mat4x4 temp = Mat4x4::g_Identity; temp.SetPosition(Vec3(110,10,randVertical)); CometParams cp; cp.m_Pos = temp.GetPosition() + Vec3(atWorld); cp.m_Radius = 6.0f; cp.m_Color = g_Cyan; cp.m_NormalDir = normalDir; cp.m_Force = 40000.0f; const EvtData_Request_New_Actor cannonBallEvt( &cp ); safeTriggerEvent( cannonBallEvt ); m_data.m_lastCometTime = currentTime; } break; default: assert(0 && _T("Unrecognized state.")); } // look in Chapter 15, page 563 for more on this bit of code if(m_pPhysics) { m_pPhysics->VOnUpdate(elapsedTime); m_pPhysics->VSyncVisibleScene(); } }
//--------------------------------------------------------------------------------------------------------------------- // Script exports for the physics system //--------------------------------------------------------------------------------------------------------------------- void InternalScriptExports::ApplyForce(LuaPlus::LuaObject normalDirLua, float force, int actorId) { if (normalDirLua.IsTable()) { Vec3 normalDir(normalDirLua["x"].GetFloat(), normalDirLua["y"].GetFloat(), normalDirLua["z"].GetFloat()); g_pApp->m_pGame->VGetGamePhysics()->VApplyForce(normalDir, force, actorId); return; } GCC_ERROR("Invalid object passed to ApplyForce(); type = " + std::string(normalDirLua.TypeName())); }
// apply force to an object from lua void LuaInternalScriptExports::ApplyForce(LuaPlus::LuaObject normalDirectionLua, float force, int gameObjectId) { if (normalDirectionLua.IsTable()) { Vec3 normalDir(normalDirectionLua["x"].GetFloat(), normalDirectionLua["y"].GetFloat(), normalDirectionLua["z"].GetFloat()); g_pApp->m_pGame->GetGamePhysics()->ApplyForce(normalDir, force, gameObjectId); return; } CB_ERROR("Invalid object passed to ApplyForce(). Type = " + std::string(normalDirectionLua.TypeName())); }
dgUnsigned32 dgBallConstraint::JacobianDerivative(dgContraintDescritor& params) { dgInt32 ret; dgFloat32 relVelocErr; dgFloat32 penetrationErr; dgMatrix matrix0; dgMatrix matrix1; if (m_jointUserCallback) { m_jointUserCallback(*this, params.m_timestep); } dgVector angle(CalculateGlobalMatrixAndAngle(matrix0, matrix1)); m_angles = angle.Scale(-dgFloat32(1.0f)); const dgVector& dir0 = matrix0.m_front; const dgVector& dir1 = matrix0.m_up; const dgVector& dir2 = matrix0.m_right; const dgVector& p0 = matrix0.m_posit; const dgVector& p1 = matrix1.m_posit; dgPointParam pointData; InitPointParam(pointData, m_stiffness, p0, p1); CalculatePointDerivative(0, params, dir0, pointData, &m_jointForce[0]); CalculatePointDerivative(1, params, dir1, pointData, &m_jointForce[1]); CalculatePointDerivative(2, params, dir2, pointData, &m_jointForce[2]); ret = 3; if (m_twistLimit) { if (angle.m_x > m_twistAngle) { dgVector p0(matrix0.m_posit + matrix0.m_up.Scale(MIN_JOINT_PIN_LENGTH)); InitPointParam(pointData, m_stiffness, p0, p0); const dgVector& dir = matrix0.m_right; CalculatePointDerivative(ret, params, dir, pointData, &m_jointForce[ret]); dgVector velocError(pointData.m_veloc1 - pointData.m_veloc0); relVelocErr = velocError % dir; if (relVelocErr > dgFloat32(1.0e-3f)) { relVelocErr *= dgFloat32(1.1f); } penetrationErr = MIN_JOINT_PIN_LENGTH * (angle.m_x - m_twistAngle); _ASSERTE(penetrationErr >= dgFloat32 (0.0f)); params.m_forceBounds[ret].m_low = dgFloat32(0.0f); params.m_forceBounds[ret].m_normalIndex = DG_NORMAL_CONSTRAINT; params.m_forceBounds[ret].m_jointForce = &m_jointForce[ret]; // params.m_jointAccel[ret] = (relVelocErr + penetrationErr) * params.m_invTimestep; SetMotorAcceleration(ret, (relVelocErr + penetrationErr) * params.m_invTimestep, params); ret++; } else if (angle.m_x < -m_twistAngle) { dgVector p0(matrix0.m_posit + matrix0.m_up.Scale(MIN_JOINT_PIN_LENGTH)); InitPointParam(pointData, m_stiffness, p0, p0); dgVector dir(matrix0.m_right.Scale(-dgFloat32(1.0f))); CalculatePointDerivative(ret, params, dir, pointData, &m_jointForce[ret]); dgVector velocError(pointData.m_veloc1 - pointData.m_veloc0); relVelocErr = velocError % dir; if (relVelocErr > dgFloat32(1.0e-3f)) { relVelocErr *= dgFloat32(1.1f); } penetrationErr = MIN_JOINT_PIN_LENGTH * (-m_twistAngle - angle.m_x); _ASSERTE(penetrationErr >= dgFloat32 (0.0f)); params.m_forceBounds[ret].m_low = dgFloat32(0.0f); params.m_forceBounds[ret].m_normalIndex = DG_NORMAL_CONSTRAINT; params.m_forceBounds[ret].m_jointForce = &m_jointForce[ret]; // params.m_jointAccel[ret] = (relVelocErr + penetrationErr) * params.m_invTimestep; SetMotorAcceleration(ret, (relVelocErr + penetrationErr) * params.m_invTimestep, params); ret++; } } if (m_coneLimit) { dgFloat32 coneCos; coneCos = matrix0.m_front % matrix1.m_front; if (coneCos < m_coneAngleCos) { dgVector p0( matrix0.m_posit + matrix0.m_front.Scale(MIN_JOINT_PIN_LENGTH)); InitPointParam(pointData, m_stiffness, p0, p0); dgVector tangentDir(matrix0.m_front * matrix1.m_front); tangentDir = tangentDir.Scale( dgRsqrt ((tangentDir % tangentDir) + 1.0e-8f)); CalculatePointDerivative(ret, params, tangentDir, pointData, &m_jointForce[ret]); ret++; dgVector normalDir(tangentDir * matrix0.m_front); dgVector velocError(pointData.m_veloc1 - pointData.m_veloc0); //restitution = contact.m_restitution; relVelocErr = velocError % normalDir; if (relVelocErr > dgFloat32(1.0e-3f)) { relVelocErr *= dgFloat32(1.1f); } penetrationErr = MIN_JOINT_PIN_LENGTH * (dgAcos (GetMax (coneCos, dgFloat32(-0.9999f))) - m_coneAngle); _ASSERTE(penetrationErr >= dgFloat32 (0.0f)); CalculatePointDerivative(ret, params, normalDir, pointData, &m_jointForce[ret]); params.m_forceBounds[ret].m_low = dgFloat32(0.0f); params.m_forceBounds[ret].m_normalIndex = DG_NORMAL_CONSTRAINT; params.m_forceBounds[ret].m_jointForce = &m_jointForce[ret]; // params.m_jointAccel[ret] = (relVelocErr + penetrationErr) * params.m_invTimestep; SetMotorAcceleration(ret, (relVelocErr + penetrationErr) * params.m_invTimestep, params); ret++; } } return dgUnsigned32(ret); }
bool CometConquestEventListener::HandleEvent( IEventData const & event ) { if ( EvtData_Request_Start_Game::sk_EventType == event.VGetEventType() ) { m_CometConquest->VChangeState(BGS_WaitingForPlayers); } else if ( EvtData_Game_State::sk_EventType == event.VGetEventType() ) { const EvtData_Game_State & castEvent = static_cast< const EvtData_Game_State & >( event ); m_CometConquest->VChangeState(castEvent.m_gameState); } else if ( EvtData_Remote_Client::sk_EventType == event.VGetEventType() ) { // This event is always sent from clients to the game server. const EvtData_Remote_Client & castEvent = static_cast< const EvtData_Remote_Client & >( event ); const int sockID = castEvent.m_socketId; const int ipAddress = castEvent.m_ipAddress; // The teapot has already been created - we need to go find it. //ActorMap::iterator i = m_CometConquest->m_ActorList.begin(); //ActorMap::iterator end = m_CometConquest->m_ActorList.end(); //shared_ptr<IActor> actor = shared_ptr<BaseActor>(); //while (i != end) //{ // actor = (*i).second; // if (actor->VGetType() == AT_Ship) // { // shared_ptr<ActorParams> params = actor->VGetParams(); // shared_ptr<ShipParams> teapotParams = boost::static_pointer_cast<ShipParams>(params); // if (teapotParams->m_ViewId == VIEWID_NO_VIEW_ATTACHED) // { // break; // } // } // ++i; //} //if (actor != shared_ptr<BaseActor>()) //{ NetworkGameView *netGameView = GCC_NEW NetworkGameView( sockID ); shared_ptr<IGameView> gameView(netGameView); m_CometConquest->VAddView(gameView, sockID); extern void ListenForCometConquestViewEvents(EventListenerPtr listener); EventListenerPtr listener ( GCC_NEW NetworkEventForwarder( sockID ) ); ListenForCometConquestViewEvents( listener ); //} } else if ( EvtData_Network_Player_Actor_Assignment::sk_EventType == event.VGetEventType() ) { // we're a remote client getting an actor assignment. // the server assigned us a playerId when we first attached (the server's socketId, actually) const EvtData_Network_Player_Actor_Assignment & castEvent = static_cast< const EvtData_Network_Player_Actor_Assignment & >( event ); shared_ptr<IGameView> playersView(GCC_NEW CometConquestGameView(true)); playersView.get()->VOnAttach(castEvent.m_remotePlayerId, castEvent.m_actorId); m_CometConquest->VAddView(playersView, castEvent.m_actorId); } else if ( EvtData_PhysCollision::sk_EventType == event.VGetEventType() ) { const EvtData_PhysCollision & castEvent = static_cast< const EvtData_PhysCollision & >( event ); shared_ptr<IActor> pGameActorA = m_CometConquest->VGetActor(castEvent.m_ActorA); shared_ptr<IActor> pGameActorB = m_CometConquest->VGetActor(castEvent.m_ActorB); if (!pGameActorA || !pGameActorB) return false; int typeA = pGameActorA->VGetType(); int typeB = pGameActorB->VGetType(); //Bullets hitting things if(AT_Bullet == typeA && AT_Ship != typeB && AT_Floor != typeB) { m_CometConquest->VRemoveActor(pGameActorA->VGetID()); } if(AT_Bullet == typeB && AT_Ship != typeA && AT_Floor != typeA) { m_CometConquest->VRemoveActor(pGameActorB->VGetID()); } //Comets hitting boundry wall if(AT_Comet == typeB && AT_BoundryWall == typeA) { m_CometConquest->VRemoveActor(pGameActorB->VGetID()); } if(AT_Comet == typeA && AT_BoundryWall == typeB) { m_CometConquest->VRemoveActor(pGameActorA->VGetID()); } //Ship getting hit by a comet if(AT_Ship == typeA && AT_Comet == typeB) { ShipParams tp; tp.m_StartPosition = static_cast<ShipParams *>(pGameActorA->VGetParams().get())->m_StartPosition; tp.m_Mat = tp.m_StartPosition; tp.m_Length = 2.5; tp.m_ViewId = static_cast<ShipParams *>(pGameActorA->VGetParams().get())->m_ViewId; tp.m_Team = static_cast<ShipParams *>(pGameActorA->VGetParams().get())->m_Team; const EvtData_Request_New_Actor requestShip( &tp ); m_CometConquest->VRemoveActor(pGameActorA->VGetID()); safeTriggerEvent( requestShip ); } if(AT_Ship == typeB && AT_Comet == typeA) { ShipParams tp; tp.m_StartPosition = static_cast<ShipParams *>(pGameActorB->VGetParams().get())->m_StartPosition; tp.m_Mat = tp.m_StartPosition; tp.m_Length = 2.5; tp.m_ViewId = static_cast<ShipParams *>(pGameActorB->VGetParams().get())->m_ViewId; tp.m_Team = static_cast<ShipParams *>(pGameActorB->VGetParams().get())->m_Team; const EvtData_Request_New_Actor requestShip( &tp ); m_CometConquest->VRemoveActor(pGameActorB->VGetID()); safeTriggerEvent( requestShip ); } //Shooting players if(AT_Ship == typeA && AT_Bullet == typeB) { ShipParams tp; tp.m_StartPosition = static_cast<ShipParams *>(pGameActorA->VGetParams().get())->m_StartPosition; tp.m_Mat = tp.m_StartPosition; tp.m_Length = 2.5; //tp.m_Id = pGameActorA->VGetID(); tp.m_ViewId = static_cast<ShipParams *>(pGameActorA->VGetParams().get())->m_ViewId; tp.m_Team = static_cast<ShipParams *>(pGameActorA->VGetParams().get())->m_Team; const EvtData_Request_New_Actor requestShip( &tp ); //m_CometConquest->VRemoveActor(pGameActorB->VGetID()); safeTriggerEvent( requestShip ); m_CometConquest->VRemoveActor(pGameActorA->VGetID()); } if(AT_Ship == typeB && AT_Bullet == typeA) { ShipParams tp; tp.m_StartPosition = static_cast<ShipParams *>(pGameActorB->VGetParams().get())->m_StartPosition; tp.m_Mat = tp.m_StartPosition; tp.m_Length = 2.5; //tp.m_Id = pGameActorB->VGetID(); tp.m_ViewId = static_cast<ShipParams *>(pGameActorB->VGetParams().get())->m_ViewId; const EvtData_Request_New_Actor requestShip( &tp ); safeTriggerEvent( requestShip ); m_CometConquest->VRemoveActor(pGameActorB->VGetID()); } //Shooting Comets if(AT_Bullet == typeA && AT_Comet == typeB) { m_CometConquest->VRemoveActor(pGameActorB->VGetID()); //m_CometConquest->VRemoveActor(pGameActorA->VGetID()); } if(AT_Bullet == typeB && AT_Comet == typeA) { //m_CometConquest->VRemoveActor(pGameActorB->VGetID()); m_CometConquest->VRemoveActor(pGameActorA->VGetID()); } //Goal & Ring if(AT_Ring == typeA && AT_Goal == typeB) { RingParams rp; rp.m_StartPosition == static_cast<RingParams *>(pGameActorA->VGetParams().get())->m_StartPosition; rp.m_Mat = rp.m_StartPosition; const EvtData_Request_New_Actor requestRing (&rp); safeTriggerEvent (requestRing); m_CometConquest->VRemoveActor(pGameActorA->VGetID()); if(0 == static_cast<GoalParams*>(pGameActorB->VGetParams().get())->m_Team) { m_CometConquest->blueTeamScore(); } else { m_CometConquest->redTeamScore(); } } if(AT_Ring == typeB && AT_Goal == typeA) { RingParams rp; rp.m_StartPosition == static_cast<RingParams *>(pGameActorB->VGetParams().get())->m_StartPosition; rp.m_Mat = rp.m_StartPosition; const EvtData_Request_New_Actor requestRing (&rp); safeTriggerEvent (requestRing); m_CometConquest->VRemoveActor(pGameActorB->VGetID()); if(0 == static_cast<GoalParams*>(pGameActorA->VGetParams().get())->m_Team) { m_CometConquest->blueTeamScore(); } else { m_CometConquest->redTeamScore(); } } } else if ( EvtData_Thrust::sk_EventType == event.VGetEventType() ) { const EvtData_Thrust & castEvent = static_cast< const EvtData_Thrust & >( event ); shared_ptr<IActor> pActor = m_CometConquest->VGetActor(castEvent.m_id); if( pActor ) { static const float newtonForce = 1.f; float thrustForce = castEvent.m_throttle * newtonForce; Mat4x4 rotation = pActor->VGetMat(); rotation.SetPosition(Vec3(0,0,0)); Vec3 dir = rotation.Xform(g_Forward); dir.Normalize(); m_CometConquest->m_pPhysics->VApplyForce(dir, thrustForce, castEvent.m_id); } } else if ( EvtData_Steer::sk_EventType == event.VGetEventType() ) { static const float newtonForce = -.25 * 1.8f; const EvtData_Steer & castEvent = static_cast< const EvtData_Steer & >( event ); float steerForce = -castEvent.m_dir * newtonForce; m_CometConquest->m_pPhysics->VApplyTorque(Vec3(0,1,0), steerForce, castEvent.m_id); } else if ( EvtData_Fire_Weapon::sk_EventType == event.VGetEventType() ) { if(!this->m_CometConquest->m_bProxy) { const EvtData_Fire_Weapon & castEvent = static_cast< const EvtData_Fire_Weapon & >( event ); ActorId gunnerId = castEvent.m_id; shared_ptr<IActor> pGunner = m_CometConquest->VGetActor(gunnerId); if (pGunner) { //Calculate depth offset from the controller Vec4 at = g_Forward4 * 3.0f; Vec4 atWorld = pGunner->VGetMat().Xform(at); Vec3 normalDir(atWorld); normalDir.Normalize(); BulletParams sp; sp.m_Pos = pGunner->VGetMat().GetPosition() + Vec3(atWorld) * 3; sp.m_Radius = 0.25; sp.m_Segments = 16; sp.m_Color = g_Cyan; sp.m_NormalDir = normalDir; sp.m_Force = g_WeaponForce; sp.m_TeamFiredBy = static_cast<ShipParams *>(pGunner->VGetParams().get())->m_Team; //Request creation of this actor. const EvtData_Request_New_Actor cannonBallEvt( &sp ); safeTriggerEvent( cannonBallEvt ); return true; } } } else if ( EvtData_Move_Actor::sk_EventType == event.VGetEventType() ) { const EvtData_Move_Actor & castEvent = static_cast< const EvtData_Move_Actor & >( event ); m_CometConquest->VMoveActor(castEvent.m_Id, castEvent.m_Mat); } else if ( EvtData_Request_New_Actor::sk_EventType == event.VGetEventType() ) { const EvtData_Request_New_Actor & castEvent = static_cast< const EvtData_Request_New_Actor & >( event ); ActorParams * pActorParams = NULL; pActorParams = castEvent.m_pActorParams; //Did we get valid actor params? if ( NULL == pActorParams ) { assert( 0 && "Invalid parameters specified for actor!" ); return false; } //Valid params. const ActorId actorID = m_CometConquest->GetNewActorID(); pActorParams->m_Id = actorID; //Package as a new actor event. /* IEventDataPtr actorEvent( IEventDataPtr( GCC_NEW EvtData_New_Actor( actorID, pActorParams ) ) ); const bool bSuccess = safeQueEvent( actorEvent ); */ const EvtData_New_Actor actorEvent( actorID, pActorParams ); const bool bSuccess = safeTriggerEvent( actorEvent ); return bSuccess; } else if ( EvtData_New_Actor::sk_EventType == event.VGetEventType() ) { const EvtData_New_Actor & castEvent = static_cast< const EvtData_New_Actor & >( event ); ActorParams * pActorParams = castEvent.m_pActorParams; if ( NULL == pActorParams ) { assert( 0 && "Received a new actor event with NULL actor parameters!" ); return false; } pActorParams->VCreate(m_CometConquest); if ( false == castEvent.m_id ) { assert( 0 && "Unable to construct desired actor type!" ); return false; } } return false; }