⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cball.cpp

📁 一个3D的保龄球的源代码
💻 CPP
字号:
/**
* <p>Title: class CBall</p>
* <p>Description: Includes properties of bowling ball and controls the ball’s movement.</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: Gameloft ShangHai</p>
* @author Tao Qing, Wei XIA
* @version 1.0
*/

#include "CBall.h"
#include "CScene.h"

/*
* Constructor of Ball Class
*/
CBall::CBall():CObject(OBJ_BALL)
{
	m_radius = DEFAULT_BALL_RADIUS;
	m_weight = DEFAULT_BALL_WEIGHT + BALL_TYPE_ONE;
	m_rotationSpeed = 0;	
	m_Texture.m_pData = NULL;
	initBall();
	
	
}


CBall::CBall(int ballType):CObject(OBJ_PIN) {
	m_radius = DEFAULT_BALL_RADIUS;
	m_weight = DEFAULT_BALL_WEIGHT + ballType;
	m_rotationSpeed = 0;
	m_Texture.m_pData = NULL;
	initBall();
}

CBall::~CBall() {
	
	
}




void CBall::computeRotationSpeed(int rotationPowerLevel) {
	
	m_rotationSpeed = rotationPowerLevel;
}

// ************************************************************
/**
* Get the ball status info. <p>
* BallSpinDirection depends on No.14th, 15th bits of ballStatus<p>
* BallType depends on No.12th,13th bits of ballStatusInfo<p>
* BallStartAngle depends on No. 4-11th bits<p>
* And ballRollingDegree depends on No. 0-3th bits.
* If ballSpinDirection == 0 means clockwise, otherwise anti-clockwise.
*
* @param ballStatusInfo Status word of ball
*/
void CBall::setBallType(int rotationPower) {
	
	int ballStatusInfo = rotationPower;
	int selectpower=m_powerBar/10;
	const int POWER_OFFSET[]={4,4,3,2,1,1,1,1,1,0,0};
	int direction;
	
	m_ballRotationDirection=(rotationPower>0)?1:0;// get the ball spin direction
	m_ballRollingDegree=(rotationPower>0)?rotationPower:-rotationPower;	// get the ball rolling degree

	if(rotationPower!=0){// get the ball type value
		m_ballCurveType=CURVE_BALL;
	}
	else{
		m_ballCurveType=LINEAR_BALL;
	}

	m_ballLAccelerationX=0;
	m_ballUAccelerationX=0;
	
	switch (m_ballCurveType) {
		
	case CURVE_BALL: {
		int b=m_ballRotationDirection==0?1:-1;
		
		m_ballLAccelerationX = b*(m_ballRollingDegree-1)/2;
		m_ballUAccelerationX = b*(m_ballRollingDegree-1)/2;		
					 }
		
		
	case LINEAR_BALL: 
	
		if((m_vPos.x>-1&&m_vPos.x<1)){// ball in the mid
			direction=dirRand%2==0?1:-1;
		}else{// not in the middle
			direction=m_vPos.x>0?1:-1;
		}
		m_ballLAccelerationX += direction*POWER_OFFSET[selectpower];//m_ballLAccelerationX = LFRICTION_MODULUS*(dirRand%2==0?1:-1)*(4-power);		
		m_ballUAccelerationX += direction*POWER_OFFSET[selectpower];//m_ballUAccelerationX = UFRICTION_MODULUS*(dirRand%2==0?1:-1)*(4-power);	
				   
		break;	
					 
	default:
		break;
	}	
	 m_ballVelocityX += m_ballUAccelerationX * 2; //Milo 12-06  //  /3
}


bool CBall::isIntoBallOutSide() {
	if (m_ballState == BALL_ONLANE &&
		//(m_currentBallPosition[0] < 0 || m_currentBallPosition[0] > LANE_WIDTH) &&
		(m_vPos.x < - (LANE_WIDTH / 2) || m_vPos.x > LANE_WIDTH / 2) &&
		!isIntoBallInvalid()) {
		return true;
	}
	return false;        
}

