Thanks, I'll see if I can get it to work. Be right back
Ok, after compiling it, it kind of works. My camera is looking from behind the character's head, and this looks like it's meant for an overhead view. Also, simply turning moves the character forward. I got to go to bed, so I'll leave my code so you can see if anything is wrong.
Code: Select all
#include <irrlicht.h>
#include <math.h>
#include <stdio.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
const int BACK=1;
const int LEFT=2;
const int FORWARD=3;
const int RIGHT=4;
class MyEventReceiver : public IEventReceiver
{
public:
// This is the one method that we have to implement
virtual bool OnEvent(const SEvent& event)
{
// Remember whether each key is down or up
if (event.EventType == EET_KEY_INPUT_EVENT)
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
// This is used to check whether a key is being held down
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:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
vector3df move(ISceneNode* node,int direction,float speed,bool move){
vector3df newPos;
if(direction==FORWARD){
vector3df facing( sin( ( node->getRotation().Y + 0.0f ) * PI/180.0f ), -tan( (node->getRotation().X + 0.0f ) * PI/180.0f ), cos( ( node->getRotation().Y + 0.0f ) * PI/180.0f ) );
facing.normalize();
newPos = (facing*speed) + node->getPosition();
}
if(direction==BACK){
vector3df facing( sin( ( node->getRotation().Y + 180.0f ) * PI/180.0f ), 0, cos( (node->getRotation().Y + 180.0f ) * PI/180.0f ) );
facing.normalize();
newPos = (facing*speed) + node->getPosition();
}
if(direction==RIGHT){
vector3df facing( sin( (node->getRotation().Y + 90.0f ) * PI/180.0f ),0, cos( (node->getRotation().Y + 90.0f ) * PI/180.0f ) );
facing.normalize();
newPos = (facing*speed) + node->getPosition();
}
if(direction==LEFT){
vector3df facing( sin( ( node->getRotation().Y - 90.0f ) * PI/180.0f ), 0, cos( (node->getRotation().Y - 90.0f ) * PI/180.0f ) );
facing.normalize();
newPos = (facing*speed) + node->getPosition();
}
if(move) node->setPosition(newPos);
return newPos;
}
int main(int argc, char** argv)
{
MyEventReceiver receiver;
//Create the main device for the game
IrrlichtDevice *device =
createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 16,
false, false, false, &receiver);
//Set the window caption
device->setWindowCaption(L"Psychic Demo");
//Create all of the environments for the game
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
//Create the head
ISceneNode* head = smgr->addSphereSceneNode();
//Create the body
ISceneNode* body = smgr->addSphereSceneNode();
//Create one hand
ISceneNode* hand1 = smgr->addSphereSceneNode();
//Create the other hand
ISceneNode* hand2 = smgr->addSphereSceneNode();
//Create one foot
ISceneNode* foot1 = smgr->addSphereSceneNode();
//Create the other foot
ISceneNode* foot2 = smgr->addSphereSceneNode();
//Test cube
ISceneNode* cube = smgr->addCubeSceneNode();
cube->setPosition(vector3df(30, 0, 0));
//Set stuff for head
if (head)
{
head->setMaterialFlag(EMF_LIGHTING, true);
head->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
head->setScale(vector3df(0.8, 0.8, 0.8));
head->setPosition(vector3df(0,0,0));
}
//Set stuff for the body
if (body)
{
body->setParent(head);
body->setMaterialFlag(EMF_LIGHTING, true);
body->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
body->setScale(vector3df(1, 1.8, 1));
body->setPosition(head->getPosition() + vector3df(0, -15, 0));
}
//Stuff for hand1
if (hand1)
{
hand1->setParent(head);
hand1->setMaterialFlag(EMF_LIGHTING, true);
hand1->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
hand1->setScale(vector3df(0.5, 0.5, 0.5));
hand1->setPosition(head->getPosition() + vector3df(0, -16, -8));
}
//Stuff for hand2
if (hand2)
{
hand2->setParent(head);
hand2->setMaterialFlag(EMF_LIGHTING, true);
hand2->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
hand2->setScale(vector3df(0.5, 0.5, 0.5));
hand2->setPosition(head->getPosition() + vector3df(0, -16, 8));
}
//Stuff for foot1
if (foot1)
{
foot1->setParent(head);
foot1->setMaterialFlag(EMF_LIGHTING, true);
foot1->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
foot1->setScale(vector3df(0.5, 0.5, 0.5));
foot1->setPosition(head->getPosition() + vector3df(0, -27, 6));
}
//Stuff for foot2
if (foot2)
{
foot2->setParent(head);
foot2->setMaterialFlag(EMF_LIGHTING, true);
foot2->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
foot2->setScale(vector3df(0.5, 0.5, 0.5));
foot2->setPosition(head->getPosition() + vector3df(0, -27, -6));
}
//Create the camera
ICameraSceneNode* camera = smgr->addCameraSceneNode(0, vector3df(-50, 0, 0), vector3df(head->getPosition()));
camera->setParent(head);
camera->setTarget(head->getPosition());
//Hide or show the mouse cursor
device->getCursorControl()->setVisible(true);
while(device->run())
{
//Detect mouse key presses
if (receiver.IsKeyDown(KEY_KEY_W))
{
head->setPosition(move(head, FORWARD ,0.5, true));
}
if (receiver.IsKeyDown(KEY_KEY_S))
{
head->setPosition(move(head, BACK, 0.5, true));
}
if (receiver.IsKeyDown(KEY_KEY_A))
{
head->setRotation(move(head, LEFT, 0.5, true));
}
if (receiver.IsKeyDown(KEY_KEY_D))
{
head->setRotation(move(head, RIGHT, 0.5, true));
}
camera->setTarget(head->getPosition());
driver->beginScene(true, true, SColor(0,200,200,200));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
device->drop();
return 0;
}