Exemple #1
0
	SimulationEngine::~SimulationEngine()
	{
		// This must occur before destroying the Ogre root or the Opal Simulator.
		destroyAllPhysicalEntities();

	#ifndef SIMULATION_ENGINE_PHYSICS_ONLY
		/*
		if (mInputManager)
		{
			mInputManager->destroyInputObject( mMouse );
			mInputManager->destroyInputObject( mKeyboard );
			OIS::InputManager::destroyInputSystem(mInputManager);
			mInputManager = NULL;
		}

		// This must occur before destroying the Opal Simulator.
		if (mPhysicalCamera)
		{
			delete mPhysicalCamera;
		}

		if (mOgreWindow)
		{
			mOgreWindow->destroy();
		}

		if (mOgreRoot)
		{
			delete mOgreRoot;
		}
		*/
	#endif

		if (mSimulator)
		{
			mSimulator->destroy();
		}
	}
Exemple #2
0
	bool PlaypenApp::processUnbufferedKeyInput(Ogre::Real dt)
	{
		// Check if we should quit looping.
		if(mInputDevice->isKeyDown(KC_ESCAPE) 
			|| mInputDevice->isKeyDown(KC_Q))
		{
			return false;
		}

		// Check if we should pause physics.
		if(mInputDevice->isKeyDown(KC_P) && mTimeUntilNextToggle <= 0)
		{
			mPaused = !mPaused;

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.5;
		}

		// Reset the scene.
		if(mInputDevice->isKeyDown(KC_R))
		{
			// Make sure the PhysicalCamera isn't grabbing anything.
			mPhysicalCamera->release();
			destroyAllPhysicalEntities();
			setupInitialPhysicalEntities();
		}

		// The following code updates the camera's position.
		opal::Vec3r cameraDir(0, 0, 0);
		bool cameraMoved = false;

		if (mInputDevice->isKeyDown(KC_LEFT) 
			|| mInputDevice->isKeyDown(KC_A))
		{
			// Move camera left.
			cameraDir[0] -= (dt * mMoveSpeed);
			cameraMoved = true;
		}

		if (mInputDevice->isKeyDown(KC_RIGHT) 
			|| mInputDevice->isKeyDown(KC_D))
		{
			// Move camera right.
			cameraDir[0] += (dt * mMoveSpeed);
			cameraMoved = true;
		}

		if (mInputDevice->isKeyDown(KC_UP) 
			|| mInputDevice->isKeyDown(KC_W))
		{
			// Move camera forward.
			cameraDir[2] -= (dt * mMoveSpeed);
			cameraMoved = true;
		}

		if (mInputDevice->isKeyDown(KC_DOWN) 
			|| mInputDevice->isKeyDown(KC_S))
		{
			// Move camera backward.
			cameraDir[2] += (dt * mMoveSpeed);
			cameraMoved = true;
		}

		if (!cameraMoved)
		{
			// Slow physical camera motion if necessary.
		}

		// Use the camera dir vector to translate the camera.
		mPhysicalCamera->moveRelative(cameraDir);

		// Toggle shadows.
		if(mInputDevice->isKeyDown(KC_H) && mTimeUntilNextToggle <= 0)
		{
			mUseShadows = !mUseShadows;

			if (mUseShadows)
			{
				mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE);
			}
			else
			{
				mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE);
			}

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.5;
		}

		// Toggle second light source.
		if(mInputDevice->isKeyDown(KC_L) && mTimeUntilNextToggle <= 0)
		{
			Ogre::Light* light1 = mSceneMgr->getLight("light1");

			if (light1->isVisible())
			{
				light1->setVisible(false);
			}
			else
			{
				light1->setVisible(true);
			}

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.5;
		}

		// Toggle GUI.
		if (mInputDevice->isKeyDown(KC_G) && mTimeUntilNextToggle <= 0)
		{
			mStatsOn = !mStatsOn;
			showDebugOverlay(mStatsOn);
			mTimeUntilNextToggle = 1;
		}

		// Handy screenshot saving procedure.
		if (mInputDevice->isKeyDown(KC_SYSRQ) 
			&& mTimeUntilNextToggle <= 0)
		{
			char tmp[20];
			sprintf(tmp, "screenshot_%d.png", ++mNumScreenShots);
			ExampleApplication::mWindow->writeContentsToFile(tmp);
			ExampleApplication::mWindow->setDebugText(String("Wrote ") 
				+ tmp);

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.5;
		}

		// Return true to continue looping.
		return true;
	}
