I tried to make a Asteroid-style spaceship with a box node initially. It works but messy
Someone please show me an example code on how to turn this code into
a system where it has the actor (the spaceship) that controls its meshes,nodes,etc,
while the actor controlled cleanly in the main loop?
(Procedural to OO)
Here's the lengthy part. Please copy n paste into your IDE and compile to see how it is. No other external files other than Irrlicht required.
Thanks in advance.
Code: Select all
#include <irrlicht.h>
#include <iostream>
#include <math.h>
using namespace std;
using namespace irr;
using namespace core;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(const SEvent& event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
return KeyIsDown[keyCode];
}
MyEventReceiver()
{
for (u32 i=0;i<KEY_KEY_CODES_COUNT;i++)
KeyIsDown[i] = false;
}
private:
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
float to_radians(float degrees)
{
return 3.14159265359 * degrees / 180.0;
}
/* main */
int main()
{
// event receiver
MyEventReceiver receiver;
// create device
IrrlichtDevice *device = createDevice( video::EDT_OPENGL,
core::dimension2d<s32>(640,480),16,false,false,false,&receiver);
// pointers as shortcuts
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* guienv = device->getGUIEnvironment();
// put an irrlicht logo
device->getGUIEnvironment()->addImage(
driver->getTexture("../../media/irrlichtlogoalpha2.tga"),
core::position2d<s32>(10,20));
// put an FPS camera
smgr->addCameraSceneNodeFPS(0, 100.0f, .1f);
device->getCursorControl()->setVisible(false);
// put a box that can be moved with WA(S)D
scene::ISceneNode * spaceship = smgr->addCubeSceneNode();
if (spaceship)
{
spaceship->setPosition(core::vector3df(0,0,30));
spaceship->setMaterialTexture(0,driver->getTexture("../../media/wall.bmp"));
spaceship->setMaterialFlag(video::EMF_LIGHTING,false);
}
spaceship->setScale(core::vector3df(0.3f,0.3f,0.3f));
// movement vars
int last_fps = -1;
u32 then = device->getTimer()->getTime();
const f32 spaceship_SPEED = .4f;
const f32 spaceship_ROT_SPEED = 100.f;
core::vector3df spaceshipVelocity;
f32 ff = .99f; // friction factor
// game loop
while (device->run())
{
// calculate time delta
const u32 now = device->getTimer()->getTime();
const f32 frameDeltaTime = (f32)(now-then)/1000.f;
then = now;
// take the pos and rot
core::vector3df spaceshipPos = spaceship->getPosition();
core::vector3df spaceshipRot = spaceship->getRotation();
// vector3df for later position calculations
core::vector3df dummyPos;
// rotate the ship
if (receiver.IsKeyDown(irr::KEY_KEY_A))
spaceshipRot.Z += spaceship_ROT_SPEED * frameDeltaTime;
else if (receiver.IsKeyDown(irr::KEY_KEY_D))
spaceshipRot.Z -= spaceship_ROT_SPEED * frameDeltaTime;
// move forth
if (receiver.IsKeyDown(irr::KEY_KEY_W))
{
float rotX = cos(to_radians(spaceshipRot.Z));
float rotY = sin(to_radians(spaceshipRot.Z));
dummyPos.X += rotX * spaceship_SPEED * frameDeltaTime;
dummyPos.Y += rotY * spaceship_SPEED * frameDeltaTime;
}
// calculate frictions,positions,etc
spaceshipVelocity.X *= ff;
spaceshipVelocity.Y *= ff;
spaceshipVelocity.X += dummyPos.X;
spaceshipVelocity.Y += dummyPos.Y;
spaceshipPos.X += spaceshipVelocity.X;
spaceshipPos.Y += spaceshipVelocity.Y;
spaceship->setPosition(spaceshipPos);
spaceship->setRotation(spaceshipRot);
driver->beginScene(true,true,video::SColor(255,100,140,101));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
// drop
device->drop();
return 0;
}