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

📄 very_old_motion.cpp

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

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

// Motion behavior
#define MIN_ACCELERATION     14.0f	// Higher value means lower minimum acceleration
#define SLOW_DOWN            1.3f	// Lower value means less slowdown
#define MID_AIR_SLOW_DOWN    1.0f	// Lower value means less slowdown
#define AIR_CONTROL          3.0f	// Higher value means less air control (mid-air acceleration)
#define GRAVITY              1.7f	// Higher value means lower gravity
#define GRAVITY_ACCELERATION 7.0f	// Higher value means less gravity acceleration
#define JUMP_SPEED           0.03f  // Higher value means more jump speed

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

CMotion::CMotion()
{
	// Set initial movement speeds
	m_MoveLeftSpeed			= 0.0f;
	m_MoveRightSpeed		= 0.0f;
	m_MoveForwardSpeed		= 0.0f;
	m_MoveBackwardSpeed		= 0.0f;
	m_MoveUpSpeed			= 0.0f;
	m_MoveDownSpeed			= 0.0f;
	m_GravityAcceleration   = 0.0f;
	m_MidAir				= FALSE;
}

CMotion::~CMotion()
{

}

void CMotion::ApplyMotionPhysics()
{
	// Apply the movement vectors

	// Is the camera in the air ?
	CheckMidAir();
	
	// Move with the speed of the current motion vectors
	// (if at least one of them has any acceleration)
	if (m_MoveForwardSpeed || m_MoveBackwardSpeed || 
		m_MoveLeftSpeed    || m_MoveRightSpeed    ||
		m_MoveUpSpeed      || m_MoveDownSpeed)
	{
		// Perform movement
		MoveCustomSpeed(m_MoveForwardSpeed, m_MoveBackwardSpeed,
			m_MoveLeftSpeed, m_MoveRightSpeed, m_MoveUpSpeed, m_MoveDownSpeed);
	}

	// Get height of the landscape at the camera's position. If no landscape
	// has been associated, get the current camera height.
	float fHeight = GetSurfaceHeight();
	
	// Process the Y-axis
	if (fHeight >= GetYPos())
	{
		// Lift camera up to the terrain
		SetYPos(fHeight);
		// No gravity acceleration / no up movement
		m_GravityAcceleration = 0.0f;
		m_MoveUpSpeed = 0.0f;
	}
	else
	{
		// Apply gravity to let the camera descent down
		if (fabs(GetYPos() - fHeight) < (GetSpeed() / GRAVITY) + m_GravityAcceleration)
		{
			// Near to the ground; Set current height to ground
			SetYPos(fHeight);
			// No gravity acceleration / no up movement
			m_GravityAcceleration = 0.0f;
			m_MoveUpSpeed = 0.0f;
		}
		else
		{
			// Descent at default gravitation level
			SetYPos(GetYPos() - ((GetSpeed() / GRAVITY) + m_GravityAcceleration));
			// More gravity acceleration
			m_GravityAcceleration += (GetSpeed() / GRAVITY) / GRAVITY_ACCELERATION;
		}
	}

	// Reduce acceleration

	// Reduce the acceleration at another rate if camera is in the air
	float fSlowDown = SLOW_DOWN;
	if (m_MidAir == TRUE)
		fSlowDown = MID_AIR_SLOW_DOWN;
		
	// Forward
	if (m_MoveForwardSpeed > GetSpeed() / MIN_ACCELERATION)
		m_MoveForwardSpeed /= fSlowDown;
	else
		m_MoveForwardSpeed = 0.0f;
	// Backward
	if (m_MoveBackwardSpeed > GetSpeed() / MIN_ACCELERATION)
		m_MoveBackwardSpeed /= fSlowDown;
	else
		m_MoveBackwardSpeed = 0.0f;
	// Left
	if (m_MoveLeftSpeed > GetSpeed() / MIN_ACCELERATION)
		m_MoveLeftSpeed /= fSlowDown;
	else
		m_MoveLeftSpeed = 0.0f;
	// Right
	if (m_MoveRightSpeed > GetSpeed() / MIN_ACCELERATION)
		m_MoveRightSpeed /= fSlowDown;
	else
		m_MoveRightSpeed = 0.0f;
	// Up
	if (m_MoveUpSpeed > GetSpeed() / MIN_ACCELERATION)
		m_MoveUpSpeed /= fSlowDown;
	else
		m_MoveUpSpeed = 0.0f;
}

void CMotion::SetCustomMotion(float fForwardSpeed, float fBackwardSpeed, float fLeftSpeed, float fRightSpeed,
							  float fUpSpeed, float fDownSpeed)
{
	// Set the movement vectors to custom accelerations

	// Is the camera in the air ?
	CheckMidAir();

	// Less acceleration allowed while camera is in the air
	float fAirControl = 1.0f;
	if (m_MidAir == TRUE)
		fAirControl = AIR_CONTROL;

	// Make sure that the new motion isn't slower than the old

	// Forward
	if (fForwardSpeed / fAirControl > m_MoveForwardSpeed)
		m_MoveForwardSpeed = fForwardSpeed / fAirControl;
	// Backward
	if (fBackwardSpeed / fAirControl > m_MoveBackwardSpeed)
		m_MoveBackwardSpeed = fBackwardSpeed / fAirControl;
	// Left
	if (fLeftSpeed / fAirControl > m_MoveLeftSpeed)
		m_MoveLeftSpeed = fLeftSpeed / fAirControl;
	// Right
	if (fRightSpeed / fAirControl > m_MoveRightSpeed)
		m_MoveRightSpeed = fRightSpeed / fAirControl;
	// Up
	if (fUpSpeed / fAirControl > m_MoveUpSpeed)
		m_MoveUpSpeed = fUpSpeed / fAirControl;
	// Down
	if (fDownSpeed / fAirControl > m_MoveDownSpeed)
		m_MoveDownSpeed = fDownSpeed / fAirControl;
}

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

void CMotion::Jump()
{
	// Let the camera jump
	
	// Is the camera in the air ?
	CheckMidAir();

	// Can't jump while in the air
	if (m_MidAir == FALSE)
		// Make sure that the new motion isn't slower than the old
		if (m_MoveUpSpeed < JUMP_SPEED)
			m_MoveUpSpeed = JUMP_SPEED;
}

void CMotion::CheckMidAir()
{
	// Checks if the camera is in the air
	if (fabs(GetYPos() - GetSurfaceHeight()) < GetSpeed() / GRAVITY)
		m_MidAir = FALSE;
	else
		m_MidAir = TRUE;
}

⌨️ 快捷键说明

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