Exemple #3
0
	bool PlaypenApp::appFrameStarted(opal::real dt)
	{
		// Do per-frame application-specific things here.

		// Handle speech input.
		bool keepLooping = true;
		bool makeObject = false;
		std::string material;
		ObjectType type;
		std::string outputString;
		while (voce::getRecognizerQueueSize() > 0)
		{
			std::string s = voce::popRecognizedString();

			//// Check if the string contains 'quit'.
			//if (std::string::npos != s.rfind("quit"))
			//{
			//	keepLooping = false;
			//}

			// Check if we should reset.
			if (std::string::npos != s.rfind("reset"))
			{
				// Make sure the PhysicalCamera isn't grabbing anything.
				mPhysicalCamera->release();
				destroyAllPhysicalEntities();
				setupInitialPhysicalEntities();
				voce::synthesize("reset");
				return true;
			}

			// Check for material color.
			if (std::string::npos != s.rfind("yellow"))
			{
				outputString += "yellow";
				material = "Plastic/Yellow";
			}
			else if (std::string::npos != s.rfind("red"))
			{
				outputString += "red";
				material = "Plastic/Red";
			}
			else if (std::string::npos != s.rfind("blue"))
			{
				outputString += "blue";
				material = "Plastic/Blue";
			}
			else if (std::string::npos != s.rfind("green"))
			{
				outputString += "green";
				material = "Plastic/Green";
			}
			else if (std::string::npos != s.rfind("purple"))
			{
				outputString += "purple";
				material = "Plastic/Purple";
			}
			else if (std::string::npos != s.rfind("orange"))
			{
				outputString += "orange";
				material = "Plastic/Orange";
			}
			else
			{
				// Default to dark gray.
				material = "Plastic/DarkGray";
			}

			// Check for object type.
			if (std::string::npos != s.rfind("box"))
			{
				outputString += " box";
				type = OBJECT_TYPE_BOX;
				makeObject = true;
			}
			else if (std::string::npos != s.rfind("sphere"))
			{
				outputString += " sphere";
				type = OBJECT_TYPE_SPHERE;
				makeObject = true;
			}
			else if (std::string::npos != s.rfind("wall"))
			{
				outputString += " wall";
				type = OBJECT_TYPE_WALL;
				makeObject = true;
			}
			else if (std::string::npos != s.rfind("tower"))
			{
				outputString += " tower";
				type = OBJECT_TYPE_TOWER;
				makeObject = true;
			}
			else if (std::string::npos != s.rfind("character"))
			{
				outputString += " character";
				type = OBJECT_TYPE_RAGDOLL;
				makeObject = true;
			}

			if (makeObject)
			{
				voce::synthesize(outputString);
				createObject(material, type);
			}
		}

		// Update the grasping spring line.
		if (mPhysicalCamera->isGrasping())
		{
			Ogre::Entity* pickingSphere = 
				mSceneMgr->getEntity("pickingSphere");
			if (!pickingSphere->isVisible())
			{
				pickingSphere->setVisible(true);
			}

			Ogre::Entity* springLine = 
				mSceneMgr->getEntity("springLine");
			if (!springLine->isVisible())
			{
				springLine->setVisible(true);
			}

			opal::Point3r desiredPos = 
				mPhysicalCamera->getGraspGlobalPos();
			Ogre::Vector3 point0(desiredPos[0], desiredPos[1], desiredPos[2]);

			opal::Point3r attachPos = mPhysicalCamera->getAttachGlobalPos();
			Ogre::Vector3 point1(attachPos[0], attachPos[1], attachPos[2]);

			pickingSphere->getParentSceneNode()->setPosition(point1);

			Ogre::Vector3 lineVec = point0 - point1;
			if (!lineVec.isZeroLength())
			{
				Ogre::SceneNode* springLineNode = 
					springLine->getParentSceneNode();
				springLineNode->setPosition(0.5 * (point0 + point1));
				
				springLineNode->setDirection(lineVec);
				springLineNode->setScale(0.1, 0.1, lineVec.length());
			}
			else
			{
				springLine->setVisible(false);
			}
		}
		else
		{
			Ogre::Entity* pickingSphere = 
				mSceneMgr->getEntity("pickingSphere");
			if (pickingSphere->isVisible())
			{
				pickingSphere->setVisible(false);
			}

			Ogre::Entity* springLine = 
				mSceneMgr->getEntity("springLine");
			if (springLine->isVisible())
			{
				springLine->setVisible(false);
			}
		}

		// Return true to continue looping.
		return keepLooping;
	}
