[b]Hey its my first 3rd Person Camera View[/b]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
ravenmacavity

[b]Hey its my first 3rd Person Camera View[/b]

Post by ravenmacavity »

:D Hey guys check out I have a coded a simple Third Person Camera view.
Although its not finished yet but I m still learning and its really nice to have such a great engine for learning game developing Irrlicht rocks.

Note: just change the model and its skin location as per yor system. Its desigened for Faerie.md2 and sydney.md2 for others u may have to adjust their position also I am working with jumps and collision stuffs.

use w (or top arrow key), s(or down arrow key)
use mouse for character control
and use scroll or left n right arrow keys for Zoom in and Zoom out.

Please give it a try n do tell me.
(Dont bother about the code I was just concerned about its working ;) )

/////////////// just cut and paste ///////////////////////////
# include<irrlicht.h>
# include<iostream>
# include<math.h>


using namespace irr;
using namespace scene;
using namespace video;
using namespace core;
using namespace gui;

IAnimatedMeshSceneNode *m1 = 0;
ICameraSceneNode *camera = 0;
IrrlichtDevice *device =0;
ISceneManager *smgr=0;
IGUIEnvironment *guienv=0;
ISceneNodeAnimator* anim=0;
ITriangleSelector* selector = 0;

bool key[KEY_KEY_CODES_COUNT];
bool curMoved = false;
bool slow_motion = false;
IGUIInOutFader* fader = 0;
EMD2_ANIMATION_TYPE state = EMAT_STAND;
//ICursorControl cursor=0;
int speed = 4;


#define model "../media/faerie.md2"
#define modelskin "../media/faerie5.bmp"


//#define speed 4
#define ResX 800
#define ResY 600
#define FullScreen false
#define subtract 60
#define offset 100
#define ani 40

float mydiff;
float mydiffer;
// Event handler
class MyEventHandler : public IEventReceiver
{ public: vector3df pos;
public: vector3df campos;
public: vector3df rot;
public: position2d<s32> old;
public: position2d<s32> curpos;
public: int forceX;
public: int forceY;
public: bool setTurnX;
public: bool setTurnY;
public: float diff;
public: float differ;
public: int MEX;
public: int MEY;
public: bool ret;
public: float ZOOM;
private: float Ratio;

public: float keytime;
public: float rkeytime;
public: int value;
public: bool running;
public: int sign;
private: int frame;
//private: ISceneNodeAnimator* anim ;

public: MyEventHandler(){
forceX = 0;
forceY = 0;
// pos = vector3df(100,-55,0);
//campos = vector3df(100,0,0);
MEX = ResX - subtract;
MEY = ResY - subtract;
ZOOM = 2000;
Ratio = 4.0f/3.0f;
for(int i=0;i<KEY_KEY_CODES_COUNT;i++)
key=false;
}
public:
virtual bool OnEvent(SEvent event){
//ret =false;
//if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
if (event.EventType == EET_MOUSE_INPUT_EVENT)
{
if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
{
// if(state != EMAT_ATTACK)
// {
state = EMAT_WAVE;
m1->setMD2Animation(state);
slow_motion =false;
// }
//ret = true;
}
if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
{ state = EMAT_FLIP;
m1->setMD2Animation(state);
slow_motion=true;
fader = guienv->addInOutFader();
fader->setColor(SColor(100,255,250,250));
fader->fadeIn(2000);

}
if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
{ float dir;
dir = -(event.MouseInput.Wheel);
ZOOM = ZOOM + dir * 0.01f;
camera->setFOV(ZOOM);
}
if(event.MouseInput.Event == EMIE_MOUSE_MOVED)
{
curMoved = true;
}

ret = false;
}



if (event.EventType == EET_KEY_INPUT_EVENT )
{ // if(key[event.KeyInput.Key] != event.KeyInput.PressedDown)
{ key[event.KeyInput.Key] = event.KeyInput.PressedDown;
return true;
}
// return false;
}

}


public: void setXPos(int forceX,int forceY,bool takeTurnX,bool takeTurnY,position2d <s32> cursor){
int Xpos,Ypos;
if((takeTurnX == true && forceX == 1) )
{ Xpos = offset;
Ypos = cursor.Y;
setTurnX = false;
old.X = offset;

}
else if(takeTurnX)
{ Xpos = ResX - offset;
Ypos = cursor.Y;
setTurnX = false;

old.X = Xpos;
}

if(takeTurnY == true && forceY == 1)
{ Xpos = cursor.X;
Ypos = offset;
setTurnY = false;
old.Y = Ypos;
}
else if(takeTurnY)
{ Xpos = cursor.X;
Ypos = ResY - offset;
setTurnY = false;
old.Y = Ypos;
}

if(takeTurnX == true || takeTurnY == true)
device->getCursorControl()->setPosition(Xpos,Ypos);

}
};

