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

📄 old_motion.cpp

📁 c++程序
💻 CPP
字号:
// Motion.cpp: Implementierung der Klasse CMotion.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Motion.h"

// Conversion factor for converting between degrees and radians
#define PI_OVER_180 0.0174532925f

//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////

CMotion::CMotion()
{
	// Init motion behavior with standard values. Every values is meant
	// to be applied in 1000ms

	m_SlowDown   = 0.5f;
	m_Gravity[x] = 0.0f;
	m_Gravity[y] = -0.5f;
	m_Gravity[z] = 0.0f;

	// Init other member variables

	m_SmallestForce = 0.1f;
}

CMotion::~CMotion()
{

}

void CMotion::ApplyMotionPhysics(DWORD dwTimeElapsed)
{
	///////////////////////////////////////////////////////////////
	// Calculate the movement that would happen in dwTimeElapsed ms
	
	unsigned int iCurForce;
	CForce cFinalForce;

	//////////////////////////////////////////////////
	// Add all forces to get the final movement vector
	
	for (iCurForce=0; iCurForce<=MAX_FORCE-1; iCurForce++)
	{
		// Is a force present at the current array position ?
		if (fabs(m_ForceArray[iCurForce][x]) + fabs(m_ForceArray[iCurForce][y]) + 
			fabs(m_ForceArray[iCurForce][z]) > m_SmallestForce)
		// Add the force to the final movement vector
		cFinalForce += m_ForceArray[iCurForce];
	}

	/////////////////////////////////////////////
	// Apply gravity to the final movement vector
	
	cFinalForce += m_Gravity;

	//////////////////////////////////////////////////////////
	// Scale the final motion vector relative to dwTimeElapsed
	
	cFinalForce[x] = (cFinalForce[x] / 1000) * dwTimeElapsed;
	cFinalForce[y] = (cFinalForce[y] / 1000) * dwTimeElapsed;
	cFinalForce[z] = (cFinalForce[z] / 1000) * dwTimeElapsed;

	//////////////////////////////////////////////////////////
	// Pass the position after the motion to the CCamera class

	SetXPos(GetXPos() + cFinalForce[x]);
	SetYPos(GetYPos() + cFinalForce[y]);
	SetZPos(GetZPos() + cFinalForce[z]);

	/////////////////////////////////////////////////////////////////
	// Lift the player up to the terrain if he has descended below it

	float fHeight = GetSurfaceHeight();
	if (GetYPos() < fHeight)
		SetYPos(fHeight);
	
	////////////////////////////////
	// Slow down all existing forces
	
	for (iCurForce=0; iCurForce<=MAX_FORCE-1; iCurForce++)
	{
		// Is a force present at the current array position ?
		if (fabs(m_ForceArray[iCurForce][x]) + fabs(m_ForceArray[iCurForce][y]) + 
			fabs(m_ForceArray[iCurForce][z]) > m_SmallestForce)
		{
			// Reduce force as specified in m_SlowDown. Scale to the elapsed time
			m_ForceArray[iCurForce].SlowDown((m_SlowDown / 1000) * dwTimeElapsed);
		}
		else
			// Force is too weak, clear it
			m_ForceArray[iCurForce].ResetForce();
	}
}

bool CMotion::AddMotionVector(CForce cNewMotion)
{
	// Add a new motion vector to the array

	for (unsigned int iCurForce=0; iCurForce<=MAX_FORCE-1; iCurForce++)
		// Is current array position empty ?
		if (fabs(m_ForceArray[iCurForce][x]) + fabs(m_ForceArray[iCurForce][y]) + 
			fabs(m_ForceArray[iCurForce][z]) <= m_SmallestForce)
		{
			// Add the force at the array position and return
			m_ForceArray[iCurForce] = cNewMotion;
			return TRUE;
		}
	
	// No empty array position was found
	return FALSE;
}

void CMotion::SetCustomMotion(float fForwardSpeed, float fBackwardSpeed, float fLeftSpeed, float fRightSpeed,
							  float fUpSpeed, float fDownSpeed)
{
	if (fForwardSpeed)
	{
		CForce cForward;
		cForward[x] = -(sinf(GetYRotation() * PI_OVER_180) * fForwardSpeed);
		cForward[y] = 0.0f;
		cForward[z] = -(cosf(GetYRotation() * PI_OVER_180) * fForwardSpeed);
		AddMotionVector(cForward);
	}

	if (fBackwardSpeed)
	{
		CForce cBackward;
		cBackward[x] = sinf(GetYRotation() * PI_OVER_180) * fBackwardSpeed;
		cBackward[y] = 0.0f;
		cBackward[z] = cosf(GetYRotation() * PI_OVER_180) * fBackwardSpeed;
		AddMotionVector(cBackward);
	}

	if (fLeftSpeed)
	{
		CForce cLeft;
		cLeft[x] = sinf((GetYRotation() - 90.0f) * PI_OVER_180) * fLeftSpeed;
		cLeft[y] = 0.0f;
		cLeft[z] = cosf((GetYRotation() - 90.0f) * PI_OVER_180) * fLeftSpeed;
		AddMotionVector(cLeft);
	}

	if (fRightSpeed)
	{
		CForce cRight;
		cRight[x] = sinf((GetYRotation() + 90.0f) * PI_OVER_180) * fRightSpeed;
		cRight[y] = 0.0f;
		cRight[z] = cosf((GetYRotation() + 90.0f) * PI_OVER_180) * fRightSpeed;
		AddMotionVector(cRight);
	}
}

void CMotion::SetDefaultMotion(bool bForward, bool bBackward, bool bLeft, bool bRight, bool bUp, bool bDown)
{
	// Add the enabled motion vectors with default accelerations to the array
	
	// Convert parameters for SetCustomMotion() and pass them
	SetCustomMotion(bForward * GetSpeed(), bBackward * GetSpeed(), 
		bLeft * GetSpeed(), bRight * GetSpeed(),
		bUp * GetSpeed(),  bDown * GetSpeed());
}

void CMotion::Jump()
{
	
}

⌨️ 快捷键说明

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