Exemplo n.º 1
0
	void PlaypenApp::createObject(const std::string& material, ObjectType type)
	{
		switch(type)
		{
			case OBJECT_TYPE_BOX:
			{
				Ogre::Vector3 boxDim(5, 5, 5);
				opal::Solid* s = mSimulator->createSolid();
				s->setPosition(mCreationPoint);
				opal::BoxShapeData data;
				data.dimensions.set(boxDim[0], boxDim[1], boxDim[2]);
				s->addShape(data);
				createPhysicalEntityBox("", material, boxDim, s);
				break;
			}
			case OBJECT_TYPE_SPHERE:
			{
				Ogre::Real radius = 3; // testing
				opal::Solid* s = mSimulator->createSolid();
				s->setPosition(mCreationPoint);
				opal::SphereShapeData data;
				data.radius = radius;
				data.material.density = 1; // testing
				s->addShape(data);
				createPhysicalEntitySphere("", material, radius, s);
				break;
			}
			case OBJECT_TYPE_WALL:
			{
				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, material);
				break;
			}
			case OBJECT_TYPE_TOWER:
			{
				createTower(2, 2, 15, opal::Vec3r(3, 1.5, 1.5), 
					opal::Matrix44r(), material);
				break;
			}
			case OBJECT_TYPE_RAGDOLL:
			{
				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("", material, 
									boxDim, s);
								break;
							}
							case opal::SPHERE_SHAPE:
							{
								opal::real r = 
									((opal::SphereShapeData*)shapeData)->radius;
								createPhysicalEntitySphere("", 
									material, r, s);
								break;
							}
							case opal::CAPSULE_SHAPE:
							{
								opal::real r = 
									((opal::CapsuleShapeData*)shapeData)->radius;
								opal::real l = 
									((opal::CapsuleShapeData*)shapeData)->length;
								createPhysicalEntityCapsule("", 
									material, r, l, s);
								break;
							}
							default:
								assert(false);
						}
					}
				}
				break;
			}
			default:
				assert(false);
				break;
		}
	}
Exemplo n.º 2
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;
	}