Historique des modifications - Message

Message #4787

Sujet: Camera personalisée (style wow)


Type Date Auteur Contenu
Création du message 11-11-2008 09:56:15 tmyke

ramis Ecris:

mais cData est bien un attribut de IEventReceiver ?non ?

En fait non wink

Comme le dit NightBird, la classe cData est un singleton dans lequel il a prépositionné des donné qui vont justement servir dans son code de camera.
Vite fait sur le gaz, voici des codes qui sont valide.

d'abord la classe cData, un singleton, il faut bien sur alimenter au préalable avant emploi
class cData
{
 protected:
	float cameraspeed;
	int		width;
	int		height;
	IrrlichtDevice *device;

	static cData *data_instance;

	cData()
	{
	}
	~cData()
	{
	}
 public:
	static cData* Get()
	{
           if(data_instance==0)
           {
               data_instance = new cData;
           }
           return data_instance;
	}
	static void Kill()
	{
		if (data_instance!=0)
		{
			delete data_instance;
			data_instance=0;
		}
	}

	float CameraSpeed()	{		return cameraspeed;	}
	int Width()	{ return width; }
	int Height(){ return height; }
	IrrlichtDevice * Device() { return device; }
	
	void SetCameraSpeed(float value)	{		cameraspeed= value;	}
	void Width(int value)	{  width=value; }
	void Height(int value){  height=value; }
	void Device(IrrlichtDevice *dev ) { device=dev; }
};
cData *cData::data_instance=0;

puis le code de cFollowCamera, légèrement modifié pour employer la cData

class cFollowCamera: public IEventReceiver
{
 public:
	ISceneNode *Target() 
	{
		return m_target;
	}
	void Target(ISceneNode *value) 
	{
		m_target = value;
		m_lastTargetPos = value->getPosition();
		m_target->grab();
	}
	void Update() 
	{
		if (!m_Cam || !m_target) return;
		if (MouseX <= cData::Get()->Width()*.05f) 
		{
			m_updated = true;
			m_yRotationAngle += cData::Get()->CameraSpeed();
			while (m_yRotationAngle > 2*PI)
				m_yRotationAngle -= 2*PI;
		}
		else if (MouseX >= cData::Get()->Width()*.95f) 
		{
			m_updated = true;
			m_yRotationAngle -= cData::Get()->CameraSpeed();	
			while (m_yRotationAngle < 2*PI)
					m_yRotationAngle += 2*PI;
		}

		if (m_lastTargetPos != m_target->getPosition()) 
		{
			m_updated = true;
			m_lastTargetPos = m_target->getPosition();
		}
		if (m_updated) 
		{
			m_updated = false;
			vector3df m_Camerapos = m_lastTargetPos;
			m_Camerapos.X += distance*sin(m_yRotationAngle);
			m_Camerapos.Z += distance*cos(m_yRotationAngle);
			m_Camerapos.Y += distance;
			m_Cam->setPosition(m_Camerapos);
			m_Cam->setTarget(m_lastTargetPos);
		}
	}

	void Zoom(float amount) 
	{
		distance *= amount;
		if (distance == 0) distance = 2; //there should be more elaborate capping of things here..
		m_updated = true;
	}
	bool OnEvent(SEvent event) 
	{
		if (event.EventType == EET_MOUSE_INPUT_EVENT) 
		{
			switch (event.MouseInput.Event) 
			{
			case EMIE_MOUSE_WHEEL:
					Zoom(((-event.MouseInput.Wheel)+1)/2+.5f); //make it be from .5 to 1.5
					return true;
					break;
			case EMIE_MOUSE_MOVED:
					MouseX = event.MouseInput.X;
					return true;
					break;
			}
		}
		return false;
	}

	cFollowCamera(ISceneNode *target) 
	{
		Target(target);
		m_Cam = cData::Get()->Device()->getSceneManager()->addCameraSceneNode(0,m_lastTargetPos + vector3df(0,20,20),m_lastTargetPos);
		m_yRotationAngle = 0.0f;
		distance = 20;
	}
	
	~cFollowCamera(void) 
	{
		m_target->drop();
	}
private:
	ISceneNode* m_target;
	ICameraSceneNode* m_Cam;
	vector3df m_lastTargetPos;
	bool m_updated;
	int MouseX;
	float distance;

	float m_yRotationAngle; //the angle about the Y axis
};

Voilà, c'est pas testé et c'est codé au saut du lit, donc à valider
wink

Retour

Options Liens officiels Caractéristiques Statistiques Communauté
Préférences cookies
Corrections
irrlicht
irrklang
irredit
irrxml
Propulsé par Django
xhtml 1.0
css 2.1
884 membres
1440 sujets
11337 messages
Dernier membre inscrit: Saidov17
164 invités en ligne
membre en ligne: -
RSS Feed