void CPhysicsManager::ApplyExplosion(NxActor* _pActor,const Vect3f& _vPosSphere, float _fEffectRadius, float _fPower)
{

	Vect3f l_vVelocityDirection;
	Vect3f l_vActorPosition;
	float l_fDistance;
	float l_fTotalPower;

	NxVec3 l_vPos = _pActor->getGlobalPosition();
	l_vActorPosition = Vect3f(l_vPos.x,l_vPos.y,l_vPos.z);

	l_vVelocityDirection = l_vActorPosition-_vPosSphere;
	l_vVelocityDirection.Normalize();

	l_fDistance = _vPosSphere.Distance(l_vActorPosition);
	l_fTotalPower = _fPower*((_fEffectRadius-l_fDistance)/_fEffectRadius);

	NxF32 coeff = _pActor->getMass() * l_fTotalPower;
	NxVec3 l_vDirection(l_vVelocityDirection.x,l_vVelocityDirection.y,l_vVelocityDirection.z);
	_pActor->addForceAtLocalPos(l_vDirection*coeff, NxVec3(0,0,0), NX_IMPULSE,true);

}
CPhysicUserData* CPhysicsManager::RaycastClosestActorShoot (const Vect3f posRay, const Vect3f& dirRay, uint32 impactMask, SCollisionInfo& info, float _fPower)
{

	//NxUserRaycastReport::ALL_SHAPES
	assert(m_pScene != NULL);

	NxRay ray; 
	ray.dir =  NxVec3(dirRay.x, dirRay.y, dirRay.z);
	ray.orig = NxVec3(posRay.x, posRay.y, posRay.z);

	NxRaycastHit hit;
	NxShape* closestShape = NULL;

	closestShape = m_pScene->raycastClosestShape(ray, NX_ALL_SHAPES, hit, impactMask);
	if (!closestShape) 
	{
		//No hemos tokado a ningún objeto físico de la escena.
		return NULL;
	}
	NxActor* actor = &closestShape->getActor();
	CPhysicUserData* impactObject =(CPhysicUserData*)actor->userData;
	//Si está petando aquí quiere decir que se ha registrado un objeto físico sin proporcionarle UserData
	assert(impactObject);

	info.m_fDistance	= hit.distance;
	info.m_Normal				= Vect3f(hit.worldNormal.x, hit.worldNormal.y, hit.worldNormal.z ); 
	info.m_CollisionPoint	= Vect3f(hit.worldImpact.x, hit.worldImpact.y, hit.worldImpact.z ); 

	Vect3f l_vDirection(dirRay.x-posRay.x,dirRay.y-posRay.y,dirRay.z-posRay.z);
	l_vDirection.Normalize();

	NxVec3 l_vDirectionVec(dirRay.x,dirRay.y,dirRay.z); 
	NxF32 coeff = actor->getMass() * _fPower;
	actor->addForceAtLocalPos(l_vDirectionVec*coeff, NxVec3(0,0,0), NX_IMPULSE,true);


	return impactObject;
}
Example #3
0
bool CPhysXProcess::ExecuteProcessAction(float _fDeltaSeconds, float _fDelta, const char* _pcAction)
{

  float l_pVelocity = 1/20;

  if(strcmp(_pcAction, "Run") == 0)
  {
    m_fVelocity = 7;
    
    return true;
  }

  if(strcmp(_pcAction, "Walk") == 0)
  {
    m_fVelocity = 2;
    
    return true;
  }

  if(strcmp(_pcAction, "MoveFwd") == 0)
  {
    Vect3f l_vDir = m_pCamera->GetDirection();

    
    l_vDir.Normalize();
    g_pPhysXController->Move(Vect3f(l_vDir.x,0.0f,l_vDir.z)*_fDeltaSeconds*m_fVelocity,_fDeltaSeconds);
    Vect3f l_ControllerPos = g_pPhysXController->GetPosition();
    m_pObject->SetPosition(Vect3f(l_ControllerPos.x,l_ControllerPos.y,l_ControllerPos.z));
    if (g_pCharacter)
    {
      g_pCharacter->SetPosition(Vect3f(l_ControllerPos.x,l_ControllerPos.y-ALTURA_TOTAL,l_ControllerPos.z));
      /*if(g_pCharacter->GetAnimatedInstanceModel()->GetCurrentCycle() != 1)
	    {
		    g_pCharacter->GetAnimatedInstanceModel()->ClearCycle(0.3f);
		    g_pCharacter->GetAnimatedInstanceModel()->BlendCycle(1,0.3f);
	    }*/
    }

    if(m_iState != 1)
      m_bStateChanged = true;

    m_iState = 1;

    return true;
  }

 if(strcmp(_pcAction, "MoveBack") == 0)
  {
    Vect3f l_vDir = m_pCamera->GetDirection();

    
    l_vDir.Normalize();
    g_pPhysXController->Move(Vect3f(-l_vDir.x,0.0f,-l_vDir.z)*_fDeltaSeconds*m_fVelocity,_fDeltaSeconds);
    Vect3f l_ControllerPos = g_pPhysXController->GetPosition();
    m_pObject->SetPosition(Vect3f(l_ControllerPos.x,l_ControllerPos.y,l_ControllerPos.z));
    if (g_pCharacter)
    {
      g_pCharacter->SetPosition(Vect3f(l_ControllerPos.x,l_ControllerPos.y-ALTURA_TOTAL,l_ControllerPos.z));
      /*if(g_pCharacter->GetAnimatedInstanceModel()->GetCurrentCycle() != 1)
	    {
		    g_pCharacter->GetAnimatedInstanceModel()->ClearCycle(0.3f);
		    g_pCharacter->GetAnimatedInstanceModel()->BlendCycle(1,0.3f);
	    }*/
    }

    if(m_iState != 1)
      m_bStateChanged = true;

    m_iState = 1;

    return true;
  }

  if(strcmp(_pcAction, "MoveLeft") == 0)
  {
    Vect3f l_vDir = m_pObject->GetPosition();
    l_vDir.x = cos(m_pObject->GetYaw()+FLOAT_PI_VALUE/2);
    l_vDir.z = sin(m_pObject->GetYaw()+FLOAT_PI_VALUE/2);
    l_vDir.y = 0.0f;
 
    l_vDir.Normalize();
    g_pPhysXController->Move(Vect3f(l_vDir.x,0.0f,l_vDir.z)*_fDeltaSeconds*m_fVelocity,_fDeltaSeconds);
    Vect3f l_ControllerPos = g_pPhysXController->GetPosition();
    m_pObject->SetPosition(Vect3f(l_ControllerPos.x,l_ControllerPos.y,l_ControllerPos.z));
    if (g_pCharacter)
    {
      g_pCharacter->SetPosition(Vect3f(l_ControllerPos.x,l_ControllerPos.y-ALTURA_TOTAL,l_ControllerPos.z));
      /*if(g_pCharacter->GetAnimatedInstanceModel()->GetCurrentCycle() != 1)
	    {
		    g_pCharacter->GetAnimatedInstanceModel()->ClearCycle(0.3f);
		    g_pCharacter->GetAnimatedInstanceModel()->BlendCycle(1,0.3f);
	    }*/
    }


    if(m_iState != 1)
      m_bStateChanged = true;

    m_iState = 1;

    return true;
  }

  if(strcmp(_pcAction, "MoveRight") == 0)
  {
    Vect3f l_vDir = m_pObject->GetPosition();
    l_vDir.x = cos(m_pObject->GetYaw()+FLOAT_PI_VALUE/2);
    l_vDir.z = sin(m_pObject->GetYaw()+FLOAT_PI_VALUE/2);
    l_vDir.y = 0.0f;

    l_vDir.Normalize();
    g_pPhysXController->Move(Vect3f(-l_vDir.x,0.0f,-l_vDir.z)*_fDeltaSeconds*m_fVelocity,_fDeltaSeconds);
    Vect3f l_ControllerPos = g_pPhysXController->GetPosition();
    m_pObject->SetPosition(Vect3f(l_ControllerPos.x,l_ControllerPos.y,l_ControllerPos.z));
    if (g_pCharacter)
    {
      g_pCharacter->SetPosition(Vect3f(l_ControllerPos.x,l_ControllerPos.y-ALTURA_TOTAL,l_ControllerPos.z));
      /*if(g_pCharacter->GetAnimatedInstanceModel()->GetCurrentCycle() != 1)
	    {
		    g_pCharacter->GetAnimatedInstanceModel()->ClearCycle(0.3f);
		    g_pCharacter->GetAnimatedInstanceModel()->BlendCycle(1,0.3f);
	    }*/
    }

    if(m_iState != 1)
      m_bStateChanged = true;

    m_iState = 1;

    return true;
  }


  if(strcmp(_pcAction, "Elevate") == 0)
  {
    
    ///////////////////////////////////////////////////////////////////////////////
    // RAGDOLL PROVES
    ///////////////////////////////////////////////////////////////////////////////

    CRenderableAnimatedInstanceModel* l_pAnim = (CRenderableAnimatedInstanceModel*)CORE->GetRenderableObjectsManager()->GetResource("rigglebot");
    CalSkeleton* l_pSkeleton = l_pAnim->GetAnimatedInstanceModel()->GetAnimatedCalModel()->getSkeleton();
    l_pSkeleton->getCoreSkeleton()->calculateBoundingBoxes(l_pAnim->GetAnimatedInstanceModel()->GetAnimatedCalModel()->getCoreModel());
    l_pSkeleton->calculateBoundingBoxes();
    if (g_pRagdoll == 0)
    {
      g_pRagdoll = new CPhysxSkeleton(false);
      CalModel* l_pCalModel = l_pAnim->GetAnimatedInstanceModel()->GetAnimatedCalModel();
      g_pRagdoll->Init("Data/Animated Models/Riggle/Skeleton.xml",l_pCalModel,l_pAnim->GetMat44(),GROUP_COLLIDABLE_PUSHABLE);
    }

    ///////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////

  }

  if(strcmp(_pcAction, "ShootBall") == 0)
  {
      if (g_pCharacter)
      {
        g_pCharacter->GetAnimatedInstanceModel()->ExecuteAction(3,0.0f);
      }

      
      
      //g_pPActorComposite->SetLinearVelocity(Vect3f(0.0f,1.0f,0.0f)*m_fPhysxVelocity);
      CPhysicsManager* l_pPhysManager = CORE->GetPhysicsManager();
      const Vect3f l_PosCamera = m_pCamera->GetEye();
      const Vect3f& l_DirCamera = m_pCamera->GetDirection().Normalize();

       CalSkeleton* l_pSkeleton = g_pCharacter->GetAnimatedInstanceModel()->GetAnimatedCalModel()->getSkeleton();
      int l_iBoneId = l_pSkeleton->getCoreSkeleton()->getCoreBoneId("Bip01 R Hand");
      CalBone* l_pBone = l_pSkeleton->getBone(l_iBoneId);
      CalVector l_vPos = l_pBone->getTranslationAbsolute();
      Mat44f l_vMat44 = g_pCharacter->GetMat44();
      Vect3f l_vVect(-l_vPos.x, l_vPos.y, l_vPos.z);
      l_vVect = l_vMat44*l_vVect;
  
      SCollisionInfo l_CInfo;
      if (g_pUserDataSHOOT != 0)
      {
        g_pUserDataSHOOT->SetColor(colWHITE);
      }
      
      g_pUserDataSHOOT = l_pPhysManager->RaycastClosestActor(l_PosCamera,l_DirCamera,4,l_CInfo);
      Vect3f l_vDirection(l_CInfo.m_CollisionPoint.x - l_vVect.x, l_CInfo.m_CollisionPoint.y - l_vVect.y, l_CInfo.m_CollisionPoint.z - l_vVect.z);
      l_vDirection.Normalize();


      if (g_pUserDataSHOOT != 0)
      {
        if (g_pRagdoll != 0)
        {
          if (g_pRagdoll->IsRagdollPhysXActor(g_pUserDataSHOOT->GetName()))
          {
            if (!g_pRagdoll->IsRagdollActive())
            {
              g_pRagdoll->SetRagdollActive(true);
            }
          }

        }
        g_pUserDataSHOOT->SetColor(colRED);
      }


      g_pUserDataSHOOT = l_pPhysManager->RaycastClosestActorShoot(l_vVect,l_vDirection,4,l_CInfo, 20.0f);
      g_vCollisions.push_back(l_CInfo);

      

  }

  if(strcmp(_pcAction, "ShootBOX") == 0)
  {
    /*CPhysicActor* l_pPActorShoot = new CPhysicActor(g_pUserData);
    l_pPActorShoot->AddBoxSphape(0.2f,m_pCamera->GetEye());
    l_pPActorShoot->CreateBody(3);
    CORE->GetPhysicsManager()->AddPhysicActor(l_pPActorShoot);
    l_pPActorShoot->SetLinearVelocity(m_pCamera->GetDirection()*m_fPhysxVelocity);
    CHECKED_DELETE(l_pPActorShoot)*/
  
    ///////////////////////////////////////////////////////////////////////////////
    // RAGDOLL PROVES
    ///////////////////////////////////////////////////////////////////////////////
  
    //CRenderableAnimatedInstanceModel* l_pAnim = (CRenderableAnimatedInstanceModel*)CORE->GetRenderableObjectsManager()->GetResource("rigglebot");
    //CalSkeleton* l_pSkeleton = l_pAnim->GetAnimatedInstanceModel()->GetAnimatedCalModel()->getSkeleton();
    //l_pSkeleton->getCoreSkeleton()->calculateBoundingBoxes(l_pAnim->GetAnimatedInstanceModel()->GetAnimatedCalModel()->getCoreModel());
    //l_pSkeleton->calculateBoundingBoxes();
  
    //if (g_pRagdoll == 0)
    //{
  
    //  int l_iId = l_pAnim->GetAnimatedInstanceModel()->GetAnimatedCoreModel()->GetCoreModel()->getCoreAnimationId("dead");
    //  g_fRagdollFrames = l_pAnim->GetAnimatedInstanceModel()->GetAnimatedCoreModel()->GetCoreModel()->getCoreAnimation(l_iId)->getTotalNumberOfKeyframes();
    //  g_fRagdollAnimationDuration = l_pAnim->GetAnimatedInstanceModel()->GetAnimatedCoreModel()->GetCoreModel()->getCoreAnimation(l_iId)->getDuration();
    //  //g_fRagdollAnimationDuration = l_pAnim->GetAnimatedInstanceModel()->GetAnimatedCoreModel()->GetCoreModel()->getCoreAnimation(l_iId)->
  
    //  l_pAnim->GetAnimatedInstanceModel()->ClearCycle(0.5f);
    //  l_pAnim->GetAnimatedInstanceModel()->BlendCycle("dead",0.5f);
    //  g_pRagdoll = new CPhysxSkeleton();
    //  //RENDER_MANAGER->SetTransform(l_pAnim->GetMat44());
    //  CalModel* l_pCalModel = l_pAnim->GetAnimatedInstanceModel()->GetAnimatedCalModel();
    //  
    //  
    //  //l_pAnim->SetVisible(false);
    //  
  
    // /* g_pRagdoll = new CPhysxRagdoll("Ragdoll Prova");
    //  g_pRagdoll->Load("Data/Animated Models/Riggle/Ragdoll.xml",false);
    //  g_pRagdoll->InitSkeleton(l_pSkeleton);*/
  
    //}

    if (g_pRagdoll != 0)
    {
      if(!g_pRagdoll->IsRagdollActive())
      {
        g_pRagdoll->SetRagdollActive(true);
      }else{
        Mat44f l_mTransform = g_pRagdoll->GetTransform();

        Mat44f l_mNewTransform;
        Mat44f l_mNewTransform2;
        l_mNewTransform.SetIdentity();
        l_mNewTransform2.SetIdentity();
        l_mNewTransform.RotByAngleY(l_mTransform.GetYaw());
        l_mNewTransform2.Translate(Vect3f(l_mTransform.GetTranslationVector().x,0.0f,l_mTransform.GetTranslationVector().z));

        g_pRagdoll->SetRagdollActive(false);

        g_pRagdoll->SetTransform(l_mNewTransform2*l_mNewTransform);
      }
    }

    if (g_pCharacter)
    {
      //g_pCharacter->SetPosition(Vect3f(l_ControllerPos.x,l_ControllerPos.y-ALTURA_TOTAL,l_ControllerPos.z));
      //if(g_pCharacter->GetAnimatedInstanceModel()->GetCurrentCycle() != 5)
	    //{
		  //  g_pCharacter->GetAnimatedInstanceModel()->ClearCycle(0.3f);
		  //  g_pCharacter->GetAnimatedInstanceModel()->BlendCycle(5,0.3f);
	    //}
      //else
      //{
      //  g_pCharacter->GetAnimatedInstanceModel()->ClearCycle(0.3f);
		  //  g_pCharacter->GetAnimatedInstanceModel()->BlendCycle(0,0.3f);
      //}
    }


  }

  if(strcmp(_pcAction, "VelocityChange") == 0)
  {
    Vect3i l_vDelta = INPUT_MANAGER->GetMouseDelta();

    if (l_vDelta.z < 0)
    {
      m_fPhysxVelocity = m_fPhysxVelocity - 2;
      if (m_fPhysxVelocity == 0)
        m_fPhysxVelocity = 2;
      
    }
    else
    {
       m_fPhysxVelocity = m_fPhysxVelocity + 2;
      if (m_fPhysxVelocity == 1000)
        m_fPhysxVelocity = 998;
      
    }
 
    
  }

  if(strcmp(_pcAction, "ZoomCamera") == 0)
  {
    Vect3i l_vDelta = INPUT_MANAGER->GetMouseDelta();

    if (l_vDelta.z < 0)
    {
      ((CThPSCamera*)m_pObjectCamera)->AddZoom(0.3f);
    }else{
      ((CThPSCamera*)m_pObjectCamera)->AddZoom(-0.3f);
    }

    return true;
  }

  if(strcmp(_pcAction, "RenderPhysX") == 0)
  {
    bool l_bRender = CORE->GetPhysicsManager()->GetDebugRenderMode();
    CORE->GetPhysicsManager()->SetDebugRenderMode(!l_bRender);
    //g_pCharacter->SetVisible(!g_pCharacter->GetVisible());
    //ExportSkeletonInfo(g_pCharacter->GetAnimatedInstanceModel()->GetAnimatedCalModel()->getSkeleton());
  }

  if(strcmp(_pcAction, "ShootGrenade") == 0)
  {
     if (g_pCharacter)
      {
        g_pCharacter->GetAnimatedInstanceModel()->ExecuteAction(3,0.0f);
      }
      
      //g_pPActorComposite->SetLinearVelocity(Vect3f(0.0f,1.0f,0.0f)*m_fPhysxVelocity);
      CPhysicsManager* l_pPhysManager = CORE->GetPhysicsManager();
      const Vect3f l_PosCamera = m_pCamera->GetEye();
      const Vect3f& l_DirCamera = m_pCamera->GetDirection().Normalize();

      CalSkeleton* l_pSkeleton = g_pCharacter->GetAnimatedInstanceModel()->GetAnimatedCalModel()->getSkeleton();
      int l_iBoneId = l_pSkeleton->getCoreSkeleton()->getCoreBoneId("Bip01 R Hand");
      CalBone* l_pBone = l_pSkeleton->getBone(l_iBoneId);
      CalVector l_vPos = l_pBone->getTranslationAbsolute();
      Mat44f l_vMat44 = g_pCharacter->GetMat44();
      //l_vMat44.Translate(Vect3f(0.1f,0.0f,0.0f));
      Vect3f l_vVect(-l_vPos.x, l_vPos.y, l_vPos.z);
      l_vVect = l_vMat44*l_vVect;
  
      SCollisionInfo l_CInfo;
      if (g_pUserDataSHOOT != 0)
      {
        g_pUserDataSHOOT->SetColor(colWHITE);
      }
      
      g_pUserDataSHOOT = l_pPhysManager->RaycastClosestActor(l_PosCamera,l_DirCamera,6,l_CInfo);
      Vect3f l_vDirection(l_CInfo.m_CollisionPoint.x - l_vVect.x, l_CInfo.m_CollisionPoint.y - l_vVect.y, l_CInfo.m_CollisionPoint.z - l_vVect.z);
      l_vDirection.Normalize();

      CPhysxGrenade* l_pGrenade = new CPhysxGrenade("Granada" + g_vGrenadesVector.size(),7.0f,3.0f,25.0f);
      if (g_pUserDataSHOOT != 0)
      {
        l_pGrenade->Init(0.1f,1.0f,GROUP_COLLIDABLE_PUSHABLE,l_vVect,l_vDirection,20.0f,"granada"); 
      }
      else
      {
        Vect3f l_vDirectionAir = l_DirCamera*10.0f;
        l_vDirectionAir = l_vDirectionAir-l_vVect;
        l_vDirectionAir.Normalize();
        l_pGrenade->Init(0.1f,1.0f,GROUP_COLLIDABLE_PUSHABLE,l_vVect,l_vDirectionAir,20.0f,"granada");
      }
      g_vGrenadesVector.push_back(l_pGrenade);

  }
  return false;
}