void PhysicsRagDoll::setPositionOrientation(const Ogre::Vector3& pos, const Ogre::Quaternion &orient)
{
    Ogre::Vector3 oldPos = mNode->_getDerivedPosition();
    Ogre::Quaternion oldOri = mNode->_getDerivedOrientation();
    Ogre::Quaternion oldOriInv = oldOri.Inverse();

    for (RagBoneMapIterator it = mRagBonesMap.begin(); it != mRagBonesMap.end(); it++)
    {
        OgreNewt::Body* body = it->second->getBody();
        if (body)
        {
            Ogre::Vector3 boneOldPos;
            Ogre::Quaternion boneOldOri;
            body->getPositionOrientation(boneOldPos, boneOldOri);
            
            // get old position and orientation in local space
            Ogre::Vector3 boneOldLocalPos = oldOriInv*(boneOldPos - oldPos);
            Ogre::Quaternion boneOldLocalOri = oldOriInv*boneOldOri;

            // calculate and set new position in orientation
            body->setPositionOrientation(pos + orient*boneOldLocalPos, orient*boneOldLocalOri);
            body->unFreeze();
        }
    }
    mNode->setPosition(pos);
    mNode->setOrientation(orient);
}
    void PhysicsManager::addLevelGeometry( Ogre::Entity* levelEntity, const std::vector<OgreNewt::CollisionPtr> &collisions)
    {
        RlAssert1(levelEntity);
        RlAssert1(levelEntity->getParentSceneNode());

        SceneNode* node = levelEntity->getParentSceneNode();
        //Level entity has to be attached to a scene node.
        

        // try one compound collision for the entity if there are several collisions
        OgreNewt::CollisionPtr collision;
        switch( collisions.size() )
        {
            case 0:
                break;
            case 1:
                collision = collisions[0];
                break;
            default:
                collision = OgreNewt::CollisionPtr(new OgreNewt::CollisionPrimitives::CompoundCollision(mWorld, collisions, 0));
                break;
        }

        if( collision )
        {
            OgreNewt::Body* body = new OgreNewt::Body(mWorld, collision );


            body->attachNode(node);
            body->setPositionOrientation(node->_getDerivedPosition(),
                node->_getDerivedOrientation());
            body->setMaterialGroupID(getMaterialID("level"));

            mLevelBodiesQuadTree.add(body);
            //mLevelBodies.push_back(body);
        }

        // adjust worldAABB
        Vector3 minV(mWorldAABB.getMinimum());
        Vector3 maxV(mWorldAABB.getMaximum());

        AxisAlignedBox entityAABB = levelEntity->getWorldBoundingBox(true);
        minV.makeFloor(entityAABB.getMinimum());
        maxV.makeCeil(entityAABB.getMaximum());
        mWorldAABB.setMinimum(minV - Vector3(50, 50, 50));
        mWorldAABB.setMaximum(maxV + Vector3(50, 50, 50));

        mWorld->setWorldSize(mWorldAABB);
    }
