im thinking i should put all the cockpit functions into a class, but when i moved all the functions over to a seperate file started getting errors left and right
Code: Select all
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
//--- set camera to behave as cockpit camera of ship ---
void makeCockpit(irr::scene::ICameraSceneNode *camera, //camera
irr::scene::ISceneNode *node, //scene node (ship)
irr::core::vector3df offset) //relative position of camera to node (ship)
{
// get transformation matrix of node
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
// transform forward vector of camera
irr::core::vector3df frv = irr::core::vector3df (0.0f, 0.0f, 1.0f);
m.transformVect(frv);
// transform upvector of camera
irr::core::vector3df upv = irr::core::vector3df (0.0f, 1.0f, 0.0f);
m.transformVect(upv);
// transform camera offset (thanks to Zeuss for finding it was missing)
m.transformVect(offset);
// set camera
camera->setPosition(node->getPosition() + offset); //position camera in front of the ship
camera->setUpVector(upv); //set up vector of camera
camera->setTarget(node->getPosition() + frv); //set target of camera (look at point) (thx Zeuss for correcting it)
// update absolute position
camera->updateAbsolutePosition();
}
void move(irr::scene::ISceneNode *node, irr::core::vector3df vel)
{
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
m.transformVect(vel);
node->setPosition(node->getPosition() + vel);
node->updateAbsolutePosition();
}
void rotate(irr::scene::ISceneNode *node, irr::core::vector3df rot)
{
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
irr::core::matrix4 n;
n.setRotationDegrees(rot);
m *= n;
node->setRotation( m.getRotationDegrees() );
node->updateAbsolutePosition();
}
void turn(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(0.0f, rot, 0.0f) );
}
void pitch(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(rot, 0.0f, 0.0f) );
}
void roll(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(0.0f, 0.0f, rot) );
}
// event reciever
bool keys[KEY_KEY_CODES_COUNT];
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(const SEvent &event)
{
if(event.EventType == EET_KEY_INPUT_EVENT)
{
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return true;
}
return false;
}
};
int main()
{
for(s32 i=0; i<KEY_KEY_CODES_COUNT; i++) keys[i] = false;
MyEventReceiver receiver;
IrrlichtDevice *device =
createDevice(video::EDT_OPENGL, core::dimension2d<u32>(800, 600), 32, false, false, false, &receiver);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
smgr->addSkyDomeSceneNode(driver->getTexture("../../media/skydome.jpg"),16,16,1.0f,2.0f);
scene::ISceneNode* node1 = smgr->addCubeSceneNode(); //replace with what ever you want
if (node1)
{
node1->setMaterialFlag(video::EMF_LIGHTING, false);
node1->setMaterialTexture(0, driver->getTexture("../../media/skydome.jpg"));
}
double nodeRot = 90;
scene::ISceneNode* node2 = smgr->addSphereSceneNode(); //just for reference
if (node2)
{
node2->setMaterialFlag(video::EMF_LIGHTING, false);
node2->setMaterialTexture(0, driver->getTexture("../../media/earth.jpg"));
node2->setPosition(core::vector3df(0,0,200));
node2->setRotation(vector3df(0,0,nodeRot));
node2->setScale(vector3df(120,120,120));
}
scene::ICameraSceneNode *camera = device->getSceneManager()->addCameraSceneNode();
while(device->run())
{
driver->beginScene(true, true, video::SColor(0,0,0,0));
smgr->drawAll();
{ // direction control
if(keys[irr::KEY_LEFT])
{
turn(node1, -0.1);
}
if(keys[irr::KEY_RIGHT])
{
turn(node1, 0.1);
}
if(keys[irr::KEY_UP])
{
pitch(node1, 0.1);
}
if(keys[irr::KEY_DOWN])
{
pitch(node1, -0.1);
}
if(keys[irr::KEY_COMMA])
{
roll(node1, 0.1);
}
if(keys[irr::KEY_PERIOD])
{
roll(node1, -0.1);
}
}
{// movement control
if(keys[irr::KEY_KEY_W])
{
move(node1, core::vector3df(0,0,0.1));
}
if(keys[irr::KEY_KEY_S])
{
move(node1, core::vector3df(0,0,-0.1));
}
}
makeCockpit(camera, node1, core::vector3df(0,7,-30));
if(nodeRot <= 360)
{
nodeRot++;
nodeRot = (nodeRot - 0.99);
}
else
{
nodeRot = 0;
}
node2->setRotation(vector3df(0,nodeRot,0));
driver->endScene();
}
device->drop();
return 1;
}