Because I Am Noob.
Noob@FOSProject.vn
Code: Select all
/**
* \RunOn: SGF(Simple Game Framework) - Irrlicht Engine
* \Summary: Third Person Camera
* \Filename: ThirdPersonCamera.cpp
* \Encoding: UTF-8
* \CreatedDate: 2:17:PM 2008/06/15
* \CreatedBy: FOSP Team - NamVoDang(kiemkhachchilo)
* \Copyright: Permission is granted to anyone to use this software/code
* \ for any purpose, including commercial applications, and to alter it
* \ and redistribute it freely
**/
#include "ThirdPersonCamera.h"
using namespace irr;
using namespace scene;
using namespace core;
ThirdPersonCamera::ThirdPersonCamera(ISceneNode* targetNode)
{
this->targetNode = targetNode;
Zoom = 2;
nRotationX = 0;
nRotationY = 0;
rotating = false;
Mouse[0] = false;
Mouse[1] = false;
rotation = vector3df(0,15,-25);//Default rotation on level enter.
mouseDelegate.addRef();
mouseDelegate.bind(this,&ThirdPersonCamera::onMouse);
}
void ThirdPersonCamera::onAdd()
{
cursor = manager->getCore()->getGraphicDevice()->getCursorControl();
ISceneManager* smgr = manager->getCore()->getGraphicDevice()->getSceneManager();
camera = smgr->addCameraSceneNode();//add camera.
//terrain.
terrain = manager->getCore()->globalVars["terrain"].getAs<irr::scene::ITerrainSceneNode*>();
manager->getCore()->getInputManager()->getMouseEvent()->addDelegate(&mouseDelegate);
manager->setActive(this,true);
}
void ThirdPersonCamera::update(float deltaTime)
{
if (Mouse[1])//Rotation process.
{
if (!rotating)
{
mousePos = cursor->getPosition();
curMousePos = mousePos;
rotating = true;
}
else
{
rotation = vector3df(0,15,-25);
mousePos = cursor->getPosition();
nRotationX += mousePos.X - curMousePos.X;
nRotationY += mousePos.Y - curMousePos.Y;
rotation.rotateYZBy((nRotationY)*0.25,vector3df(0,0,0));
rotation.rotateXZBy((nRotationX)*-0.25,vector3df(0,0,0));//The value (mousePos.X - curMousePos.X) determine camera rotation.
curMousePos = mousePos;
}
}
target = targetNode->getPosition();
camPos = rotation;
camPos *= Zoom;//Zoom process.
camPos += target;
//Invoid under terrain camera
if (camPos.Y < terrain->getHeight(camPos.X,camPos.Z) + 5)
{
camPos.Y = terrain->getHeight(camPos.X,camPos.Z) + 5;
}
//Update camera.
camera->setTarget(target);
camera->setPosition(camPos);
}
void ThirdPersonCamera::onRemove()
{
manager->getCore()->getInputManager()->getMouseEvent()->removeDelegate(&mouseDelegate);//remove delegate
camera->remove();//remove object
}
void ThirdPersonCamera::onMouse(SMouseEvent& args)
{
if(args.mouseEvent==EMIE_MOUSE_WHEEL)//On Wheel Event.
{
Zoom = Zoom + args.wheel*0.25;//change zoom speed by wheel value
//Dont zoom in or out in max and min zoom value is reached
if (Zoom < 0.5)
Zoom = 0.5;
if (Zoom > 3.5)
Zoom = 3.5;
}
else if(args.mouseEvent==EMIE_RMOUSE_PRESSED_DOWN)
{
Mouse[1] = true;
}
else if(args.mouseEvent==EMIE_RMOUSE_LEFT_UP)
{
Mouse[1] = false;
rotating = false;
}
}
ICameraSceneNode* ThirdPersonCamera::getPointer() const
{
return camera;
}
Code: Select all
/**
* \RunOn: SGF(Simple Game Framework) - Irrlicht Engine
* \Summary: Third Person Camera
* \Filename: ThirdPersonCamera.h
* \Encoding: UTF-8
* \CreatedDate: 2:17:PM 2008/06/15
* \CreatedBy: FOSP Team - NamVoDang(kiemkhachchilo)
* \CreatedBy: FOSP Team - NamVoDang(kiemkhachchilo)
* \Copyright: Permission is granted to anyone to use this software/code
* \ for any purpose, including commercial applications, and to alter it
* \ and redistribute it freely
**/
#ifndef _THIRD_PERSON_CAMERA_H_
#define _THIRD_PERSON_CAMERA_H_
#include "SGF.h"
#include <functional>
#include <irrlicht.h>
using namespace irr;
using namespace scene;
using namespace core;
class ThirdPersonCamera: public sgfEntity
{
public:
ThirdPersonCamera(ISceneNode* targetNode);
ICameraSceneNode* getPointer() const;
private:
float Zoom;
s32 Rotation,nRotationX,nRotationY;
bool rotating;
bool Mouse[1];//Left 0, Right 1.
gui::ICursorControl* cursor;
position2d<s32> mousePos;
position2d<s32> curMousePos;
vector3df camPos;//Camera position.
vector3df target;//Look at.
vector3df rotation;
f32 RotateStartX, RotateStartY, curentRotateStartX, curentRotateStartY;
ISceneNode* targetNode;//Character.
ICameraSceneNode* camera;//Camera.
ITerrainSceneNode* terrain;
sgfMethodDelegate<ThirdPersonCamera,SMouseEvent> mouseDelegate;//Register mouse event.
void onAdd();
void update(float deltaTime);//Update.
void onRemove();
void onMouse(SMouseEvent& args);
};
#endif
- Kiếm trung đệ nhất môn -
Võ Đang