// Main
int main() {
MyEventHandler receiver;

//receiver.SFrame(320);
device = createDevice(EDT_DIRECTX9,dimension2d<s32>(ResX,ResY),
16,FullScreen,false,true,&receiver);
IVideoDriver *driver = device->getVideoDriver();
smgr = device->getSceneManager();
guienv = device->getGUIEnvironment();
fader = guienv->addInOutFader();
fader->setColor(SColor(0,150,100,120));



IAnimatedMesh *mesh=smgr->getMesh(model);
m1=smgr->addAnimatedMeshSceneNode(mesh);

m1->setPosition(vector3df(-91,142,-677));
m1->setMaterialFlag(EMF_LIGHTING,true);
m1->setMaterialTexture(0,driver->getTexture(modelskin));
m1->setLoopMode(true);
m1->setAnimationSpeed(500);

camera = smgr->addCameraSceneNode(m1,vector3df(-60,150,0),vector3df(-91,142,-677));

camera->setFarValue(1100);

ILightSceneNode *light =
smgr->addLightSceneNode(0,vector3df(0,100,50),SColor(0,255,120,120),5000.0f);

ISceneNode *node =0;
ISceneNode *bill = 0;
// MAP
bill = smgr->addBillboardSceneNode(node, core::dimension2d<f32>(50, 50));
bill->setMaterialFlag(video::EMF_LIGHTING, false);
bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
bill->setMaterialTexture(0, driver->getTexture("../media/particlewhite.bmp"));
bill->setPosition(light->getPosition());






device->getFileSystem()->addZipFileArchive("../media/map-20kdm2.pk3");
IAnimatedMesh *map = smgr->getMesh("20kdm2.bsp");
node = smgr->addAnimatedMeshSceneNode(map);
node->setPosition(core::vector3df(-1300,-144,-1249));
node->setMaterialFlag(EMF_LIGHTING,false);
//node->getMaterial(0).EmissiveColor.set(0,100,0,0);

selector = smgr->createOctTreeTriangleSelector(
map->getMesh(0), node, 128);
node->setTriangleSelector(selector);
selector->drop();


// *********** FOR COLLISION
/* anim = smgr->createCollisionResponseAnimator(
selector, camera, core::vector3df(20,20,20),
core::vector3df(0,0,0),
core::vector3df(0,15,0));
//camera->addAnimator(anim);
//anim->drop();
*/

anim = smgr->createCollisionResponseAnimator(
selector, m1, core::vector3df(15,20,15),
core::vector3df(0,-0.3,0),
core::vector3df(0,10,0));
m1->addAnimator(anim);
anim->drop();

// *************************************


m1->setDebugDataVisible(false);
//guienv->addStaticText(L"differeenceesssssss",rect<int>(10,10,200,22), true);
vector3df my,cam,currentpos;
vector3df forward;
float temp;
int value = 0;
device->getCursorControl()->setVisible(false);
int lastFPS = 0;
EMD2_ANIMATION_TYPE laststate = EMAT_STAND;

ISceneNode* selectedSceneNode = 0;
//scene::ISceneNode* lastSelectedSceneNode = 0;
bool tFloor = true;



while(device->run())
{
wchar_t tmp[1024];
stringw str=L"Frame:";
//str += m1->getFrameNr();
int fps = driver->getFPS();
str += fps;
if (lastFPS != fps)
{
//core::stringw str = L"Collision detection example - Irrlicht Engine [";
//str += driver->getName();
//str += "] FPS:";

//device->setWindowCaption(str.c_str());
lastFPS = fps;
}

vector3df myY = m1->getPosition();
vector3df camY = camera->getPosition();
str += " myY:";
str +=(stringw) myY.Y;
str += "";
str+= (stringw)myY.X;
str += "camZ";
str += (stringw) myY.Z;
device->setWindowCaption(str.c_str());

if(key[KEY_UP] || key[KEY_DOWN] || key[KEY_KEY_W] || key[KEY_KEY_S])
{ value = (key[KEY_UP] || key[KEY_KEY_W]) == true ? 1: -1 ;
//key[KEY_UP] = false;
if(state != EMAT_RUN && tFloor == true)
// { //m1->setFrameLoop(560, 1400);

state=EMAT_RUN;
//m1->setMD2Animation(state);
// }

}
if(!key[KEY_UP] && !key[KEY_DOWN] && !key[KEY_KEY_W] && !key[KEY_KEY_S] && tFloor)
{state = EMAT_STAND;
value = 0;
//tFloor = true;
}

if(key[KEY_LEFT] || key[KEY_RIGHT])
{ int dir;
dir = key[KEY_LEFT]== true ? -1: 1;
// m1->setRotation(rot);
receiver.ZOOM = receiver.ZOOM + dir * 0.01f;
//Ratio = Ratio + dir*0.01f;
// camera->setAspectRatio(Ratio);
camera->setFOV(receiver.ZOOM);
}

if(key[KEY_SPACE] && tFloor == true)
{ if(state != EMAT_JUMP)
{
state = EMAT_JUMP;
m1->setLoopMode(false);

tFloor = false;
vector3df height = m1->getPosition();
height.Y += 80; //30;
m1->setPosition(height);
//camera = smgr->addCameraSceneNode(0,camera->getPosition(),camera->getTarget());
camera->setParent(0);
height.Y += 30;
camera->setTarget(height);


}

}

if(curMoved)
{ receiver.curpos = device->getCursorControl()->getPosition();
mydiff = receiver.curpos.X - receiver.old.X ;
mydiffer = receiver.curpos.Y - receiver.old.Y;
receiver.diff = mydiff;
receiver.differ = mydiffer;
if(mydiff > 0)
{ receiver.forceX = 1;
//printf("X: %d ",&eventX);
if(receiver.curpos.X >= receiver.MEX)
receiver.setTurnX = true;

}
else if(mydiff < 0)
{receiver.forceX = -1;
//printf("negative force ");
if(receiver.curpos.X <= subtract)
receiver.setTurnX = true;
}
if(mydiffer > 0)
{ receiver.forceY = 1;
if(receiver.curpos.Y >= receiver.MEY)
receiver.setTurnY = true;
}
else if(mydiffer < 0)
{ receiver.forceY = -1;
if(receiver.curpos.Y <= subtract)
receiver.setTurnY = true;
}
receiver.old = receiver.curpos;





}

if(receiver.forceX != 0 || receiver.forceY != 0)
{ //rot.Y += diff * 0.1 * speed;//4.0f;

my = m1->getRotation();
my.Y += mydiff * 0.05 * speed;//4.0f;
m1->setRotation(my);

cam = camera->getPosition();

cam.Y += mydiffer * 0.2;//3.0f;
camera->setPosition(cam);

receiver.setXPos(receiver.forceX,receiver.forceY,receiver.setTurnX,receiver.setTurnY,receiver.curpos);
receiver.forceX = 0;
receiver.forceY = 0;

}

if(state == EMAT_JUMP)
{

core::line3d<f32> line;
vector3df test = m1->getPosition();
line.start = test;
test.Y = test.Y - 35; // ground reached at 40
line.end = test;

core::triangle3df tri;
core::vector3df intersection;

if (smgr->getSceneCollisionManager()->getCollisionPoint(
line, selector, intersection, tri))
{ tFloor = true;
m1->setLoopMode(true);
bill->setPosition(intersection);
// camera = smgr->addCameraSceneNode(m1,camera->getPosition(),camera->getTarget());
}

temp = (m1->getRotation()).Y*GRAD_PI2 + PI/2;
bool collide = false;
forward = vector3df(sin(temp),0,cos(temp));
currentpos = m1->getPosition() + (forward)*(value)*5;
m1->setPosition(currentpos);
currentpos.Y += 30;
camera->setTarget(currentpos);

}
// if(tFloor == false)
{
vector3df doit = m1->getPosition();
doit.Y += 30;

camera->setTarget(doit);
//
if(camera->getParent() == 0)
camera->setParent(m1);
// camera = smgr->addCameraSceneNode(m1,camera->getPosition(),camera->getTarget());

//camera = smgr->addCameraSceneNode(m1,camera->getPosition(),camera->getTarget());
}

if(state == EMAT_RUN )
{ temp = (m1->getRotation()).Y*GRAD_PI2 + PI/2;
//printf("Angle is : %f",&temp);
//forward=vector3df( sin( temp.Y*PI/180.0f + PI/2), 0, cos( temp.Y*PI/180.0f + PI/2) );
//vector3df height = m1->getPosition();
//height.Y = height.Y + 10;
bool collide = false;
forward = vector3df(sin(temp),0,cos(temp));
currentpos = m1->getPosition() + (forward)*(value)*5;
//****************************
m1->setPosition(currentpos);

currentpos.Y += 30;
// if(collide)
// cam.Y = currentpos.Y; // 60 for Ninja.ms3d

camera->setTarget(currentpos);

// camera->setPosition(cam);
}
/* if(slow_motion)
{ m1->setAnimationSpeed(30);

}
else
m1->setAnimationSpeed(100);
*/
// Animation goes here :
if(laststate != state)
{ m1->setMD2Animation(state);
laststate = state;

}

//camera->setPosition(cam);
driver->beginScene(true,true,0);
smgr->drawAll();
driver->endScene();

//device->setWindowCaption(str.c_str());
}
device->drop();
return 0;

}