void OgreNewtonApplication::createScene()
{
	// sky box.
	mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");
	
	// shadows on!
	mSceneMgr->setShadowTechnique( Ogre::SHADOWTYPE_STENCIL_ADDITIVE );


	// floor object!
	Entity* floor;
	SceneNode* floornode;
	floor = mSceneMgr->createEntity("Floor", "simple_terrain.mesh" );
	floornode = mSceneMgr->getRootSceneNode()->createChildSceneNode( "FloorNode" );
	floornode->attachObject( floor );
	floor->setMaterialName( "Simple/BeachStones" );

	floor->setCastShadows( false );

	//Ogre::Vector3 siz(100.0, 10.0, 100.0);
	OgreNewt::CollisionPtr col = OgreNewt::CollisionPtr(new OgreNewt::CollisionPrimitives::TreeCollision( m_World, floor, true, 0 ));
	OgreNewt::Body* bod = new OgreNewt::Body( m_World, col );
#ifdef OGRENEWT_NO_COLLISION_SHAREDPTR
	delete col;
#endif
	
	//floornode->setScale( siz );
	bod->attachNode( floornode );
	bod->setPositionOrientation( Ogre::Vector3(0.0,-4.0,0.0), Ogre::Quaternion::IDENTITY );

	

	

	// position camera
	mCamera->setPosition(0.0, -2.0, 10.0);
	mCamera->setNearClipDistance( 0.01f );

	//make a light
	Ogre::Light* light;

	light = mSceneMgr->createLight( "Light1" );
	light->setType( Ogre::Light::LT_POINT );
	light->setPosition( Ogre::Vector3(0.0, 100.0, 100.0) );



}
OgreNewt::Body* OgreNewtonApplication::makeSimpleBox( Ogre::Vector3& size, Ogre::Vector3& pos, Ogre::Quaternion& orient )
{
	Entity* box1;
	SceneNode* box1node;

	box1 = mSceneMgr->createEntity( "Entity"+Ogre::StringConverter::toString(mEntityCount++), "box.mesh" );
	box1node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	box1node->attachObject( box1 );
	box1node->setScale( size );

	OgreNewt::ConvexCollisionPtr col = OgreNewt::ConvexCollisionPtr(new OgreNewt::CollisionPrimitives::Box( m_World, size, 0 ));
	OgreNewt::Body* bod = new OgreNewt::Body( m_World, col );


	// base mass on the size of the object.
	Ogre::Real mass = size.x * size.y * size.z * 2.5;
		
	// calculate the inertia based on box formula and mass
	Ogre::Vector3 inertia, offset;
    col->calculateInertialMatrix(inertia, offset);

#ifdef OGRENEWT_NO_COLLISION_SHAREDPTR
	delete col;
#endif
				
	bod->attachNode( box1node );
	bod->setMassMatrix( mass, mass*inertia );
    bod->setCenterOfMass(offset);
	bod->setStandardForceCallback();

	box1->setMaterialName( "Simple/BumpyMetal" );


	bod->setPositionOrientation( pos, orient );

	return bod;
}
void OgreNewtonApplication::createScene()
{

	// setup CEGUI
	mGUIRenderer = new CEGUI::OgreCEGUIRenderer( mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr );
	new CEGUI::System( mGUIRenderer );

	// load up CEGUI stuff...
	try
	{
		using namespace CEGUI;
		CEGUI::Logger::getSingleton().setLoggingLevel( CEGUI::Informative );

		CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
        CEGUI::System::getSingleton().setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
        CEGUI::System::getSingleton().setDefaultFont((CEGUI::utf8*)"BlueHighway-10");

		CEGUI::Window* sheet = CEGUI::WindowManager::getSingleton().createWindow( (CEGUI::utf8*)"DefaultWindow", (CEGUI::utf8*)"root_wnd" );
		CEGUI::System::getSingleton().setGUISheet( sheet );

		//makeGUI();
		//setupGUI();
	
	}
	catch (CEGUI::Exception)
	{}


	// sky box.
	mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");
	
	// shadows on!
	mSceneMgr->setShadowTechnique( Ogre::SHADOWTYPE_STENCIL_ADDITIVE );


	// floor object!
	Entity* floor;
	SceneNode* floornode;
	floor = mSceneMgr->createEntity("Floor", "simple_terrain.mesh" );
	floornode = mSceneMgr->getRootSceneNode()->createChildSceneNode( "FloorNode" );
	floornode->attachObject( floor );
	floor->setMaterialName( "Simple/BeachStones" );
	floor->setCastShadows( false );

	


	//Ogre::Vector3 siz(100.0, 10.0, 100.0);
	OgreNewt::CollisionPtr col = OgreNewt::CollisionPtr(new OgreNewt::CollisionPrimitives::TreeCollision( m_World, floor, true, 0 ));
	OgreNewt::Body* bod = new OgreNewt::Body( m_World, col );
#ifdef OGRENEWT_NO_COLLISION_SHAREDPTR
	delete col;
#endif
	
	//floornode->setScale( siz );
	bod->attachNode( floornode );
	bod->setPositionOrientation( Ogre::Vector3(0.0,-10.0,0.0), Ogre::Quaternion::IDENTITY );


	// make a simple rope.
	Ogre::Vector3 size(3,1.0,1.0);
	Ogre::Vector3 pos(0,1,0);
	Ogre::Quaternion orient = Ogre::Quaternion::IDENTITY;

	// loop through, making bodies and connecting them.
	OgreNewt::Body* parent = NULL;
	OgreNewt::Body* child = NULL;

	for (int x=0;x<8;x++)
	{
		// make the next box.
		child = makeSimpleBox(size, pos, orient);

		// now make a new joint connecting this to the last box.
		OgreNewt::Joint* joint;

		// make the joint right between the bodies...
		

		if (parent)
		{
			joint = new OgreNewt::BasicJoints::BallAndSocket( m_World, child, parent, pos-Ogre::Vector3(size.x/2,0,0) );
		}
		else
		{
			// no parent, this is the first joint, so just pass NULL as the parent, to stick it to the "world"
			joint = new OgreNewt::BasicJoints::BallAndSocket( m_World, child, NULL, pos-Ogre::Vector3(size.x/2,0,0) );
		}

		// offset pos a little more.
		pos += Ogre::Vector3(size.x,0,0);

		// save the last body for the next loop!
		parent = child;
	}

	for (int i=0; i<15;i++)
	{
		pos = Ogre::Vector3( 10-rand()%20, 4+rand()%2, 10-rand()%20 );
		size = Ogre::Vector3( 1+rand()%3, 1+rand()%3, 1+rand()%3 );

		OgreNewt::Body* bod = makeSimpleBox( size, pos, orient );
	}

	
	// position camera
	mCamera->setPosition(0.0, -3.0, 23.0);

	//make a light
	Ogre::Light* light;

	light = mSceneMgr->createLight( "Light1" );
	light->setType( Ogre::Light::LT_POINT );
	light->setPosition( Ogre::Vector3(0.0, 100.0, 100.0) );



}
void OgreNewtonApplication::createScene()
{
	// sky box.
	mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");
	
	// shadows on!
	mSceneMgr->setShadowTechnique( Ogre::SHADOWTYPE_STENCIL_ADDITIVE );


	// floor object!
	Entity* floor;
	SceneNode* floornode;
	floor = mSceneMgr->createEntity("Floor", "simple_terrain.mesh" );
	floornode = mSceneMgr->getRootSceneNode()->createChildSceneNode( "FloorNode" );
	floornode->attachObject( floor );
	floor->setMaterialName( "Simple/BeachStones" );

	floor->setCastShadows( false );

	//-------------------------------------------------------------
	// add some other objects.
	Entity* floor2;
	SceneNode* floornode2;
	floor2 = mSceneMgr->createEntity("Floor2", "simple_terrain.mesh" );
	floornode2 = floornode->createChildSceneNode( "FloorNode2" );
	floornode2->attachObject( floor2 );
	floor2->setMaterialName( "Simple/BeachStones" );
	floor2->setCastShadows( false );
	floornode2->setPosition( Ogre::Vector3(80.0f, 0.0f, 0.0f) );

	Entity* floor3;
	SceneNode* floornode3;
	floor3 = mSceneMgr->createEntity("Floor3", "simple_terrain.mesh" );
	floornode3 = floornode->createChildSceneNode( "FloorNode3" );
	floornode3->attachObject( floor3 );
	floor3->setMaterialName( "Simple/BeachStones" );
	floor3->setCastShadows( false );
	floornode3->setPosition( Ogre::Vector3(-80.0f, -5.0f, 0.0f) );
	floornode3->setOrientation( Ogre::Quaternion( Ogre::Degree(15.0f), Ogre::Vector3::UNIT_Z ) );
	//-------------------------------------------------------------

	// using the new "SceneParser" TreeCollision primitive.  this will automatically parse an entire tree of
	// SceneNodes (parsing all children), and add collision for all meshes in the tree.
	OgreNewt::CollisionPrimitives::TreeCollisionSceneParser* stat_col = new OgreNewt::CollisionPrimitives::TreeCollisionSceneParser( m_World );
	stat_col->parseScene( floornode, true, 0 );
	OgreNewt::Body* bod = new OgreNewt::Body( m_World, OgreNewt::CollisionPtr(stat_col) );
#ifdef OGRENEWT_NO_COLLISION_SHAREDPTR
	delete stat_col;
#endif
	
	bod->attachNode( floornode );
	bod->setPositionOrientation( Ogre::Vector3(0.0,-20.0,0.0), Ogre::Quaternion::IDENTITY );



	// make a simple rope.
	Ogre::Vector3 size(3,1.5,1.5);
	Ogre::Vector3 pos(0,3,0);
	Ogre::Quaternion orient = Ogre::Quaternion::IDENTITY;

	// loop through, making bodies and connecting them.
	OgreNewt::Body* parent = NULL;
	OgreNewt::Body* child = NULL;

	for (int x=0;x<5;x++)
	{
		// make the next box.
		child = makeSimpleBox(size, pos, orient);

		// now make a new joint connecting this to the last box.
		OgreNewt::Joint* joint;

		// make the joint right between the bodies...
		

		if (parent)
		{
			joint = new MyCustomBallSocket(child, parent, pos-Ogre::Vector3(size.x/2,0,0), Ogre::Vector3(Ogre::Vector3::UNIT_X) );
		}
		else
		{
			// no parent, this is the first joint, so just pass NULL as the parent, to stick it to the "world"
			joint = new MyCustomBallSocket(child, NULL, pos-Ogre::Vector3(size.x/2,0,0), Ogre::Vector3(Ogre::Vector3::UNIT_X) );
		}

		// offset pos a little more.
		pos += Ogre::Vector3(size.x,0,0);

		// save the last body for the next loop!
		parent = child;
	}

	
	

	

	// position camera
	mCamera->setPosition(0.0, -3.0, 20.0);

	//make a light
	Ogre::Light* light;

	light = mSceneMgr->createLight( "Light1" );
	light->setType( Ogre::Light::LT_POINT );
	light->setPosition( Ogre::Vector3(0.0, 100.0, 100.0) );



}
Esempio n. 7
0
void EngineMap::create() {
	//carrega atributos do xml
	String fileName = "media/maps/";
	fileName += _name.c_str();
	fileName += ".xml";
	
	TiXmlDocument doc(fileName.c_str());
	if (!doc.LoadFile()) return;

	TiXmlHandle handleDoc(&doc);
	
	//obtém elemento com as configurações do mapa
	TiXmlElement* elMap = handleDoc.FirstChild("map").Element();
	if (!elMap) return;

	String stPlayerStart            = elMap->Attribute("player_start");
	String stPlayerStartOrientation = elMap->Attribute("player_start_orientation");
	String stSkyBox                 = elMap->Attribute("skybox");
	String stMinWorldSize           = elMap->Attribute("min_world_size");
	String stMaxWorldSize           = elMap->Attribute("max_world_size");
	String stAmbientLight           = elMap->Attribute("ambient_light");
	
	if (stPlayerStart != "") {
		_playerStart = StringConverter::parseVector3(stPlayerStart);
	} else {
		_playerStart = Vector3::ZERO;
	}

	if (stPlayerStartOrientation != "") {
		_playerStartOrientation = StringConverter::parseQuaternion(stPlayerStartOrientation);
	} else {
		_playerStartOrientation = Quaternion(1,0,0,0);
	}

	_skybox       = stSkyBox;
	_minWorldSize = StringConverter::parseVector3(stMinWorldSize);
	_maxWorldSize = StringConverter::parseVector3(stMaxWorldSize);

	//cria a iluminação geral do mapa
	Vector3 ambientLight = StringConverter::parseVector3(stAmbientLight);
	EngineGlobalObjects::getInstance().getSceneManager()->setAmbientLight(ColourValue(ambientLight.x, ambientLight.y, ambientLight.z));	
	
	//obtém elementos e cria
	TiXmlElement* elObject = elMap->FirstChildElement("object");

	while(elObject) {  
		
		//obtém o tipo do objeto
		String stType = elObject->Attribute("type");
		
		//verifica o tipo
		if (stType == "static_mesh") {
			String stMass         = elObject->Attribute("mass");
			String stInertia      = elObject->Attribute("inertia");
			String stCollision    = elObject->Attribute("collision");
			String stCastShadows  = elObject->Attribute("castshadows");
			String stMeshFile     = elObject->Attribute("meshfile");
			String stMaterial     = elObject->Attribute("material");
			String stName         = elObject->Attribute("name");
			String stPosition     = elObject->Attribute("position");
			String stOrientation  = elObject->Attribute("orientation");
			String stScale        = elObject->Attribute("scale");
			String stCenterOfMass = elObject->Attribute("center_of_mass");
			
			//prepara os atributos
			bool castShadow		   = StringConverter::parseBool(stCastShadows);
			String nodeName        = "map_object_" + stName;
			String entityName      = "map_object_entity_" + stName;
			Real mass			   = StringConverter::parseReal(stMass);
			Vector3 inertia	       = StringConverter::parseVector3(stInertia);
			Vector3 position	   = StringConverter::parseVector3(stPosition);
			Quaternion orientation = StringConverter::parseQuaternion(stOrientation);
			Vector3 scale          = StringConverter::parseVector3(stScale);
			Vector3 centerOfMass   = StringConverter::parseVector3(stCenterOfMass);

			//cria o node e entity
			Entity *entity;
			entity = EngineGlobalObjects::getInstance().getSceneManager()->createEntity(entityName, stMeshFile);
			
			if (stMaterial != "") {
				entity->setMaterialName(stMaterial);
			}

			entity->setCastShadows(castShadow);

			SceneNode *node = EngineGlobalObjects::getInstance().getSceneManager()->getRootSceneNode()->createChildSceneNode(nodeName);
			node->attachObject(entity);

			//define tamanho
			node->setScale(scale);

			//cria colisão
			if (stCollision == "fixed") {
				
				OgreNewt::CollisionPtr col = OgreNewt::CollisionPtr(new OgreNewt::CollisionPrimitives::TreeCollision( EngineGlobalObjects::getInstance().getWorld(), entity, true, 0 ));
				OgreNewt::Body* bod = new OgreNewt::Body( EngineGlobalObjects::getInstance().getWorld(), col );			
				
				bod->attachNode( node );
				bod->setPositionOrientation( position, orientation );
				
				//define callback customizado
				bod->setCustomForceAndTorqueCallback<EngineMap>(&EngineMap::forceCallback, this);

			} else if (stCollision == "normal") {				
				
				OgreNewt::ConvexCollisionPtr col = OgreNewt::ConvexCollisionPtr(new OgreNewt::CollisionPrimitives::ConvexHull(EngineGlobalObjects::getInstance().getWorld(), entity, 0));
				OgreNewt::Body* bod = new OgreNewt::Body( EngineGlobalObjects::getInstance().getWorld(), col );

				bod->attachNode( node );
				bod->setMassMatrix( mass, inertia );
				bod->setCenterOfMass(centerOfMass);
				bod->setStandardForceCallback();

				bod->setPositionOrientation( position, orientation );
				
				//define callback customizado
				bod->setCustomForceAndTorqueCallback<EngineMap>(&EngineMap::forceCallback, this);
			}

		} else if (stType == "light") {
			
			String stName         = elObject->Attribute("name");
			String stPosition     = elObject->Attribute("position");
			String stDirection    = elObject->Attribute("direction");
			String stAttenuation  = elObject->Attribute("attenuation");
			String stCastShadows  = elObject->Attribute("castshadows");
			String stDiffuse      = elObject->Attribute("diffuse");
			String stLightrange   = elObject->Attribute("lightrange");
			String stLighttype    = elObject->Attribute("lighttype");
			String stPower        = elObject->Attribute("power");
			String stSpecular     = elObject->Attribute("specular");

			Light *light;
			light = EngineGlobalObjects::getInstance().getSceneManager()->createLight(stName);
			
			if(stLighttype=="LT_POINT") light->setType(Light::LT_POINT);
			if(stLighttype=="LT_SPOTLIGHT") light->setType(Light::LT_SPOTLIGHT);
			if(stLighttype=="LT_DIRECTIONAL") light->setType(Light::LT_DIRECTIONAL);

			light->setPosition(StringConverter::parseVector3(stPosition));
			light->setDiffuseColour(StringConverter::parseColourValue(stDiffuse));
			light->setSpecularColour(StringConverter::parseColourValue(stSpecular));
			
			Vector4 att = StringConverter::parseVector4(stAttenuation);
			light->setAttenuation(att.x, att.y, att.z, att.w);
			light->setDirection(StringConverter::parseVector3(stDirection));
			light->setPowerScale(StringConverter::parseReal(stPower));
			light->setSpotlightFalloff(1);

			light->setCastShadows(StringConverter::parseBool(stCastShadows));

		}

		elObject = elObject->NextSiblingElement("object");
	}

	//obtém elementos da fog e cria
	TiXmlElement* elFog = elMap->FirstChildElement("fog");

	while(elFog) {  
		String stFogMode    = elFog->Attribute("mode");
		String stFogColour  = elFog->Attribute("colour");
		String stFogStart   = elFog->Attribute("start");
		String stFogEnd     = elFog->Attribute("end");
		String stFogDensity = elFog->Attribute("density");
		
		_fogMode    = stFogMode;
		_fogColour  = StringConverter::parseColourValue(stFogColour);
		_fogStart   = StringConverter::parseReal(stFogStart);
		_fogEnd     = StringConverter::parseReal(stFogEnd);
		_fogDensity = StringConverter::parseReal(stFogDensity);
		
		//configura a fog no cenário se o modo da fog foi informado
		if (stFogMode != "") {
			FogMode fogMode;

			if (_fogMode == "none") fogMode = FogMode::FOG_NONE;
			if (_fogMode == "exp") fogMode = FogMode::FOG_EXP;
			if (_fogMode == "exp2") fogMode = FogMode::FOG_EXP2;
			if (_fogMode == "linear") fogMode = FogMode::FOG_LINEAR;

			EngineGlobalObjects::getInstance().getSceneManager()->setFog(fogMode, _fogColour, _fogDensity, _fogStart, _fogEnd); 
		}


		elFog = elFog->NextSiblingElement("fog");
	}



	//cria o céu
	createSkyBox();	

	doc.Clear();
}