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

📄 sphere.h

📁 小游戏 有碰撞检测和音乐 使用键盘openal
💻 H
字号:
#pragma once
#include "object.h"
#include "plane.h"
#include "vector.h"
#include "camera.h"
#include "define.h"
#include <map>
#include <math.h>
const float RAD_TO_DEG = 57.2957795130823208767981548141052f;
CVector pos, view, up;
extern CCamera *camera ;
extern int score;
int fsm_state = 0;
class CSphere : public CObject
{
public:
	float r,g,b;
	float radius;
	bool wireFrame;
	float angle;
	float speed;
	CVector pos;
	CSphere()
	{}


	CSphere( float x, float y, float z, float r, float g, float b , float radius, bool wireFrame, float speed)
: r( r ), g( g ), b( b ), radius( radius ),wireFrame ( wireFrame ), angle(0.0f), speed ( speed )
	{
		pos.x = x; pos.y =y; pos.z =z;
		type = "sphere";
	}


	void Render ()
	{
		glColor3f( r, g, b );
		glPushMatrix();
		glTranslatef( pos.x, pos.y, pos.z );
		glRotatef( angle*RAD_TO_DEG, 0.0, 1.0, 0.0 );
		GLUquadricObj *object = gluNewQuadric();
		if ( wireFrame )
		gluQuadricDrawStyle( object, GLU_LINE );
		gluSphere( object, radius, 25, 25 );
		gluDeleteQuadric( object );
		glPopMatrix();
	}
	bool CollideWith( CObject *_other )
	{
		if (_other->type=="sphere")
		{
			CSphere *other = (CSphere*) _other;
			float dbc = (other->pos-pos).Magnitude();
			float dbcs = dbc*dbc;
			float sors = (radius + other->radius)*(radius + other->radius);
			float distance = dbcs-sors;
			if (dbcs > sors)
			{
				if(distance > 0&&distance <=0.5)
				{
					
				//	fsm_state = 1; 
				
			//		std::cout<<"chase"<<std::endl;
				}
				else if(distance > 0.5)
				{
				//	fsm_state = 0; 
		//			std::cout<<"idle"<<std::endl;
				}
		//		std::cout<<"distance"<<distance<<std::endl;
				return false;	
			}
			else
			{
				score++;
			//	fsm_state = 2; 
				return true;
			}
			
		}
		if (_other->type=="plane")
		{
			CPlane *other = (CPlane*) _other;
			CVector normal = other->normal;
			// check if direction is parallel to plane
			CVector direction = camera->view - camera->pos;
			float dot_product = direction * normal;
			if ( (dot_product > -0.01) && (dot_product < 0.01)) 
				return false;
			// calculate distance plane from origin
			float dpo = - ((normal.x * other->v[0].x) + (normal.y * other->v[0].y) + (normal.z * other->v[0].z));

			// calculate distance of center of sphere from plane
			float dpsc = (normal.x * pos.x + normal.y * pos.y + normal.z * pos.z + dpo);
			// if no intersection with plane
			if( dpsc > radius ) return false;
			// get vector for closest distance of sphere center to plane
			CVector offset = normal * dpsc;
			// get vector for closest point or hit
			CVector hit_point = pos - offset;
			float angle = 0.0;
			CVector vA, vB; // temp vectors
		// loop through & sum interior angles (plane vertices with hit point)
			for (int i = 0; i < 4; i++)
			{
				vA = other->v[i] - hit_point;// get pair of vectors at a time from hit point to vertices
				vB = other->v[(i + 1) % 4] - hit_point;
				float temp_angle = acos( vA*vB / (vA.Magnitude()*vB.Magnitude()) );
				if(_isnan(temp_angle))
				temp_angle = 0.0;
				angle += temp_angle;
			}
			if( angle >= ( 0.99 * (2.0 * 3.14159)) ) // point in plane
				return true;
			else 
			{
				for (int i = 0; i < 4; i++) 
				{
					// create vector from end point to our center
					CVector vVector1 = pos - other->v[i];
					// normalize direction vector from end point to next point
					CVector vVector2 = (other->v[(i + 1) % 4] - other->v[i]).Normalize();
					// find distance of line segment
					float d = (other->v[(i + 1) % 4] - other->v[i]).Magnitude();
					// project vector 1 to 2
					float t = vVector2* vVector1;
					// first point is nearest
					if (t <= 0)
					hit_point = other->v[i];
					// second point is nearest
					if (t >= d)
					hit_point = other->v[(i + 1) % 4];
					// get closest point on current edge to center of sphere.
					hit_point = other->v[i] + vVector2 * t;
					// calculate distance between closest point and sphere center
					float dcpsc = (pos - hit_point).Magnitude();
					// if distance is less than radius, collision is true
					if(dcpsc < radius)
					return true;
				}
			}
			return false;
		}
	}
};

⌨️ 快捷键说明

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