///////////////////////////////////////////////////////////////
puh
Posts: 356
Joined: Tue Aug 26, 2003 3:53 pm

Post by puh »

:D Hey guys try using [ code ] [ / code ] tags when posting any code fragments!
It will let people easily check the representation of the code by themselvs.
ravenmacavity

ok then here it goes again

Post by ravenmacavity »

puh wrote::D Hey guys try using [ code ] [ / code ] tags when posting any code fragments!
It will let people easily check the representation of the code by themselvs.
:oops: ok then here it is, but please compile and run this and do tell me

Code: Select all

# include<irrlicht.h>
# include<iostream>
# include<math.h>

//# pragma comment(lib,"irrlicht.lib")

using namespace irr;
using namespace scene;
using namespace video;
using namespace core;
using namespace gui;    
//using namespace gui;
//ISceneNode *dummy=0;
IAnimatedMeshSceneNode *m1 = 0;
ICameraSceneNode *camera = 0;
IrrlichtDevice *device =0;
ISceneManager *smgr=0;
IGUIEnvironment *guienv=0;
ISceneNodeAnimator* anim=0;
ITriangleSelector* selector = 0;

bool key[KEY_KEY_CODES_COUNT];
bool curMoved = false;
bool slow_motion = false;
                                         IGUIInOutFader* fader = 0;   