void CBall::intoBallOutSide() {
	m_ballState = BALL_OUTSIDE;
	m_ballVelocityX = 0;
	m_vPos.z = 4;//????????
	m_bInGutter = TRUE;		
	
	
	if (m_vPos.x < - (LANE_WIDTH /2) ) 
	{
		m_vPos.x = - (DEFAULT_BALL_RADIUS + ((LANE_WIDTH) /2))-2; //what's the 13? Milo 09-23
	}
	else 
	{
		m_vPos.x = (LANE_WIDTH) /2 + DEFAULT_BALL_RADIUS+2;//what's the 13? Milo 09-23
	}
}

bool CBall::isIntoBallInvalid() {
	if (m_ballState != BALL_INVALID &&
		(m_vPos.y < -500 ||
		m_vPos.y > LANE_LENGTH + LANE_WIDTH ||
		(m_ballVelocityX == 0 && m_ballVelocityY == 0))) {
		return true;
	}
	return false;
}


void CBall::initBall(){
	
	power = 1;

	m_ballRollingArray[0] = 0;
	m_ballRollingArray[1] = 0;
	m_ballRollingArray[2] = 0;
	
	m_ballVelocityX = 0;
	m_ballVelocityY = 0;
	m_ballGutterVelocity = 0;
	
	m_ballLAccelerationX = 0;
	m_ballLAccelerationY = 0;
	m_ballUAccelerationX = 0;
	m_ballUAccelerationY = 0;
	
	m_ballState = BALL_INVALID;
	m_ballCurveType = LINEAR_BALL;
	m_ballRotationDirection = 0;
	m_ballRollingDegree = 0;
	
	m_vPos.x = 0;//- LANE_WIDTH / 2;
	m_vPos.z = DEFAULT_BALL_RADIUS;///

		
	m_vRot.x = 0;
	m_vRot.y = 0;
	m_vRot.z = 512;


	m_bInGutter = FALSE;
	m_bIsThrown = FALSE;
	
}




void CBall::SetRenderingProperty(int frame)
{
	if ( m_vPos.y < (HEAVY_OIL_AREA)) {
		
		m_ballRollingArray[1] += 0; //100; //(100 +m_ballRollingDegree * sin(450) / VALUE_MULTIPLE_TEN);
		m_ballRollingArray[1] %= 2048;
		
		m_ballRollingArray[2] += (2) * (m_ballVelocityX/BALL_VELOCITY_EXTENSION  + (m_ballRollingDegree * frame));
		m_ballRollingArray[2] %= 2048;
		
		m_vRot.x = 0; //20;//900 * m_ballRollingDegree * sin(450) / VALUE_MULTIPLE_TEN;				
	}
	else 
	{
		m_ballRollingArray[0] += 5 *(m_ballVelocityY); //50; //* sin(450) / VALUE_MULTIPLE_TEN;//m_ballRollingDegree * sin(450) / VALUE_MULTIPLE_TEN;;
		m_ballRollingArray[0] %= 2048;
		
        m_ballRollingArray[1] += 0; //100; //(100 +m_ballRollingDegree * sin(450) / VALUE_MULTIPLE_TEN);
		m_ballRollingArray[1] %= 2048;
		m_ballRollingArray[2] += (2) * (m_ballVelocityX/BALL_VELOCITY_EXTENSION  + (m_ballRollingDegree * frame));
		m_ballRollingArray[2] %= 2048;
		
		m_vRot.x = -m_ballRollingArray[0]; //20;//900 * m_ballRollingDegree * sin(450) / VALUE_MULTIPLE_TEN;
		
	}
	m_vRot.y = m_ballRollingArray[1]; //60;//20 * m_ballRollingDegree * sin(450) / VALUE_MULTIPLE_TEN;///rotation[0];
	
	//considering direction
	m_vRot.z = m_ballRollingArray[2]; //900;// * m_ballRollingDegree;
	m_vRot.z *= m_ballRotationDirection == 0 ? -1 : 1;

	if (m_ballState != BALL_ONLANE)
	{
		m_vRot.z  = 0;
	}
}

