Free Flight (space flight) functions
Thanks!
Thanks alot! i was just making a flightsim and looking for functions like this
However, i'm using the cockpit cam to look just behind the plane, and when i do roll(model,2), i dont roll a little sidways(i use it while turning to add a more realistic effect..), is this something obvious or a bug?
Thx.
-dudMan
However, i'm using the cockpit cam to look just behind the plane, and when i do roll(model,2), i dont roll a little sidways(i use it while turning to add a more realistic effect..), is this something obvious or a bug?
Thx.
-dudMan
got the node moving now, is there a way to smooth out the keyboard controls?
Code: Select all
#include "irrlicht.h"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
double playerspeed = 0.0;
// free flight functions
void makeCockpit(irr::scene::ICameraSceneNode *camera,
irr::scene::ISceneNode *node,
irr::core::vector3df offset)
{
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
irr::core::vector3df frv = irr::core::vector3df (0.0f, 0.0f, 1.0f);
m.transformVect(frv);
irr::core::vector3df upv = irr::core::vector3df (0.0f, 1.0f, 0.0f);
m.transformVect(upv);
m.transformVect(offset);
offset += node->getPosition();
camera->setPosition(offset);
camera->setUpVector(upv);
offset += frv;
camera->setTarget(offset);
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(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<s32>(800, 600), 32, false, false, false, &receiver);
device->setResizeAble(true);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
scene::ISceneNode* shuttle = smgr->addSphereSceneNode(); //replace with what ever you want
if (shuttle)
shuttle->setMaterialFlag(video::EMF_LIGHTING, false);
scene::ISceneNode* earth = smgr->addSphereSceneNode();
scene::ICameraSceneNode *camera = device->getSceneManager()->addCameraSceneNode();
while(device->run())
{
driver->beginScene(true, true, SColor(255,100,101,140));
smgr->drawAll();
// direction control
if(keys[irr::KEY_LEFT])
{
turn(shuttle, 0.01);
}
if(keys[irr::KEY_RIGHT])
{
turn(shuttle, -0.01);
}
if(keys[irr::KEY_UP])
{
pitch(shuttle, 0.01);
}
if(keys[irr::KEY_DOWN])
{
pitch(shuttle, -0.01);
}
if(keys[irr::KEY_COMMA])
{
roll(shuttle, 0.01);
}
if(keys[irr::KEY_PERIOD])
{
roll(shuttle, -0.01);
}
// movement control
if(keys[irr::KEY_KEY_W])
{
playerspeed = playerspeed + 0.0001;
if (playerspeed = 0.01)
playerspeed =0.009;
move(shuttle, core::vector3df(0,0,0.01));
}
if(keys[irr::KEY_KEY_S])
{
playerspeed = playerspeed - 0.0001;
if (playerspeed = -0.01)
playerspeed = -.009;
move(shuttle, core::vector3df(0,0,playerspeed));
}
if(keys[irr::KEY_KEY_Q])
{
playerspeed = 0;
}
move(shuttle, core::vector3df(0,0,playerspeed));
makeCockpit(camera, shuttle, core::vector3df(0,7,-30));
driver->endScene();
}
device->drop();
return 1;
}
-
- Posts: 275
- Joined: Fri May 12, 2006 6:37 pm
- Location: Germany
arras! You ARE the MAN!
After studying a bit about matrices in relation to 3d graphics I've finally understood your code and converted my horrible math (damn sin/cos and trigo) to your way and everything is working excellent.
Smoothness and accurate-ness. Everything I need is there.
Thank you very much, your code indeed helped me a lot.
P.S
I've modified it just a bit to suit my movement animator which made it much more better now yay.. (though I didn't upload the changes, [not sure if 'yet'] - but it can all be accessed in my engine and I'll later edit this post with a link to the code)
Edit:
I forgot to say that you no longer need to "updateAbsolutePosition()" in your functions. I'm not sure what was with Irr v1.2/1.3 but I use v1.4 and it works the same without it.
After studying a bit about matrices in relation to 3d graphics I've finally understood your code and converted my horrible math (damn sin/cos and trigo) to your way and everything is working excellent.
Smoothness and accurate-ness. Everything I need is there.
Thank you very much, your code indeed helped me a lot.
P.S
I've modified it just a bit to suit my movement animator which made it much more better now yay.. (though I didn't upload the changes, [not sure if 'yet'] - but it can all be accessed in my engine and I'll later edit this post with a link to the code)
Edit:
I forgot to say that you no longer need to "updateAbsolutePosition()" in your functions. I'm not sure what was with Irr v1.2/1.3 but I use v1.4 and it works the same without it.
Agreed! This code helped out my project, though I'm still trying to figure out what EXACTLY one of those functions does... I need to study my matrix math a bit more.
The Open Descent Foundation is always looking for programmers! http://www.odf-online.org
"I'll find out if what I deleted was vital here shortly..." -d3jake
"I'll find out if what I deleted was vital here shortly..." -d3jake
Glad it is of some help to you both It is just matrix math. It does the same as sin/cos math.
MasterGod >> yes its good idea. I am also thinking for few weeks about that ...putting all in to scene node animator. I want to do the same with my cameras (code is not here).
updateAbsolutePosition() was necessary back in older versions of Irrlicht else you got strange results. I do not remember for which version this was coded originally but it was well before 1.0
But do not forget gimbal lock. If you want to avoid it, you should look at quaternions.
MasterGod >> yes its good idea. I am also thinking for few weeks about that ...putting all in to scene node animator. I want to do the same with my cameras (code is not here).
updateAbsolutePosition() was necessary back in older versions of Irrlicht else you got strange results. I do not remember for which version this was coded originally but it was well before 1.0
But do not forget gimbal lock. If you want to avoid it, you should look at quaternions.