EMD2_ANIMATION_TYPE state = EMAT_STAND;
//ICursorControl cursor=0;
int speed = 4;


#define model "../../media/faerie.md2"
#define modelskin "../../media/faerie5.bmp"


//#define speed 4
#define ResX 800
#define ResY 600
#define FullScreen false
#define subtract 60
#define offset 100
#define ani 40      

float mydiff;
float mydiffer;
// Event handler
class MyEventHandler : public IEventReceiver
{  public: vector3df pos;
   public: vector3df campos;
   public: vector3df rot;   
   public: position2d<s32> old;
   public: position2d<s32> curpos;
   public: int forceX;
   public: int forceY;
   public: bool setTurnX;
   public: bool setTurnY;
   public: float diff;
   public: float differ;
   public: int MEX;
   public: int MEY;
   public: bool ret;
   public: float ZOOM;
   private: float Ratio;
   
   public: float keytime;
   public:  float rkeytime;
   public:  int value;
   public: bool running;
   public: int sign;  
   private: int frame;
   //private: ISceneNodeAnimator* anim ;
   
  public: MyEventHandler(){
                forceX = 0;
                forceY = 0;
               // pos  = vector3df(100,-55,0);
                //campos = vector3df(100,0,0);
                MEX = ResX - subtract;
                MEY = ResY - subtract;
                ZOOM = 2000;
                Ratio = 4.0f/3.0f;
                for(int i=0;i<KEY_KEY_CODES_COUNT;i++)
                   key[i]=false;
               } 
   public:
          virtual bool OnEvent(SEvent event){
             //ret =false;  
            //if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
            if (event.EventType == EET_MOUSE_INPUT_EVENT) 
            {                   
              if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
             {   
                 // if(state != EMAT_ATTACK)
                   // { 
                      state = EMAT_WAVE;
                      m1->setMD2Animation(state);
                      slow_motion =false; 
                     // }
                   //ret = true;
             }
              if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
                {  state = EMAT_FLIP;
                   m1->setMD2Animation(state);
                     slow_motion=true;
                     fader = guienv->addInOutFader();
                      fader->setColor(SColor(100,255,250,250));
                      fader->fadeIn(2000);
                   
                }   
              if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
              {  float dir;
                 dir = -(event.MouseInput.Wheel);
                  ZOOM = ZOOM + dir * 0.01f; 
                 camera->setFOV(ZOOM);
              }          
             if(event.MouseInput.Event == EMIE_MOUSE_MOVED)
                 {
                  curMoved = true;
                 }
                
               ret = false;                
            }
            
            
            
            if (event.EventType == EET_KEY_INPUT_EVENT )
             {  // if(key[event.KeyInput.Key] != event.KeyInput.PressedDown)
                    { key[event.KeyInput.Key] = event.KeyInput.PressedDown;
                      return true; 
                      }
                 // return false;      
             }                  
               
           }  
           
                
   public: void setXPos(int forceX,int forceY,bool takeTurnX,bool takeTurnY,position2d <s32> cursor){
      int Xpos,Ypos;
      if((takeTurnX == true && forceX == 1) )
         { Xpos = offset;
           Ypos = cursor.Y;
           setTurnX = false;
           old.X = offset;
           //device->getCursorControl()->setPosition(Xpos,cursor.Y);         
           
         } 
      else if(takeTurnX)     
         { Xpos = ResX - offset;
           Ypos = cursor.Y;
           setTurnX = false;
           
           old.X = Xpos;
          // device->getCursorControl()->setPosition(Xpos,cursor.Y);
         }                    
      
      if(takeTurnY == true && forceY == 1)
         { Xpos = cursor.X;
           Ypos = offset;
           setTurnY = false;
           old.Y = Ypos;
           //device->getCursorControl()->setPosition(cursor.X,Ypos);         
         } 
      else if(takeTurnY)     
         { Xpos = cursor.X;
           Ypos = ResY - offset;
           setTurnY = false;
           old.Y = Ypos;
           //device->getCursorControl()->setPosition(cursor.X,Ypos);
         }                    
      
     if(takeTurnX == true || takeTurnY == true) 
         device->getCursorControl()->setPosition(Xpos,Ypos);
                       
   }               
};