void CBall::SetBallVelocity(Vector4s desPos, Vector4s StaPos, int time)
{
	m_ballVelocityX = ((desPos.x - StaPos.x)*BALL_VELOCITY_EXTENSION) / (time);
	m_ballVelocityY = ((desPos.y - StaPos.y)) / (time); 
	
	m_ballGutterVelocity = m_ballVelocityY * GUTTER_REDUCTION /100;	
	bFirstRun=1;
	
	//add by sunfishfine
	//to correct the rotation angle. the initial rot.z will be set to 0 in the SetRenderingProperty(). 
	//in order to fix the bug, clear the rot.z before ball on track
	m_vRot.z=0;


}
  
void CBall::GoToNext()
{
	if(bFirstRun&&BALL_OUTSIDE!=m_ballCurveType){
		bFirstRun=0;
		m_PositionX=m_vPos.x*100;
	}
	
	m_vPos.y+=(m_ballVelocityY);
	if(BALL_OUTSIDE!=m_ballState){
		m_ballVelocityX += m_ballLAccelerationX;		
	}
	if(m_vPos.y>=HEAVY_OIL_AREA){
		if(m_ballCurveType==CURVE_BALL)
			if (m_vPos.y > LIGHT_OIL_AREA) {
				m_ballVelocityX += m_ballUAccelerationX;
			}
			else if(BALL_OUTSIDE==m_ballState){
				m_vPos.y += (m_ballGutterVelocity-m_ballVelocityY);	
			}
	}
	
	if(BALL_OUTSIDE!=m_ballState){
	m_PositionX =(m_PositionX+ m_ballVelocityX);
	m_vPos.x=m_PositionX/BALL_VELOCITY_EXTENSION;
	}
	
}

void CBall::IsValid()
{
	if (isIntoBallOutSide()) 
	{
		intoBallOutSide();
		
	}
	if (isIntoBallInvalid()) 
	{
		m_ballState = BALL_INVALID;
	}
}

void CBall::StepWithPlayer(CPlayer *player, Vector4s *hand, bool withHand)
{
	CMatrix44 mat;
	player->UpdateWorldMat();
	mat = player->GetWorldMatrix();
	Vector4s vec;
	if (withHand == TRUE)
	{
		vec.x = (hand->x - (SCREEN_WIDTH/2 - 29))*2/5;//hand.x ;//DEFAULT_BALL_RADIUS / DEFAULT_EXTEND;;//
		vec.y = HAND_POSITION_Y ;//+ SCREEN_HEIGHT;//* DEFAULT_EXTEND;
		vec.z = HAND_POSITION_Z  - (hand->y);//- DEFAULT_BALL_RADIUS / DEFAULT_EXTEND;//* DEFAULT_EXTEND + DEFAULT_BALL_RADIUS;///DEFAULT_EXTEND;
									   /*vec.x = player->m_ballTracePos[player->GetCurFrame()][0] + 30;//DEFAULT_BALL_RADIUS / DEFAULT_EXTEND;;//
									   vec.y = player->m_ballTracePos[player->GetCurFrame()][1] + 55;//* DEFAULT_EXTEND;
									   vec.z = player->m_ballTracePos[player->GetCurFrame()][2] -10;//- DEFAULT_BALL_RADIUS / DEFAULT_EXTEND;//* DEFAULT_EXTEND + DEFAULT_BALL_RADIUS;///DEFAULT_EXTEND;
										*/
	}
	else
	{
		vec.x = player->m_ballTracePos[player->GetCurFrame()][0] ;//- DEFAULT_BALL_RADIUS / DEFAULT_EXTEND;
		vec.y = player->m_ballTracePos[player->GetCurFrame()][1] ;//* DEFAULT_EXTEND;
		vec.z = player->m_ballTracePos[player->GetCurFrame()][2] ;//- DEFAULT_BALL_RADIUS / DEFAULT_EXTEND;//* DEFAULT_EXTEND + DEFAULT_BALL_RADIUS;///DEFAULT_EXTEND;
		
		if(vec.z < DEFAULT_BALL_RADIUS|| m_bIsThrown == TRUE)
		{
			if(vec.z > DEFAULT_BALL_RADIUS )
			{
				m_bIsThrown =  FALSE ;
			}
			else		
			{
				m_bIsThrown = TRUE;
				vec.z = DEFAULT_BALL_RADIUS ;
				//vec.x -= PLAYER_POSITION_X; // Milo 03-03
			}
		}
		vec = mat.Transform(vec);
	}
	SetPosition(vec) ;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -