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

📄 simplescenes_crash.h

📁 使用stl技术,(还没看,是听说的)
💻 H
📖 第 1 页 / 共 2 页
字号:
/*
SimpleScenes_Crash.h
----------------------
A reimplementation of the ODE "crashing into a wall" demo 
using Ogre and the OgreOde wrapper.
*/
#ifndef _SIMPLESCENES_CRASH_H_
#define _SIMPLESCENES_CRASH_H_

/*
The crash test extends the base test class
*/
class SimpleScenes_Crash:public SimpleScenes
{
public:
	// Standard constructor/destructor
	SimpleScenes_Crash()
	{
		// Main bit of the helicopter
		_apache_body = _mgr->createEntity("Apache Body", "apache_body.mesh");
		_apache_body->setCastShadows(true);

		_apache_body_node = _mgr->getRootSceneNode()->createChildSceneNode("Apache Body");
		_apache_body_node->attachObject(_apache_body);
		_apache_body_node->setPosition(0,1.2,0);

		// Main rotor
		_main_rotor = _mgr->createEntity("Main Rotor", "main_rotor.mesh");
		_main_rotor->setCastShadows(true);

		_main_rotor_node = _apache_body_node->createChildSceneNode("Main Rotor");
		_main_rotor_node->attachObject(_main_rotor);
		_main_rotor_node->setPosition(0,0.987322,0.573885);

		// Tail rotor
		_tail_rotor = _mgr->createEntity("Tail Rotor", "tail_rotor.mesh");
		_tail_rotor->setCastShadows(true);

		_tail_rotor_node = _apache_body_node->createChildSceneNode("Tail Rotor");
		_tail_rotor_node->attachObject(_tail_rotor);
		_tail_rotor_node->setPosition(0.174927,0.173132,-3.50708);

		// Set up the parameters for the main rotor
		_main_min = 2.0;
		_main_max = 90.0;
		_main_spd = _main_min;
		_main_response = 40.0;
		_main_scale = 1.0;

		// Set up the parameters for the tail rotor
		_tail_min = -80.0;
		_tail_max = 80.0;
		_tail_response = 90.0;
		_tail_scale = 0.5;
		_tail_spd = 0.0;

		// Other simulation parameters
		_rotate_scale = 20.0;
		_thrust = 50.0;
		_linear_damp = 12.5;
		_angular_damp = 25.0;
		_thrust_offs = Vector3(0,0.1,0);
		_fire_rate = 0.5;
		_fire_time = _fire_rate;
		_fire_force = 250.0;
		_fire_mass = 0.1;
		_fire_size = 0.25;
		_rockets = 0;

		_apply_damping = true;

		// Create a space for the helicopter to live in
		_apache_space = new OgreOde::SimpleSpace(_space);
		_apache_space->setInternalCollisions(false);
		_apache_space->setAutoCleanup(false);	

		// Create the physical body of the helicopter
		OgreOde::BoxMass apache_mass(1.5,Vector3(1.4,1.46,4.24));
		_apache = new OgreOde::Body();
		_apache_body_node->attachObject(_apache);
		_apache->setMass(apache_mass);

		_bodies.push_back(_apache);
		_last_node = _apache_body_node;

		// Create the geometry of the helicopter from several bits

		// Main body
		OgreOde::TransformGeometry* trans = new OgreOde::TransformGeometry(_apache_space);
		OgreOde::BoxGeometry* geom = new OgreOde::BoxGeometry(Vector3(1.4,1.46,4.24));		
		trans->setBody(_apache);
		trans->setEncapsulatedGeometry(geom);
		geom->setPosition(Vector3(0.013,0.014,1.052));
		_geoms.push_back(geom);
		_geoms.push_back(trans);

		// Left wing
		trans = new OgreOde::TransformGeometry(_apache_space);
		geom = new OgreOde::BoxGeometry(Vector3(0.75,0.55,1.05));		
		trans->setBody(_apache);
		trans->setEncapsulatedGeometry(geom);
		geom->setPosition(Vector3(-1.089,-0.486,0.446));		
		_geoms.push_back(geom);
		_geoms.push_back(trans);

		// Right wing
		trans = new OgreOde::TransformGeometry(_apache_space);
		geom = new OgreOde::BoxGeometry(Vector3(0.75,0.55,1.05));		
		trans->setBody(_apache);
		trans->setEncapsulatedGeometry(geom);
		geom->setPosition(Vector3(1.089,-0.505,0.446));		
		_geoms.push_back(geom);
		_geoms.push_back(trans);

		// Tail boom
		trans = new OgreOde::TransformGeometry(_apache_space);
		geom = new OgreOde::BoxGeometry(Vector3(0.73,0.66,3.28));		
		trans->setBody(_apache);
		trans->setEncapsulatedGeometry(geom);
		geom->setPosition(Vector3(0,-0.533,-2.104));		
		_geoms.push_back(geom);
		_geoms.push_back(trans);

		// Tail flipper (probably not the technically correct name!)
		trans = new OgreOde::TransformGeometry(_apache_space);
		geom = new OgreOde::BoxGeometry(Vector3(1.61,0.30,0.45));		
		trans->setBody(_apache);
		trans->setEncapsulatedGeometry(geom);
		geom->setPosition(Vector3(-0.014,-0.622,-3.778));		
		_geoms.push_back(geom);
		_geoms.push_back(trans);

		// Tail fin
		trans = new OgreOde::TransformGeometry(_apache_space);
		geom = new OgreOde::BoxGeometry(Vector3(0.30,1.27,0.96));		
		trans->setBody(_apache);
		trans->setEncapsulatedGeometry(geom);
		geom->setPosition(Vector3(0,-0.042,-3.491));	
		_geoms.push_back(geom);
		_geoms.push_back(trans);

		// Rear wheel - a separate body connected by a ball joint, doesn't have a SceneNode
		OgreOde::SphereMass wheel_mass(0.02,0.06);
		_rear_wheel = new OgreOde::Body();
		_rear_wheel->setMass(wheel_mass);
		_rear_wheel->setPosition(Vector3(0.0,0.068,-3.937));
		OgreOde::BallJoint* ball_joint = new OgreOde::BallJoint();
		ball_joint->attach(_apache,_rear_wheel);
		ball_joint->setAnchor(_rear_wheel->getPosition());
		OgreOde::SphereGeometry* sphere = new OgreOde::SphereGeometry(0.06,_apache_space);
		sphere->setBody(_rear_wheel);
		_geoms.push_back(sphere);
		_bodies.push_back(_rear_wheel);
		_joints.push_back(ball_joint);

		// Left front wheel - connected by a hinge joint so it can only roll forward
		OgreOde::SphereMass front_wheel_mass(0.03,0.17);
		_left_front = new OgreOde::Body();
		_left_front->setMass(front_wheel_mass);
		_left_front->setPosition(Vector3(-0.699,0.199,1.163));
		OgreOde::HingeJoint* hinge_joint = new OgreOde::HingeJoint();
		hinge_joint->attach(_apache,_left_front);
		hinge_joint->setAnchor(_left_front->getPosition());
		hinge_joint->setAxis(Vector3::UNIT_X);
		sphere = new OgreOde::SphereGeometry(0.17,_apache_space);
		sphere->setBody(_left_front);
		_geoms.push_back(sphere);
		_bodies.push_back(_left_front);
		_joints.push_back(hinge_joint);

		// Right front wheel - connected by a hinge joint, doesn't have a SceneNode
		_right_front = new OgreOde::Body();
		_right_front->setMass(front_wheel_mass);
		_right_front->setPosition(Vector3(0.699,0.185,1.163));
		hinge_joint = new OgreOde::HingeJoint();
		hinge_joint->attach(_apache,_right_front);
		hinge_joint->setAnchor(_right_front->getPosition());
		hinge_joint->setAxis(Vector3::UNIT_X);
		sphere = new OgreOde::SphereGeometry(0.17,_apache_space);
		sphere->setBody(_right_front);
		_geoms.push_back(sphere);
		_bodies.push_back(_right_front);
		_joints.push_back(hinge_joint);

		// Create the wall of boxes to crash into, make it 
		// smaller in Debug mode for performance reasons
		int i = 0;
#ifndef _DEBUG
		for(Real y = 0.0;y <= 5.0;y += 1.0)
		{
			for(Real x = -6.0 + (y * 0.5);x <= 6.0 - (y * 0.5);x += 1.0)
			{
#else
		for(Real y = 0.0;y <= 1.0;y += 1.0)
		{
			for(Real x = -2.0 + (y * 0.5);x <= 2.0 - (y * 0.5);x += 1.0)
			{
#endif
				// Create the Ogre box
				String name = String("Box_") + StringConverter::toString(i);
				Entity* box = _mgr->createEntity(name,"Crate.mesh");
				box->setCastShadows(true);

				SceneNode* node = _mgr->getRootSceneNode()->createChildSceneNode(name);
				node->attachObject(box);
				node->setScale(0.1,0.1,0.1);

				// Set the position and rotate that, then rotate the box by the same amount
				Quaternion q;
				q.FromAngleAxis(Degree(45),Vector3::UNIT_Y);
				Vector3 pos(x,y + 0.5,-10.0);
				node->setPosition(q * pos);
				node->setOrientation(q);

				// Create a box for ODE and attach it to the Ogre version
				OgreOde::Body* body = new OgreOde::Body();
				node->attachObject(body);
				body->setMass(OgreOde::BoxMass(0.01,Vector3(1,1,1)));

				OgreOde::BoxGeometry* geom = new OgreOde::BoxGeometry(Vector3(1.0,1.0,1.0),_space);
				geom->setBody(body);

				// Keep track of the ODE objects for house keeping
				_bodies.push_back(body);
				_geoms.push_back(geom);

				i++;
			}
		}
	}
	virtual ~SimpleScenes_Crash()
	{
		// Destroy the non-standard scene nodes that make up the helicopter
		_apache_body_node->removeAndDestroyChild("Main Rotor");
		_apache_body_node->removeAndDestroyChild("Tail Rotor");
		
		// Remove the entities that represent the scene nodes we destroyed
		_mgr->removeEntity("Main Rotor");
		_mgr->removeEntity("Tail Rotor");
        
		// Delete the space in which the helicopter lived
		delete _apache_space;
	}

	// Return our name for the test application to display
	virtual const String& getName()
	{
		static String name = "Test Crash";
		return name;
	}

	// Return a description of the keys that the user can use in this test
	virtual const String& getKeys()
	{
		static String keys = "I/K - Throttle, J/L - Turn, G/B/V/N - Thrust, X - Fire";
		return keys;
	}

	// Handle the user's key presses
	virtual void frameEnded(Real time,InputReader* input)
	{
		// Do default key handling
		SimpleScenes::frameEnded(time,input);

		// Throttle up or down
		if(input->isKeyDown(KC_I)) _main_spd += (_main_response * time);

⌨️ 快捷键说明

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