// Main 
int main() {
   MyEventHandler receiver;
   
   //receiver.SFrame(320);
   device = createDevice(EDT_DIRECTX9,dimension2d<s32>(ResX,ResY),
                                         16,FullScreen,false,true,&receiver);
   IVideoDriver *driver = device->getVideoDriver();
   smgr = device->getSceneManager();
   guienv = device->getGUIEnvironment();
   fader = guienv->addInOutFader();
                      fader->setColor(SColor(0,150,100,120));
                    
   
   
   // addAnimatedMeshSceneNode(smgr->getMesh("../media/zombie.ms3d"));
   IAnimatedMesh *mesh=smgr->getMesh(model);
   m1=smgr->addAnimatedMeshSceneNode(mesh);
   
   m1->setPosition(vector3df(-91,142,-677));
   //m1->setRotation(vector3df(0,100,0));
   //m1->setScale(vector3df(6,6,6));
   m1->setMaterialFlag(EMF_LIGHTING,true);
   m1->setMaterialTexture(0,driver->getTexture(modelskin));
   m1->setLoopMode(true);
   m1->setAnimationSpeed(500);
  
   camera = smgr->addCameraSceneNode(m1,vector3df(-60,150,0),vector3df(-91,142,-677));
   
     camera->setFarValue(1100);
   
   ILightSceneNode *light = 
   smgr->addLightSceneNode(0,vector3df(0,100,50),SColor(0,255,120,120),5000.0f);
   
    ISceneNode *node =0;
    ISceneNode *bill = 0;
   // MAP
    bill = smgr->addBillboardSceneNode(node, core::dimension2d<f32>(50, 50));
	bill->setMaterialFlag(video::EMF_LIGHTING, false);
	bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
	bill->setMaterialTexture(0,	driver->getTexture("../media/particlewhite.bmp")); 
    bill->setPosition(light->getPosition());


   
   
   
   
    device->getFileSystem()->addZipFileArchive("../media/map-20kdm2.pk3");
    IAnimatedMesh *map = smgr->getMesh("20kdm2.bsp");
    node = smgr->addAnimatedMeshSceneNode(map);
     node->setPosition(core::vector3df(-1300,-144,-1249));
    node->setMaterialFlag(EMF_LIGHTING,false);
    //node->getMaterial(0).EmissiveColor.set(0,100,0,0);
    
    selector = smgr->createOctTreeTriangleSelector(
            map->getMesh(0), node, 128);
		node->setTriangleSelector(selector);
		selector->drop();
   
   
   // ***********   FOR COLLISION 
   /* anim =    smgr->createCollisionResponseAnimator(
		selector, camera, core::vector3df(20,20,20),
		core::vector3df(0,0,0),
		core::vector3df(0,15,0));
	//camera->addAnimator(anim);
	//anim->drop(); 
     */
   
    anim = smgr->createCollisionResponseAnimator(
		selector, m1, core::vector3df(15,20,15),
		core::vector3df(0,-0.3,0),
		core::vector3df(0,10,0));
	m1->addAnimator(anim);
	anim->drop(); 
    
   // *************************************      
    
       
   m1->setDebugDataVisible(true); 
   //guienv->addStaticText(L"differeenceesssssss",rect<int>(10,10,200,22), true);
    vector3df my,cam,currentpos;
    vector3df forward;
    float temp;
    int value = 0;
    device->getCursorControl()->setVisible(false);
    int lastFPS = 0;
    EMD2_ANIMATION_TYPE laststate = EMAT_STAND; 
    
    ISceneNode* selectedSceneNode = 0;
	//scene::ISceneNode* lastSelectedSceneNode = 0;
    bool tFloor = true;
    
   //video::SMaterial material;
	//material.Texture1 = 0;//driver->getTexture("../media/faerie5.bmp");
	//material.Lighting = false;
    
   
   while(device->run())
    {                                                                                                
      wchar_t tmp[1024];
      stringw str=L"Frame:";
      //str += m1->getFrameNr();
      int fps = driver->getFPS();
      str += fps;
		if (lastFPS != fps)
		{
		  //core::stringw str = L"Collision detection example - Irrlicht Engine [";
          //str += driver->getName();
          //str += "] FPS:";
          
          //device->setWindowCaption(str.c_str());
          lastFPS = fps;
         }
        
      vector3df myY = m1->getPosition(); 
      vector3df camY = camera->getPosition(); 
      str += "  myY:";
      str +=(stringw) myY.Y;
    //  str += "  camY:";
     // str +=  (stringw)myY.Y;
      str += "";
      str+=   (stringw)myY.X;
      str += "camZ";
      str += (stringw) myY.Z;
      //str += "  Z:";
     // str += (stringw)myXYZ.Z;
      //str += "    ForceY:";
      //str += receiver.forceY;
       device->setWindowCaption(str.c_str());
      
      if(key[KEY_UP] || key[KEY_DOWN] || key[KEY_KEY_W] || key[KEY_KEY_S])
        { value = (key[KEY_UP] || key[KEY_KEY_W]) == true ? 1: -1 ;
          //key[KEY_UP] = false;
          if(state != EMAT_RUN && tFloor == true) 
           //   { //m1->setFrameLoop(560, 1400);
                         
                        state=EMAT_RUN;
                        //m1->setMD2Animation(state);
            //  }
            
        }
      if(!key[KEY_UP] && !key[KEY_DOWN] && !key[KEY_KEY_W] && !key[KEY_KEY_S] && tFloor)
         {state = EMAT_STAND;
           value = 0;
           //tFloor = true;
         }               
    
      if(key[KEY_LEFT] || key[KEY_RIGHT])
        { int dir;
          dir = key[KEY_LEFT]== true ? -1: 1;
                    // m1->setRotation(rot);
          receiver.ZOOM = receiver.ZOOM +   dir * 0.01f; 
                    //Ratio = Ratio + dir*0.01f;
                   // camera->setAspectRatio(Ratio);
          camera->setFOV(receiver.ZOOM);
        }
     
      if(key[KEY_SPACE] && tFloor == true)
        { if(state != EMAT_JUMP)
            {  
              state = EMAT_JUMP;
              m1->setLoopMode(false);
              
              tFloor = false;
              vector3df  height = m1->getPosition();
              height.Y +=  80; //30;
               m1->setPosition(height);
               //camera = smgr->addCameraSceneNode(0,camera->getPosition(),camera->getTarget());
              camera->setParent(0);
              height.Y += 30;
              camera->setTarget(height);
             
             
              //camera->setTarget(m1->getPosition());
              //height.Y += 30;
              
              //tFloor = true;
            }
                               
        }   
      
               
                                                                        
      if(curMoved)
       { receiver.curpos = device->getCursorControl()->getPosition();
                  mydiff = receiver.curpos.X - receiver.old.X ;
                  mydiffer = receiver.curpos.Y - receiver.old.Y;
                  receiver.diff = mydiff;
                  receiver.differ = mydiffer;
                  if(mydiff > 0)
                    { receiver.forceX = 1;
                      //printf("X:  %d  ",&eventX);
                      if(receiver.curpos.X >= receiver.MEX)
                        receiver.setTurnX = true;
                      
                    } 
                  else if(mydiff < 0) 
                     {receiver.forceX = -1;
                      //printf("negative force     "); 
                       if(receiver.curpos.X <= subtract)
                         receiver.setTurnX = true;
                     }
                 if(mydiffer > 0)
                    { receiver.forceY = 1;
                      if(receiver.curpos.Y >= receiver.MEY)
                          receiver.setTurnY = true;
                    }
                  else if(mydiffer < 0) 
                    { receiver.forceY = -1;
                      if(receiver.curpos.Y <= subtract)
                        receiver.setTurnY = true;
                    }
                 receiver.old = receiver.curpos;
                  
      
      
      
      
      }
      
      if(receiver.forceX != 0 || receiver.forceY != 0)
       { //rot.Y += diff * 0.1 * speed;//4.0f; 
         
         my = m1->getRotation();
         my.Y += mydiff * 0.05 * speed;//4.0f; 
         m1->setRotation(my);
          
         cam = camera->getPosition();
         
            cam.Y +=  mydiffer * 0.2;//3.0f;
                     camera->setPosition(cam); 
         
         receiver.setXPos(receiver.forceX,receiver.forceY,receiver.setTurnX,receiver.setTurnY,receiver.curpos);
         receiver.forceX = 0;
         receiver.forceY = 0;
         
       }
     
     if(state == EMAT_JUMP)
      {  
        
         core::line3d<f32> line;
	     vector3df test = m1->getPosition(); 	
            line.start = test;
	      	test.Y = test.Y - 35;        // ground reached at 40
	        line.end = test;         
          
	    core::triangle3df tri;
    	core::vector3df intersection;
    	
		if (smgr->getSceneCollisionManager()->getCollisionPoint(
			line, selector, intersection, tri))
		{   tFloor = true;
		    m1->setLoopMode(true);
            bill->setPosition(intersection); 
           // camera = smgr->addCameraSceneNode(m1,camera->getPosition(),camera->getTarget());
        } 
        
         temp = (m1->getRotation()).Y*GRAD_PI2 + PI/2;
         bool collide = false;
         forward = vector3df(sin(temp),0,cos(temp));
         currentpos = m1->getPosition() + (forward)*(value)*5; 
         m1->setPosition(currentpos);
         currentpos.Y += 30;    
         camera->setTarget(currentpos);  
               
      } 
     // if(tFloor == false)
       { 
        vector3df doit = m1->getPosition();
        doit.Y += 30;
        
        camera->setTarget(doit); 
        //
        if(camera->getParent() == 0)
           camera->setParent(m1);
       //   camera = smgr->addCameraSceneNode(m1,camera->getPosition(),camera->getTarget());        
        
        //camera = smgr->addCameraSceneNode(m1,camera->getPosition(),camera->getTarget());        
       }            
     
     if(state == EMAT_RUN )
       { temp = (m1->getRotation()).Y*GRAD_PI2 + PI/2;
         bool collide = false;
         forward = vector3df(sin(temp),0,cos(temp));
         currentpos = m1->getPosition() + (forward)*(value)*5; 
         //****************************  
         m1->setPosition(currentpos);
         
 currentpos.Y += 30;    
        
         camera->setTarget(currentpos);
         
         }
      // Animation goes here :
         if(laststate != state)
          {  m1->setMD2Animation(state);
             laststate = state;
                      
          }                       
       
       //camera->setPosition(cam);
      driver->beginScene(true,true,0);
      smgr->drawAll();
      driver->endScene(); 
       
      //device->setWindowCaption(str.c_str());
    }
   device->drop();
   return 0;   
                                              
 }