Exemple #4
0
	bool PlaypenApp::processUnbufferedKeyInput(Ogre::Real dt)
	{
		// Check if we should quit looping.
		if(mKeyboard->isKeyDown(OIS::KC_ESCAPE) 
			|| mKeyboard->isKeyDown(OIS::KC_Q))
		{
			return false;
		}

		// Check if we should pause physics.
		if(mKeyboard->isKeyDown(OIS::KC_P) && mTimeUntilNextToggle <= 0)
		{
			mPaused = !mPaused;

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.5;
		}

		// Reset the scene.
		if(mKeyboard->isKeyDown(OIS::KC_R))
		{
			// Make sure the PhysicalCamera isn't grabbing anything.
			mPhysicalCamera->release();
			destroyAllPhysicalEntities();
			setupInitialPhysicalEntities();
		}

		// Create various types of objects when the number keys are 
		// pressed.

		// Create a box.
		if(mKeyboard->isKeyDown(OIS::KC_1) && mTimeUntilNextToggle <= 0)
		{
			Ogre::Vector3 boxDim(3, 3, 3);
			opal::Solid* s = mSimulator->createSolid();
			s->setPosition(mCreationPoint);
			opal::BoxShapeData data;
			data.dimensions.set(boxDim[0], boxDim[1], boxDim[2]);
			s->addShape(data);
			createPhysicalEntityBox("", "Plastic/Yellow", boxDim, s);

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.3;
		}

		// Create a sphere.
		if(mKeyboard->isKeyDown(OIS::KC_2) && mTimeUntilNextToggle <= 0)
		{
			Ogre::Real radius = 2;
			opal::Solid* s = mSimulator->createSolid();
			s->setPosition(mCreationPoint);
			opal::SphereShapeData data;
			data.radius = radius;
			s->addShape(data);
			createPhysicalEntitySphere("", "Plastic/Purple", radius, s);

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.3;
		}

		// Create a capsule.
		if(mKeyboard->isKeyDown(OIS::KC_3) && mTimeUntilNextToggle <= 0)
		{
			Ogre::Real radius = 2;
			Ogre::Real length = 5;
			opal::Solid* s = mSimulator->createSolid();
			s->setPosition(mCreationPoint);
			opal::CapsuleShapeData data;
			data.radius = radius;
			data.length = length;
			s->addShape(data);
			createPhysicalEntityCapsule("", "Plastic/Red", radius, length, s);

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.3;
		}

		// Create a cylinder.
		if(mKeyboard->isKeyDown(OIS::KC_4) && mTimeUntilNextToggle <= 0)
		{
			Ogre::Real radius = 3;
			Ogre::Real length = 5;
			opal::Solid* s = mSimulator->createSolid();
			s->setPosition(mCreationPoint);
			opal::CylinderShapeData data;
			data.radius = radius;
			data.length = length;
			s->addShape(data);
			createPhysicalEntityCylinder("", "Plastic/Blue", radius, length, s);

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.3;
		}

		// Create a long box.
		if(mKeyboard->isKeyDown(OIS::KC_5) && mTimeUntilNextToggle <= 0)
		{
			Ogre::Vector3 boxDim(2, 10, 3);
			opal::Solid* s = mSimulator->createSolid();
			s->setPosition(mCreationPoint);
			opal::BoxShapeData data;
			data.dimensions.set(boxDim[0], boxDim[1], boxDim[2]);
			data.material.density = 10;
			s->addShape(data);
			createPhysicalEntityBox("", "Plastic/Green", boxDim, s);

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.3;
		}

		//// Create a log.
		//if(mKeyboard->isKeyDown(OIS::KC_6) && mTimeUntilNextToggle <= 0)
		//{
		//	Ogre::Real logScale = 9;
		//	Ogre::Vector3 boxDim(0.4243, 0.4243, 2);
		//	boxDim *= logScale;
		//	opal::Solid* s = mSimulator->createSolid();
		//	opal::Matrix44r m;
		//	m.rotate(90, 1, 0, 0);
		//	s->setTransform(m);
		//	s->setPosition(mCreationPoint);
		//	opal::BoxShapeData data;
		//	data.dimensions.set(boxDim[0], boxDim[1], boxDim[2]);
		//	s->addShape(data);

		//	std::string name = generateUniqueName();
		//	Ogre::SceneNode* sn = mSceneMgr->getRootSceneNode()->
		//		createChildSceneNode(name);
		//	sn->scale(logScale, logScale, logScale);
		//	Entity* e = mSceneMgr->createEntity(name, "log.mesh");
		//	e->setNormaliseNormals(true);
		//	e->setMaterialName("Textured/Wood");
		//	sn->attachObject(e);
		//	createPhysicalEntity(name, sn, s);

		//	// Reset the timer for toggle keys.
		//	mTimeUntilNextToggle = 0.3;
		//}

		// Create a knot.
		if(mKeyboard->isKeyDown(OIS::KC_6) && mTimeUntilNextToggle <= 0)
		{
			opalSamples::PhysicalEntity* pe = createPhysicalEntityMesh("", 
				"knot.mesh", "Textured/RustedMetal", false, 0.1);
			pe->getSolid()->setPosition(opal::Point3r(0, 40, 0));

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.3;
		}

		// Create a wall.
		if(mKeyboard->isKeyDown(OIS::KC_7) && mTimeUntilNextToggle <= 0)
		{
			opal::Matrix44r m;
			m.rotate(45, 0, 1, 0);
			m.translate(0, 0, -23);
			createWall(6, 8, opal::Vec3r(3, 1.5, 1.5), m);

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.3;
		}

		// Create a tower.
		if(mKeyboard->isKeyDown(OIS::KC_8) && mTimeUntilNextToggle <= 0)
		{
			createTower(2, 2, 15, opal::Vec3r(3, 1.5, 1.5));

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.3;
		}

		// Create a ragdoll.
		if(mKeyboard->isKeyDown(OIS::KC_9) && mTimeUntilNextToggle <= 0)
		{
			opal::Blueprint ragdollBP;
			opal::loadFile(ragdollBP, "../data/blueprints/ragdoll.xml");
			opal::Matrix44r transform;
			transform.translate(mCreationPoint[0], mCreationPoint[1] - 5, 
				mCreationPoint[2]);

			// Instantiate the Blueprint.
			opal::BlueprintInstance instance;
			mSimulator->instantiateBlueprint(instance, ragdollBP, transform, 16);

			unsigned int i=0;
			for (i=0; i<instance.getNumSolids(); ++i)
			{
				opal::Solid* s = instance.getSolid(i);
				const opal::SolidData& data = s->getData();
				unsigned int j=0;
				for (j=0; j<data.getNumShapes(); ++j)
				{
					opal::ShapeData* shapeData = data.getShapeData(j);

					switch(shapeData->getType())
					{
						case opal::BOX_SHAPE:
						{
							opal::Vec3r dim = 
								((opal::BoxShapeData*)shapeData)->dimensions;
							Ogre::Vector3 boxDim(dim[0], dim[1], dim[2]);
							createPhysicalEntityBox("", "Plastic/LightBlue", 
								boxDim, s);
							break;
						}
						case opal::SPHERE_SHAPE:
						{
							opal::real r = 
								((opal::SphereShapeData*)shapeData)->radius;
							createPhysicalEntitySphere("", 
								"Plastic/LightBlue", r, s);
							break;
						}
						case opal::CAPSULE_SHAPE:
						{
							opal::real r = 
								((opal::CapsuleShapeData*)shapeData)->radius;
							opal::real l = 
								((opal::CapsuleShapeData*)shapeData)->length;
							createPhysicalEntityCapsule("", 
								"Plastic/LightBlue", r, l, s);
							break;
						}
						default:
							assert(false);
					}
				}
			}

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.5;
		}

		// The following code updates the camera's position.
		opal::Vec3r cameraDir(0, 0, 0);
		bool cameraMoved = false;

		if (mKeyboard->isKeyDown(OIS::KC_LEFT) 
			|| mKeyboard->isKeyDown(OIS::KC_A))
		{
			// Move camera left.
			cameraDir[0] -= (dt * mMoveSpeed);
			cameraMoved = true;
		}

		if (mKeyboard->isKeyDown(OIS::KC_RIGHT) 
			|| mKeyboard->isKeyDown(OIS::KC_D))
		{
			// Move camera right.
			cameraDir[0] += (dt * mMoveSpeed);
			cameraMoved = true;
		}

		if (mKeyboard->isKeyDown(OIS::KC_UP) 
			|| mKeyboard->isKeyDown(OIS::KC_W))
		{
			// Move camera forward.
			cameraDir[2] -= (dt * mMoveSpeed);
			cameraMoved = true;
		}

		if (mKeyboard->isKeyDown(OIS::KC_DOWN) 
			|| mKeyboard->isKeyDown(OIS::KC_S))
		{
			// Move camera backward.
			cameraDir[2] += (dt * mMoveSpeed);
			cameraMoved = true;
		}

		if (!cameraMoved)
		{
			// Slow physical camera motion if necessary.
		}

		// Use the camera dir vector to translate the camera.
		mPhysicalCamera->moveRelative(cameraDir);

		// Toggle shadows.
		if(mKeyboard->isKeyDown(OIS::KC_H) && mTimeUntilNextToggle <= 0)
		{
			mUseShadows = !mUseShadows;

			if (mUseShadows)
			{
				mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE);
			}
			else
			{
				mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE);
			}

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.5;
		}

		// Toggle second light source.
		if(mKeyboard->isKeyDown(OIS::KC_L) && mTimeUntilNextToggle <= 0)
		{
			Ogre::Light* light1 = mSceneMgr->getLight("light1");

			if (light1->isVisible())
			{
				light1->setVisible(false);
			}
			else
			{
				light1->setVisible(true);
			}

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.5;
		}

		// Toggle GUI.
		if (mKeyboard->isKeyDown(OIS::KC_G) && mTimeUntilNextToggle <= 0)
		{
			mStatsOn = !mStatsOn;
			showDebugOverlay(mStatsOn);
			mTimeUntilNextToggle = 1;
		}

		// Handy screenshot saving procedure.
		if (mKeyboard->isKeyDown(OIS::KC_SYSRQ) 
			&& mTimeUntilNextToggle <= 0)
		{
			char tmp[20];
			sprintf(tmp, "screenshot_%d.png", ++mNumScreenShots);
			ExampleApplication::mWindow->writeContentsToFile(tmp);
			mDebugText = String("Wrote ") + tmp;

			// Reset the timer for toggle keys.
			mTimeUntilNextToggle = 0.5;
		}

		// Return true to continue looping.
		return true;
	}