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

📄 simplescenes_buggy.h

📁 使用stl技术,(还没看,是听说的)
💻 H
字号:
/*
SimpleScenes_Buggy.h
-------------------
A reimplementation of the ODE test Buggy application using
the OgreOde wrapper for Ogre
*/
#ifndef _SIMPLESCENES_BUGGY_H_
#define _SIMPLESCENES_BUGGY_H_

/*
Buggy test extends from the base test class
*/
class SimpleScenes_Buggy:public SimpleScenes
{
public:
	// Constructor also needs to create the scene
	SimpleScenes_Buggy()
	{
		// Create the vehicle using the supplied name and body mesh, make it 1.5 units heavy 
		// e.g. tonnes and offset the center of gravity by half (a metre) to make it more stable
		// This puts the CoG somewhere around the wheel hubs, which is probably reasonably
		// realistic for a sports-car.
		vehicle = new OgreOde::Vehicle("Subaru","Scooby_Body.mesh",1.5,Vector3(0,-0.5,0));

		// Move the vehicle
		vehicle->setPosition(Vector3(0,0.82898,0));

		// Create all the wheels, using the supplied mesh and with the specified offset
		// realtive to the car. The mass is 0.02 units, in our case that's around 20Kg
		vehicle->addWheel("Scooby_WheelL.mesh",Vector3(0.8045,-0.46698,1.4),0.02);
		vehicle->addWheel("Scooby_WheelR.mesh",Vector3(-0.8045,-0.46698,1.4),0.02);
		vehicle->addWheel("Scooby_WheelL.mesh",Vector3(0.8045,-0.46698,-1.4),0.02);
		vehicle->addWheel("Scooby_WheelR.mesh",Vector3(-0.8045,-0.46698,-1.4),0.02);

		// Set up the suspension spring and damping constants, passing the rate we're going to
		// be stepping the world so it can work out the forces needed each step
		// We could do this per-wheel, like the other factors, but it's easier to do it this way.
		// N.B. You must create the wheels before you can do this!
		vehicle->setSuspension(90,0.95,SimpleScenes::STEP_RATE);

		// Set the steer, power and brake factor for each wheel, initially we're rear wheel drive
		vehicle->getWheel(0)->setFactors(1,0,0.75);
		vehicle->getWheel(1)->setFactors(1,0,0.75);
		vehicle->getWheel(2)->setFactors(0,1,0.25);
		vehicle->getWheel(3)->setFactors(0,1,0.25);

		_drive = 'R';
		setInfoText("Rear wheel drive");

		// Set the engine (and other drivetrain) parameters, lots of work still to do here!
		vehicle->getEngine()->setRevLimit(40.0);
		vehicle->getEngine()->setTorque(0.25,5.0);
		vehicle->getEngine()->setBrakeForce(10.0);

		// Create a box to jump over, the visual version
		Entity* entity = _mgr->createEntity("Jump", "Crate.mesh");
		entity->setNormaliseNormals(true);
		entity->setCastShadows(true);

		SceneNode* node = _mgr->getRootSceneNode()->createChildSceneNode(entity->getName());
		node->attachObject(entity);
		node->setPosition(Vector3(0,0.3,-5));
		node->setOrientation(Quaternion(Radian(0.4),Vector3(1,0,0)));
		node->setScale(0.3,0.1,0.4);

		// Create the physical version (just static geometry, it can't move so
		// it doesn't need a body) and keep track of it
		_geoms.push_back(OgreOde::MeshInformer::createSingleStaticBox(node,_space));

		// The car is what we'll want to look at
		_last_node = vehicle->getSceneNode();
	}

	// Override the destructor since there's some special deleting to do
	virtual ~SimpleScenes_Buggy()
	{
		delete vehicle;

		// Destroy the jump manually since it's not associated with 
		// any body it won't get deleted automatically
		_mgr->destroySceneNode("Jump");
		_mgr->removeEntity("Jump");

		// Geometries and Joints will get deleted by the base class
	}

	// Return our name
	virtual const String& getName()
	{
		static String name = "Test Buggy";
		return name;
	}

	// Tell the user what keys they can use
	virtual const String& getKeys()
	{
		static String keys = "I/K - Accelerate/Brake, J/L - Turn, X - Change drive mode";
		return keys;
	}

	// Our special key handling
	virtual void frameEnded(Real time,InputReader* input)
	{
		// Do default processing
		SimpleScenes::frameEnded(time,input);

		// Tell the vehicle what digital inputs are being pressed; left, right, power and brake
		// There are equivalent methods for analogue controls, current you can't change gear so
		// you can't reverse!
		vehicle->setInputs(input->isKeyDown(KC_J),input->isKeyDown(KC_L),input->isKeyDown(KC_I),input->isKeyDown(KC_K));

		// Update the vehicle, you need to do this every time step
		vehicle->update(time);
		
		// Change the drive mode between front, rear and 4wd
		if((input->isKeyDown(KC_X))&&(_key_delay > SimpleScenes::KEY_DELAY))
		{
			switch(_drive)
			{
				// Switch from rear to front
				case 'R':
					_drive = 'F';

					vehicle->getWheel(0)->setPowerFactor(1);
					vehicle->getWheel(1)->setPowerFactor(1);
					vehicle->getWheel(2)->setPowerFactor(0);
					vehicle->getWheel(3)->setPowerFactor(0);

					setInfoText("Front wheel drive");
				break;

				// Switch from front to all
				case 'F':
					_drive = '4';

					vehicle->getWheel(0)->setPowerFactor(0.6);
					vehicle->getWheel(1)->setPowerFactor(0.6);
					vehicle->getWheel(2)->setPowerFactor(0.4);
					vehicle->getWheel(3)->setPowerFactor(0.4);

					setInfoText("All wheel drive");
				break;

				// Switch from all to rear
				case '4':
					_drive = 'R';

					vehicle->getWheel(0)->setPowerFactor(0);
					vehicle->getWheel(1)->setPowerFactor(0);
					vehicle->getWheel(2)->setPowerFactor(1);
					vehicle->getWheel(3)->setPowerFactor(1);

					setInfoText("Rear wheel drive");
				break;
			}
			_key_delay = 0.0;
		}
	}

	// Override the collision callback to set our own parameters
	bool collision(OgreOde::Contact* contact)
	{
		// If the base class doesn't think the collision needs to 
		// happen then we won't do anything either
		if(!SimpleScenes::collision(contact)) return false;

		// Set the floor to be a bit slippy
		contact->setCoulombFriction(12.0);
		return true;
	}

protected:
	// Keep track of the things we need to delete manually or change according to user input
	OgreOde::Vehicle *vehicle;
	char _drive;
};

#endif

⌨️ 快捷键说明

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