Megamanx10102

What verson of irrlicht is this for?

Post by Megamanx10102 »

What verson of irrlicht is this for and what did you use to compile this? (visual C++ 6?)
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

I tried compiling this with 0.10 and msvc++ 6...

niether the first nor the second would compile.

look how much wasted forum space there is now over something that never should have been posted without testing if it worked first.

excuse me while I shoot myself.
bearSoft
Posts: 165
Joined: Fri Apr 01, 2005 9:55 pm
Location: Denmark

Post by bearSoft »

well it works very well for me
very nice i have to say, realy good work
Thank you for sharing this with the community

For those that did not get it to work:
I changed the driver to oGl
eg
device = createDevice(video::EDT_OPENGL,
dimension2d<s32>(ResX,ResY),
16,FullScreen,false,true,&receiver);


Then i made sure that all references to the media folder actually is for my system.

this is on irr 0.10 and dev
Regards.
Tech: win98se| 320mb ram| abitbe6| 433mhzceleron| atiRadeon7000.64mb| soundblaster125| dx9.0b | devCPP | IRR 0.12.0 |
Megamanx10102

do you mean devc++?

Post by Megamanx10102 »

Do you mean dev-c++?

Can you give me a STEP BY STEP on how you compiled it?

I've never used that program before.

--------------------------------------------------------------------

