here is the basic idea. I will include something about angles aswell
Code: Select all
#include <irrlicht.h>
#include <iostream.h>
using namespace irr;
using namespace core;
using namespace video;
using namespace scene;
using namespace gui;
using namespace io;
#pragma comment(lib, "Irrlicht.lib")
//Globals
//Variables
float fAngleX = 0, fAngleY = 0, fCameraDistance = 100, fLookAtX = 0, fLookAtY = 0, fLookAtZ = 0;
IrrlichtDevice* Device = NULL;
ICameraSceneNode* Camera = NULL;
position2d<f32> MousePos;
//Classes
class EventHandler : public IEventReceiver
{
//Forgive my indentation for classes, I have only been learning C++ for a couple of months.
public:
virtual bool OnEvent(SEvent event)
{
switch(event.EventType)
{
case EET_MOUSE_INPUT_EVENT:
{
switch(event.MouseInput.Event)
{
case EMIE_MOUSE_WHEEL:
{
float fMultiplyer = 1;
fMultiplyer += event.MouseInput.Wheel/10;
fCameraDistance *= fMultiplyer;
//here you can have an if statement involving fCameraDistance that will change fCameraDistance if it is bigger than 100 to 100 etc.
}
break;
}
}
break;
case EET_KEY_INPUT_EVENT:
{
switch(event.KeyInput.Key)
{
case KEY_NUMPAD8: //Move "Forwards"
fLookAtX--;
break;
case KEY_NUMPAD2: //Move "Backwards"
fLookAtX++;
break;
case KEY_NUMPAD4: //Move "Left"
fLookAtZ--;
break;
case KEY_NUMPAD6: //Move "Right"
fLookAtZ++;
break;
case KEY_NUMPAD9: //Move "Up"
fLookAtY++;
break;
case KEY_NUMPAD1: //Move "Down"
fLookAtY--;
break;
case KEY_KEY_Q: //Quit
Device->closeDevice();
break;
}
}
break;
}
return false;
}
};
//Function Prototypes
void DrawCamera(float fLookX, float fLookY, float fLookZ, float fAX, float fAY, float fDist);
int main()
{
EventHandler MyReceiver;
Device = createDevice(EDT_OPENGL, dimension2d<s32>(1024, 768), 32, true, false, false, &MyReceiver);
IVideoDriver* Driver = Device->getVideoDriver();
ISceneManager* SceneManager = Device->getSceneManager();
Camera = SceneManager->addCameraSceneNode(0, vector3df(0, 0, 0), vector3df(0, 0, 0));
SceneManager->addTestSceneNode();
DrawCamera(fLookAtX, fLookAtY, fLookAtZ, fAngleX, fAngleY, fCameraDistance);
while(Device->run())
{
Driver->beginScene(true, true, SColor(255, 255, 255, 200));
SceneManager->drawAll();
Driver->endScene();
DrawCamera(fLookAtX, fLookAtY, fLookAtZ, fAngleX, fAngleY, fCameraDistance);
//I will implement here a basic camera that will move when the mouse is at the edge of the screen. My tests showed that the relative mouse pos is not infact 1.0 at the end of the screen. Perhaps this could be changed by niko? (awesome job though!)
MousePos = Device->getCursorControl()->getRelativePosition();
if(MousePos.X >= 0.99)
fAngleX += 0.1;
if(MousePos.Y >= 0.99)
fAngleY += 0.1;
if(!MousePos.X)
fAngleX -= 0.1;
if(!MousePos.Y)
fAngleY -= 0.1;
if(fAngleX >= 360)
fAngleX -= 360;
if(fAngleX <= 0)
fAngleX += 360;
if(fAngleY > 89)
fAngleY = 89;
if(fAngleY < -89)
fAngleY = -89;
}
return 0;
}
//Function Definitions
void DrawCamera(float fLookX, float fLookY, float fLookZ, float fAX, float fAY, float fDist)
{
vector3df CameraPos = Camera->getPosition(), CameraTarget = Camera->getTarget();
float fX = CameraPos.X, fY = CameraPos.Y, fZ = CameraPos.Z, fXZDistance;
fXZDistance = fDist*cos(PI*(fAngleY/180));
fY = fDist*sin(PI*(fAY/180));
fX = cos(PI*(fAX/180))*fXZDistance;
fZ = sin(PI*(fAX/180))*fXZDistance;
fX += fLookX;
fY += fLookY;
fZ += fLookZ;
CameraPos.X = fX;
CameraPos.Y = fY;
CameraPos.Z = fZ;
Camera->setPosition(CameraPos);
CameraTarget.X = fLookX;
CameraTarget.Y = fLookY;
CameraTarget.Z = fLookZ;
Camera->setTarget(CameraTarget);
//OK, very inefficient procedure, I would change it if anyone wants me to do a game quality camera.
}
well... there it is. My camera I made for you
The controls are, mouse to the edge of the screen rotates the view. numpad keys, 8 and 2 forward and back, 4 and 6 left and right and 1 and 9 up and down.
Have a try with just the code, there's a test scene node there for you.