Code: Select all
#include <irrlicht.h>
struct ship
{
ship(void)
{
Forward = false;
Backward = false;
Left = false;
Right = false;
Speed = 0.f;
Camera = 0;
Node = 0;
}
void init(irr::scene::ISceneManager* smgr, irr::scene::ICameraSceneNode* cam)
{
Camera = cam;
Node = smgr->addCubeSceneNode();
Node->setScale(irr::core::vector3df(2,0.5,5));
//Node->setMaterialFlag(irr::video::EMF_LIGHTING, false);
}
bool Forward;
bool Backward;
bool Left;
bool Right;
irr::f32 Speed;
irr::core::vector3df Rotation;
irr::scene::ISceneNode* Node;
irr::scene::ICameraSceneNode* Camera;
irr::core::vector3df CamPos;
void update(const irr::f32& diff)
{
irr::core::vector3df vec(0,0,1);
irr::core::vector3df up(0,1,0);
irr::core::matrix4 mat0;
mat0.setRotationDegrees(irr::core::vector3df(Rotation.X, Rotation.Y, 0.f));
irr::core::matrix4 mat1;
mat1.setRotationDegrees(irr::core::vector3df(0, 0, Rotation.Z));
irr::core::matrix4 mat2 = mat0*mat1;
mat0.rotateVect(vec);
mat2.rotateVect(up);
//printf("Roll: %f\n", Rotation.Z);
//printf("Up: %f, %f, %f\n", up.X, up.Y, up.Z);
if (Forward)
{
Speed = 100.f;
}
else if (Backward)
Speed = -100.f;
else
Speed = 0.f;
if (Left)
{
irr::core::vector3df v(90.f*diff*sin(Rotation.Z/180.f*irr::core::PI), -90.f*diff*cos(Rotation.Z/180.f*irr::core::PI), 0);
Rotation+=v;
}
else if (Right)
{
irr::core::vector3df v(-90.f*diff*sin(Rotation.Z/180.f*irr::core::PI), 90.f*diff*cos(Rotation.Z/180.f*irr::core::PI), 0);
Rotation+=v;
}
if (Rotation.X > 360.f)
Rotation.X -= 360.f;
if (Rotation.X < -360.f)
Rotation.X += 360.f;
if (Rotation.Y > 360.f)
Rotation.Y -= 360.f;
if (Rotation.Y < -360.f)
Rotation.Y += 360.f;
if (Rotation.Z > 360.f)
Rotation.Z -= 360.f;
if (Rotation.Z < -360.f)
Rotation.Z += 360.f;
if (up.Y > 0.f)
{
irr::core::vector3df rot = vec.getHorizontalAngle();
if (Rotation.Z < -90 && Rotation.Z > -270)
{
Rotation.Z += 180;
Rotation.X = rot.X;
Rotation.Y = rot.Y;
}
else if (Rotation.Z > 90 && Rotation.Z < 270)
{
Rotation.Z -= 180;
Rotation.X = rot.X;
Rotation.Y = rot.Y;
}
}
if (Node)
{
Node->setPosition(Node->getPosition()+vec*Speed*diff);
Node->setRotation(mat2.getRotationDegrees());
if (Camera)
{
//Camera->setUpVector(up);
Camera->setTarget(Node->getPosition());
CamPos = Node->getPosition()-vec*100+up*20;
Camera->setPosition(Camera->getPosition()+(CamPos-Camera->getPosition())*diff);
}
}
}
}SpaceShip;
struct Events : irr::IEventReceiver
{
Events(irr::gui::ICursorControl* cursor) : MousePos(320, 240)
{
Cursor = cursor;
}
irr::core::vector2di MousePos;
irr::gui::ICursorControl* Cursor;
bool OnEvent(const irr::SEvent& event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
{
if (event.KeyInput.Key == irr::KEY_KEY_W)
{
SpaceShip.Forward = event.KeyInput.PressedDown;
}
else if (event.KeyInput.Key == irr::KEY_KEY_S)
{
SpaceShip.Backward = event.KeyInput.PressedDown;
}
else if (event.KeyInput.Key == irr::KEY_KEY_A)
{
SpaceShip.Left = event.KeyInput.PressedDown;
}
else if (event.KeyInput.Key == irr::KEY_KEY_D)
{
SpaceShip.Right = event.KeyInput.PressedDown;
}
}
else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
if (event.MouseInput.Event == irr::EMIE_MOUSE_MOVED)
{
if (Cursor->getRelativePosition().Y != 0.5f)
{
SpaceShip.Rotation.X -= (Cursor->getRelativePosition().Y-0.5f)*180.f*cos(SpaceShip.Rotation.Z/180.f*irr::core::PI);
SpaceShip.Rotation.Y -= (Cursor->getRelativePosition().Y-0.5f)*180.f*sin(SpaceShip.Rotation.Z/180.f*irr::core::PI);
Cursor->setPosition(Cursor->getRelativePosition().X, 0.5f);
}
if (Cursor->getRelativePosition().X != 0.5f)
{
SpaceShip.Rotation.Z -= (Cursor->getRelativePosition().X-0.5f)*180.f;
Cursor->setPosition(0.5f, Cursor->getRelativePosition().Y);
}
return true;
}
}
return false;
}
};
int main()
{
irr::IrrlichtDevice* device = irr::createDevice(irr::video::EDT_OPENGL);
irr::scene::ISceneManager* smgr = device->getSceneManager();
irr::video::IVideoDriver* driver = device->getVideoDriver();
irr::scene::ICameraSceneNode* camera = smgr->addCameraSceneNode();
irr::scene::ISceneNode* Node = smgr->addCubeSceneNode();
Node->setPosition(irr::core::vector3df(15,0,0));
Node = smgr->addCubeSceneNode();
Node->setPosition(irr::core::vector3df(0,-10,0));
Node->setMaterialFlag(irr::video::EMF_LIGHTING, false);
Node->setScale(irr::core::vector3df(100,1,100));
SpaceShip.init(smgr, camera);
device->getCursorControl()->setPosition(320,240);
Events events(device->getCursorControl());
device->setEventReceiver(&events);
irr::u32 LastTime = device->getTimer()->getTime();
irr::f32 Diff = 0;
while(device->run())
{
irr::u32 Time = device->getTimer()->getTime();
Diff = 0.f;
if (LastTime < Time)
{
Diff = Time-LastTime;
Diff /= 1000.f;
}
SpaceShip.update(Diff);
driver->beginScene(true, true, irr::video::SColor(255,0,0,255));
smgr->drawAll();
driver->endScene();
LastTime = Time;
}
device->closeDevice();
device->run();
device->drop();
return 0;
}