"Welcome to Noobsville," - Population, Just me.

--------------------------------------------------------------------
bearSoft
Posts: 165
Joined: Fri Apr 01, 2005 9:55 pm
Location: Denmark

Post by bearSoft »

Do you mean dev-c++?
yes
Can you give me a STEP BY STEP on how you compiled it?
there is nothing speciel to it
in irr faq there is a devcpp tutorial but if u use (own) msVisStudio it would be an unnessary overhead to get dev as well

ofcause u can get it to work on visual studio as well
The only reason i can think of that may be giving u trouble is as i already wrote -wrong path to resourses
-what errors do u get from ur compiler?
Regards.
Tech: win98se| 320mb ram| abitbe6| 433mhzceleron| atiRadeon7000.64mb| soundblaster125| dx9.0b | devCPP | IRR 0.12.0 |
the user
Posts: 6
Joined: Sun Mar 20, 2005 7:47 pm

Post by the user »

Hi,
nice Third Person Camera, but how can i control the runnig speed?
Krypt
Posts: 4
Joined: Fri Jul 08, 2005 10:04 pm

Post by Krypt »

im sure its a cool code but i get windows error when it compiles and starts up., debugger hangs also so im not sure what the problem is


debugger may have got this info before it crashed :

298 C:\Dev-Cpp\Untitled1.cpp [Warning] passing `irr::f32' for converting 1 of `irr::core::string<T>::string(int) [with T = wchar_t]'

