void CCharacterControllerEnemy::Update(void)
{
    float fTrackTexCoordOffsetMoveFactor  = 0.2f;
    
    m_vRotation.y = CMathHelper::Instance()->Get_WrapAngle(m_vRotation.y, 0.0f, 360.0f);
    
    switch (m_eState)
    {
        case E_AI_STATE_NONE:
            std::cout<<"[CCharacterControllerEnemy::Update]"<<"state -> AI_STATE_NONE"<<std::endl;
            m_pChassis->StartExhaustEmitt(false);
            m_pChassis->StartDirtEmitt(false);
            break;
        case E_AI_STATE_STAND:
        {
            m_pChassis->StartExhaustEmitt(false);
            m_pChassis->StartDirtEmitt(false);
            std::cout<<"[CCharacterControllerEnemy::Update]"<<"state -> E_AI_STATE_STAND"<<std::endl;
        }
            break;
        case E_AI_STATE_MOVE:
        {
            float fMoveSpeed = GameSDBStorage::Instance()->Get_UserMoveSpeed(m_eFullSetType);
            glm::vec2 vPoint_01 = glm::vec2(m_vPosition.x, m_vPosition.z);
            glm::vec2 vPoint_02 = glm::vec2(m_vMovePoint.x, m_vMovePoint.z);
            
            if(glm::distance(vPoint_01, vPoint_02) < 1.0f)
            {
                m_eState = E_AI_STATE_NONE;
                break;
            }
            
            float fAngleFromSeekerToTarget = CMathHelper::Instance()->Get_RotationBetweenPoints(m_vMovePoint, m_vPosition) + 180.0f;
            //glm::degrees(CMathHelper::Instance()->Get_RotationBetweenPointsDot(vPoint_01, vPoint_02));
            fAngleFromSeekerToTarget = CMathHelper::Instance()->Get_WrapAngle(fAngleFromSeekerToTarget, 0.0f, 360.0f);
            if(m_eStreerMode == E_AI_STEER_MODE_LEFT)
            {
                if(fabsf(fAngleFromSeekerToTarget - m_vRotation.y) > 45.0f)
                {
                    m_pNavigationHelper->MoveLeft();
                    m_pTrack->Move_LeftTrack(-fTrackTexCoordOffsetMoveFactor);
                    m_pTrack->Move_RightTrack(fTrackTexCoordOffsetMoveFactor);
                }
                else
                {
                    if(fabs(fAngleFromSeekerToTarget - m_vRotation.y) > 1.0f)
                    {
                        m_pNavigationHelper->MoveLeft();
                    }
                    //MoveForward();
                    if(m_pBox2dBody != NULL)
                    {
                        b2Vec2 vForce = b2Vec2(0.0f, 0.0f);
                        vForce.x += sinf(glm::radians(m_vRotation.y)) * fMoveSpeed;
                        vForce.y += cosf(glm::radians(m_vRotation.y)) * fMoveSpeed;
                        m_pBox2dBody->SetAwake(true);
                        m_pBox2dBody->SetLinearVelocity(vForce);
                    }
                    m_pTrack->Move_LeftTrack(-fTrackTexCoordOffsetMoveFactor);
                    m_pTrack->Move_RightTrack(-fTrackTexCoordOffsetMoveFactor);
                }
            }
            else if(m_eStreerMode == E_AI_STEER_MODE_RIGHT)
            {
                if(fabsf(fAngleFromSeekerToTarget - m_vRotation.y) > 45.0f)
                {
                    m_pNavigationHelper->MoveRight();
                    m_pTrack->Move_LeftTrack(-fTrackTexCoordOffsetMoveFactor);
                    m_pTrack->Move_RightTrack(fTrackTexCoordOffsetMoveFactor);
                }
                else
                {
                    if(fabs(fAngleFromSeekerToTarget - m_vRotation.y) > 1.0f)
                    {
                        m_pNavigationHelper->MoveRight();
                    }
                    //MoveForward();
                    if(m_pBox2dBody != NULL)
                    {
                        b2Vec2 vForce = b2Vec2(0.0f, 0.0f);
                        vForce.x += sinf(glm::radians(m_vRotation.y)) * fMoveSpeed;
                        vForce.y += cosf(glm::radians(m_vRotation.y)) * fMoveSpeed;
                        m_pBox2dBody->SetAwake(true);
                        m_pBox2dBody->SetLinearVelocity(vForce);
                    }
                    m_pTrack->Move_LeftTrack(-fTrackTexCoordOffsetMoveFactor);
                    m_pTrack->Move_RightTrack(-fTrackTexCoordOffsetMoveFactor);
                }
            }
            m_pChassis->StartExhaustEmitt(true);
            m_pChassis->StartDirtEmitt(true);
        }
            break;
        default:
            break;
    }
    
    if(m_pBox2dBody != NULL)
    {
        m_pBox2dBody->SetTransform(m_pBox2dBody->GetPosition(), glm::radians(m_vRotation.y));
    }
    
    m_pTrack->Update();
    m_pTower->Update();
    m_pChassis->Update();
    
    //Shoot();
    
    glm::vec3 vTargetPoint;
    if(m_pTarget != NULL)
    {
        vTargetPoint = m_pTarget->Get_Position();
    }
    else
    {
        vTargetPoint = m_vMovePoint;
    }
    
    //m_fTowerRotationY = CMathHelper::Instance()->Get_RotationBetweenPoints(m_vPosition, vTargetPoint);
    m_vRotation.y = CMathHelper::Instance()->Get_RotationBetweenPoints(m_vPosition, vTargetPoint);
    
    /*glm::vec3 vStepPosition = m_vPosition;
    vStepPosition.x += sinf(glm::radians(m_vRotation.y)) * m_fMoveSpeed;
    vStepPosition.z += cosf(glm::radians(m_vRotation.y)) * m_fMoveSpeed;
    m_fTowerRotationY = CMathHelper::Instance()->Get_RotationBetweenPointsDot(m_vPosition - vStepPosition, m_vPosition - vTargetPoint);
    */
    Set_Position(m_vPosition);
    _SmoothRotation();
    Set_Rotation(m_vRotation);
}
void CCharacterControllerPlayer::Update(void)
{   
    float fTrackTexCoordOffsetMoveFactor  = 0.2f;
    float fTrackTexCoordOffsetSteerFactor = 0.1f;
    
    ICamera* pCamera = CSceneMgr::Instance()->Get_Camera();
    switch (m_eMoveState)
    {
        case ICharacterController::E_CHARACTER_CONTROLLER_MOVE_STATE_NONE:
            if(pCamera->Get_FovY() > k_CAMERA_FOV_Y_STATE_NONE)
            {
                pCamera->Set_FovY(pCamera->Get_FovY() - k_CAMERA_FOV_Y_DELTA_STATE_NONE);
            }
            m_pChassis->StartExhaustEmitt(false);
            m_pChassis->StartDirtEmitt(false);
            break;
        case ICharacterController::E_CHARACTER_CONTROLLER_MOVE_STATE_FORWARD:
            
            //MoveForward();
            m_pTrack->Move_LeftTrack(-fTrackTexCoordOffsetMoveFactor);
            m_pTrack->Move_RightTrack(-fTrackTexCoordOffsetMoveFactor);
            
            if(pCamera->Get_FovY() < k_CAMERA_FOV_Y_STATE_FORWARD)
            {
                pCamera->Set_FovY(pCamera->Get_FovY() + k_CAMERA_FOV_Y_DELTA_STATE_FORWARD);
            }
            m_pChassis->StartExhaustEmitt(true);
            m_pChassis->StartDirtEmitt(true);
            break;
        case ICharacterController::E_CHARACTER_CONTROLLER_MOVE_STATE_BACKWARD:
            
            //MoveBackward();
            m_pTrack->Move_LeftTrack(fTrackTexCoordOffsetMoveFactor);
            m_pTrack->Move_RightTrack(fTrackTexCoordOffsetMoveFactor);
            
            if(pCamera->Get_FovY() > k_CAMERA_FOV_Y_STATE_BACKWARD)
            {
                pCamera->Set_FovY(pCamera->Get_FovY() - k_CAMERA_FOV_Y_DELTA_STATE_BACKWARD);
            }
            m_pChassis->StartExhaustEmitt(true);
            m_pChassis->StartDirtEmitt(true);
            break;
        default:
            break;
    }
    
    switch (m_eSteerState)
    {
        case ICharacterController::E_CHARACTER_CONTROLLER_STEER_STATE_NONE:
            
            break;
        case ICharacterController::E_CHARACTER_CONTROLLER_STEER_STATE_LEFT:
            m_pNavigationHelper->SteerRight();
            m_pTrack->Move_LeftTrack(-fTrackTexCoordOffsetMoveFactor);
            m_pTrack->Move_RightTrack(fTrackTexCoordOffsetSteerFactor);
            m_pChassis->StartExhaustEmitt(true);
            m_pChassis->StartDirtEmitt(true);
            break;
        case ICharacterController::E_CHARACTER_CONTROLLER_STEER_STATE_RIGHT:
            m_pNavigationHelper->SteerLeft();
            m_pTrack->Move_LeftTrack(fTrackTexCoordOffsetSteerFactor);
            m_pTrack->Move_RightTrack(-fTrackTexCoordOffsetMoveFactor);
            m_pChassis->StartExhaustEmitt(true);
            m_pChassis->StartDirtEmitt(true);
            break;
        default:
            break;
    }
    
    /*if(fabsf(CSettings::g_fAccellerometer_Y) > 0.1f && fabsf(m_fTowerRotationY + CSettings::g_fAccellerometer_Y * m_fTowerSteerSpeed) < 60.0f)
    {
        m_fTowerRotationY += CSettings::g_fAccellerometer_Y * m_fTowerSteerSpeed;
    }
    
    std::cout<<"[ BODY ANGLE ] :"<<_Get_WrapAngle(m_vRotation.y, 0.0f, 360.0f)<<std::endl;
    std::cout<<"[ TOWER ANGLE ] :"<<_Get_WrapAngle(m_fTowerRotationY + m_vRotation.y, 0.0f, 360.0f)<<std::endl;
   
    if(fabsf(_Get_WrapAngle(m_vRotation.y, 0.0f, 360.0f) - _Get_WrapAngle(m_fTowerRotationY + m_vRotation.y, 0.0f, 360.0f)) > 60.0f)
    {
        if(_Get_WrapAngle(m_vRotation.y, 0.0f, 360.0f) < _Get_WrapAngle((m_fTowerRotationY + m_vRotation.y), 0.0f, 360.0f))
        {
            m_fTowerRotationY -= m_fSteerSpeed;
            SteerLeft();
        }
        else
        {
            m_fTowerRotationY += m_fSteerSpeed;
            SteerRight();
        }
    }*/
    
    float fTowerSteerSpeed = GameSDBStorage::Instance()->Get_UserSteerSpeed(m_eFullSetType);
    switch (m_eSteerTowerState)
    {
        case ICharacterController::E_CHARACTER_CONTROLLER_STEER_STATE_TOWER_NONE:
            
            break;
        case ICharacterController::E_CHARACTER_CONTROLLER_STEER_STATE_TOWER_RIGHT:
        {
            if((m_fTowerRotationY + fTowerSteerSpeed) > 60.0f)
            {
                m_pNavigationHelper->MoveLeft();
            }
            else
            {
                m_fTowerRotationY += fTowerSteerSpeed;
            }
        }
            break;
        case ICharacterController::E_CHARACTER_CONTROLLER_STEER_STATE_TOWER_LEFT:
        {
            if((m_fTowerRotationY - fTowerSteerSpeed) < -60.0f)
            {
                m_pNavigationHelper->MoveRight();
            }
            else
            {
                m_fTowerRotationY -= fTowerSteerSpeed;
            }
        }
            break;
            
        default:
            break;
    }
    
    float fMoveSpeed = GameSDBStorage::Instance()->Get_UserMoveSpeed(m_eFullSetType);

    if(m_pBox2dBody != NULL)
    {
        b2Vec2 vForce = b2Vec2(0.0f, 0.0f);
        switch (m_eMoveState)
        {
            case 1:
            {
                vForce.x += sinf(glm::radians(m_vRotation.y)) * fMoveSpeed;
                vForce.y += cosf(glm::radians(m_vRotation.y)) * fMoveSpeed;
            }
                break;
            case 2:
            {
                vForce.x -= sinf(glm::radians(m_vRotation.y)) * fMoveSpeed;
                vForce.y -= cosf(glm::radians(m_vRotation.y)) * fMoveSpeed;
            }
                break;
            default:
                break;
        }
        
        m_pBox2dBody->SetAwake(true);
        m_pBox2dBody->SetLinearVelocity(vForce);
    }
    
    if(m_pBox2dBody != NULL)
    {
        m_pBox2dBody->SetTransform(m_pBox2dBody->GetPosition(), glm::radians(m_vRotation.y));
    }
    
    /*if(m_pBox2dBody != NULL)
    {
        _UpdateFriction();
        _UpdateDrive(static_cast<int>(m_eMoveState));
        _UpdateTurn(static_cast<int>(m_eSteerState));
    }*/
    
    glm::vec3 vTargetDecalPosition = m_pTargetDecal->Get_Position();
    vTargetDecalPosition.x = m_vPosition.x + sin(glm::radians(m_fTowerRotationY + m_vRotation.y)) * 6.0f;
    vTargetDecalPosition.z = m_vPosition.z + cos(-glm::radians(m_fTowerRotationY + m_vRotation.y)) * 6.0f;
    m_pTargetDecal->Set_Position(vTargetDecalPosition);
    
    m_pTrack->Update();
    m_pTower->Update();
    m_pChassis->Update();

    Set_Position(m_vPosition);
    _SmoothRotation();
    Set_Rotation(m_vRotation);
}
void Run_Static_Test(void * Contact_Addr, void * Contact_DDR)
{
	int Cur_servo_pos = 1400;
	int Servo_step = 0;
	double Cur_angle = 0;
	double Angle_step = 0;
	int Current_Euler_data[3];
	int Prev_Euler_data[3];
	Change_Mode(SERVO1, WHEEL, Data_out);	
	Get_Zero_Pos(Contact_Addr, Contact_DDR);
	
	//printf("Finished Zeroing servo\n");
	
	usleep(1000*30);
	
	Change_Mode(SERVO1, MULTI, Data_out);
	
	usleep(1000*30);
	
	uint8_t Error_array[15];
	
	uint8_t Check_sum = (~(SERVO1 + MX_GOAL_LENGTH + MX_WRITE_DATA + 20)) & 0xFF;
	uint8_t Data_packet_2[9] = {MX_START,MX_START,SERVO1,MX_GOAL_LENGTH,MX_WRITE_DATA, 20, 0, 0,Check_sum};
	UART_WriteRead(Data_packet_2, sizeof(Data_packet_2), ONE_BYTE_READ, Error_array);
	
	printf("Step Size,Roll,Pitch,Yaw\n");
	
	double i;
	int j;
	for(i = 0.01; i < 0.5 ; i += 0.03)
	{
		Get_Orientation(Prev_Euler_data);
		
		Angle_step = i;
		for(j = 0; j<3;j++)
		{
			//printf("Entered second for loop\n");
			Cur_angle += Angle_step;
			
			//Find angles in radians
			double Cur_angle_rad  =  Cur_angle  * (PI/180);
			double Angle_step_rad =  Angle_step * (PI/180);
			
			Servo_step = HINGE_DISTANCE * THREADS_PER_INCH * COUNTS_PER_360 * (tan(Cur_angle_rad) - tan(Cur_angle_rad - Angle_step_rad));
			Cur_servo_pos += Servo_step;
			//printf("Did math\n");
			if(Cur_servo_pos > 24000){
				printf("It broke :(\n At %lf\n",i);
			}
			Set_Position(SERVO1, Cur_servo_pos, Data_out);

			usleep(3000*1000);
			Get_Orientation(Current_Euler_data);
			
			printf("%lf,%lf,%lf,%lf\n",Angle_step, (float)(Current_Euler_data[0] - Prev_Euler_data[0])/(float)16, (float)(Current_Euler_data[1] - Prev_Euler_data[1])/(float)16, (float)(Current_Euler_data[2] - Prev_Euler_data[2])/(float)16);
			
			Prev_Euler_data[0] = Current_Euler_data[0];
			Prev_Euler_data[1] = Current_Euler_data[1];
			Prev_Euler_data[2] = Current_Euler_data[2];
			
		}
		
			Change_Mode(SERVO1, WHEEL, Data_out);	
			
			Get_Zero_Pos(Contact_Addr, Contact_DDR);
			
			//printf("Finished Zeroing servo\n");
			
			usleep(1000*300);
			
			Change_Mode(SERVO1, MULTI, Data_out);
			
			usleep(1000*300);
			
			Cur_servo_pos = 1400;
			Cur_angle = 0;	
			
	}
}
Example #4
0
void CBuilding::OnBox2dPositionChanged(const glm::vec3& _vPosition)
{
    Set_Position(glm::vec3(_vPosition.x, m_vPosition.y, _vPosition.z));
}