302 C:\Dev-Cpp\Untitled1.cpp [Warning] passing `irr::f32' for converting 1 of `irr::core::string<T>::string(int) [with T = wchar_t]'

304 C:\Dev-Cpp\Untitled1.cpp [Warning] passing `irr::f32' for converting 1 of `irr::core::string<T>::string(int) [with T = wchar_t]'


-------------------

line 298 is :
str +=(stringw) myY.Y;

line 302 is :
str+= (stringw)myY.X;

line 394 is :
str += (stringw) myY.Z;
ravenmacavity

Nice to get yor feedbacks

Post by ravenmacavity »

:D Its really pleasing when someone like yor work.

Thanx BearSoft <for helping me indirectly 8) >
I was out of town returned today only.

For u Krypt let me tell u warnings are not real problems they only tell that some code amendments r needed else in most of the cases programe with some warnings always work.
In this case, as I already said that I was only concerned with 3rd person camera view and didnt give much concern to code style and efficiency.

The only problem that make the programe crash is the wrong path for model and the model skin.

As I have used faerie in my programe code so u have to just set its path correctly else the Irrlicht will crash before even showing somthing try out this and I m sure this will help. :lol: or
copy the faerie.md2 and faerie5.bmp files and put them in yor project folder and edit these lines:
#define model "../../media/faerie.md2"
#define modelskin "../../media/faerie5.bmp"

To:

#define model "faerie.md2"
#define modelskin "faerie5.bmp"

To control the speed just change the last "5" to any value just try out::

Code: Select all

if(state == EMAT_RUN )
       { temp = (m1->getRotation()).Y*GRAD_PI2 + PI/2;
         bool collide = false;
         forward = vector3df(sin(temp),0,cos(temp));
         currentpos = m1->getPosition() + (forward)*(value) * 5; //<----- "5"
Guys try these keys:
up arrow or w,
down arrow or s,
left & right arrow,
space bar
and yes use mouse to control the direction
Post Reply