Another Third Person Cam

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
vargero
Posts: 4
Joined: Mon Jun 16, 2008 12:06 pm

Another Third Person Cam

Post by vargero »

Well, I'm new to Irrlicht, and I'm developing a game with my friends. Well, I used some camera codes from here, mixing and stuff, and everything (about camera) is working well. The problem is, I want to make the camera turn as I move the mouse, so I did this code:

Code: Select all

#include <iostream>
#include "irrlicht.h"
#include <windows.h>

#define PI 3.14159265
#define PATH "D:/projetos/CuteKiller/"

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

int x=0,
    y=0,
    z=0,
    mp=0,
    sr=0,
    rtt=0,
    ree=0,
    veloc=0,
    velocl=1,
    v=10,
    zoom = 40;


bool mouseDownL;
bool mouseDownM;
bool mouseDownR;
f32 lastWheelMovement;
f32 LastMouseX;
position2d<f32> cursor;


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;
      }
      if (event.EventType == EET_MOUSE_INPUT_EVENT)
            {
            // left mouse button state check
                if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)   mouseDownL = true;
                if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)      mouseDownL = false;

            // middle mouse button state check
                //if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)   mouseDownM = true;
                //if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)      mouseDownM = false;

            // mouse wheel
                if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
                {
                    //while(EMIE_MOUSE_WHEEL){
                        lastWheelMovement = event.MouseInput.Wheel;

                    //}

                    /*zoom -= -event.MouseInput.Wheel * (zoom / 20) * 2;
                    if(zoom < 10) zoom = 10;
                    if(zoom > 300) zoom = 300;*/
                }

            // right mouse button state check
                if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
                {
                    /*mouseDownR = true;
                    cursorOld.X = event.MouseInput.X;
                    cursorOld.Y = event.MouseInput.Y;
                    cursorDelta.X = 0;
                    cursorDelta.Y = 0;
                    cameraOrbitOld = cameraOrbit;
                    cameraAngleOld = cameraAngle;*/
                }
            //endroit ou mettre la remise a zéro de la position
                if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)      mouseDownR = false;

            // mouse move check
                if(event.MouseInput.Event == EMIE_MOUSE_MOVED)
                {
                    //cursor.X = 0;



                    cursor.X = event.MouseInput.X;


            // add condition that right mouse button be down
                    if(mouseDownR == true)
                    {
                        /*cursor.X = event.MouseInput.X;
                        cursor.Y = event.MouseInput.Y;
                        cursorDelta.X = cursor.X - cursorOld.X;
                        cursorDelta.Y = cursor.Y - cursorOld.Y;
                        if(cursorDelta.Y > 100) cursorDelta.Y = 100;
                        if(cursorDelta.Y < -100) cursorDelta.Y = -100;
                        cameraOrbit = (int)(cameraOrbitOld + cursorDelta.X) % 360;
                        cameraAngle = (int)(cameraAngleOld + cursorDelta.Y) % 360;
                        if(cameraAngle > 88) cameraAngle = 88;
                        if(cameraAngle < -88) cameraAngle = -88;*/
                    }

                }
            return false;
            }
   return false;
   }
};

void mouseread(){

    }

int main()
{

   veloc=velocl;


   IrrlichtDevice *device =
      createDevice(EDT_BURNINGSVIDEO, dimension2d<s32>(800, 600), 6, false, true);

   MyEventReceiver receiver;
   device->setEventReceiver(&receiver);

   device->setWindowCaption(L"Green 3D");
   IVideoDriver* driver = device->getVideoDriver();
   ISceneManager* smgr = device->getSceneManager();
   IGUIEnvironment* guienv = device->getGUIEnvironment();

   IAnimatedMesh* mesh = smgr->getMesh(PATH"sydney.md2");
   IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
   if (node) {
      node->setMaterialFlag(EMF_LIGHTING, true);
      node->setMD2Animation ( scene::EMAT_STAND );
      node->setMaterialTexture( 0, driver->getTexture(PATH"sydney.bmp"));
   }

   if (node)
      node->setPosition(core::vector3df(0,-40,0));

   node->addShadowVolumeSceneNode(0,true,100.0f);
   smgr->setShadowColor(video::SColor(220,0,0,0));

   device->getFileSystem()->addZipFileArchive(PATH"map-20kdm2.pk3");

   scene::IAnimatedMesh* meshmap = smgr->getMesh(PATH"20kdm2.bsp");
   scene::ISceneNode* nodemap = 0;
   if (meshmap) nodemap = smgr->addOctTreeSceneNode(meshmap->getMesh(0));
   if (nodemap) nodemap->setPosition(core::vector3df(-1300,-144,-1249));

   scene::ITriangleSelector* selector = 0;

   if (nodemap)
   {
      nodemap->setPosition(core::vector3df(-1350,-130,-1400));
      selector = smgr->createOctTreeTriangleSelector(meshmap->getMesh(0), nodemap, 128);
      nodemap->setTriangleSelector(selector);
      selector->drop();
   }

   if (node)
   {
      scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
      selector, node, core::vector3df(10,25,10),
      core::vector3df(0,-2,0), // gravidade
      core::vector3df(0,0,0));
      node->addAnimator(anim);
      anim->drop();
      node->setLoopMode(true);
   }

   ICameraSceneNode* principalcam;
   principalcam = smgr->addCameraSceneNode(node,core::vector3df(-100,zoom,0),core::vector3df(0,0,0));

   ISceneNode * light = smgr->addLightSceneNode( 0, vector3df( 30, 140,0), SColorf(1.f, 1.f, 1.f,0.f), 600.f);
   light->setParent(node);

   while(device->run())
   {
      driver->beginScene(true, true, SColor(255,100,101,140));
      smgr->drawAll();
      guienv->drawAll();
      driver->endScene();
      rtt--;
    cout << cursor.X << "\n";

      if(mp!=0)
      {
         if(rtt<=1)
         {
            node->setMD2Animation ( scene::EMAT_STAND );
            mp=0;
         }
      }

      if(veloc>1)
         veloc--;

      if(cursor.X > 420)
      {
         if(veloc<2)
         {

            sr+=v;
            veloc=velocl/2;

         }
      }

      if(cursor.X < 380)
      {
         if(veloc<2)
         {
            sr-=v;
            veloc=velocl/2;
         }
      }

      if(keys[KEY_KEY_W])
      {
         if(veloc<2)
         {
            vector3df vr = node->getRotation();
            vector3df vp = node->getPosition();
            vp.X += v*cos(vr.Y*PI/180);
            vp.Z -= v*sin(vr.Y*PI/180);
            node->setPosition(vp);
            veloc=velocl;
         }

         rtt=3;
         if(mp!=1)
         {
            node->setMD2Animation ( scene::EMAT_RUN );
            mp=1;
         }
      }

      if(keys[KEY_KEY_S])
      {
         if(veloc<2)
         {
            vector3df vr = node->getRotation();
            vector3df vp = node->getPosition();
            vp.X -= cos(vr.Y*PI/180);
            vp.Z += sin(vr.Y*PI/180);
            node->setPosition(vp);
            veloc=velocl;
         }
         rtt=3;
         if(mp!=1)
         {
            node->setMD2Animation ( scene::EMAT_RUN );
            mp=1;
         }
      }

      if(keys[KEY_SPACE])
      {
         rtt=10;
         if(mp!=3)
         {
            node->setMD2Animation ( scene::EMAT_JUMP );
            mp=3;
         }
      }

    if(keys[KEY_BACK])
      {
      cout << "OI" << zoom << " " << lastWheelMovement << "\n";
      }

    if (EMIE_MOUSE_WHEEL){
        if(lastWheelMovement > 0){
          cout << "OI" << zoom << " " << EMIE_MOUSE_WHEEL << "\n";
          zoom-= 20;
          if (zoom <= 15) zoom = 15;
          principalcam = smgr->addCameraSceneNode(node,core::vector3df(-100,zoom,0),core::vector3df(0,0,0));
        lastWheelMovement = 0;

        }
        if(lastWheelMovement < 0){
          cout << "OI" << zoom << " " << EMIE_MOUSE_WHEEL << "\n";
          zoom+= 20;
          if (zoom >= 100) zoom = 100;
          principalcam = smgr->addCameraSceneNode(node,core::vector3df(-100,zoom,0),core::vector3df(0,0,0));
    lastWheelMovement = 0;

        }

    }



      node->setRotation(core::vector3df(0,sr,0));
      principalcam->setTarget(node->getPosition());
   }
   device->drop();
   return 0;
}

This way, it works, but not well, and I want to remove this annoying cursor. Thanks for atention! (And sorry if some really old post could help, but I shearched and found nothing)
wildrj
Posts: 301
Joined: Thu Mar 23, 2006 12:49 am
Location: Texas/ Cyberspace
Contact:

Post by wildrj »

lol tutorial 2 baby :P

device->getCursorControl()->setVisible(false); << i belive thats what your looking for.
http://wild.deathtouchstudios.com << My gamedev blog
<Programming is a way of life>
If at first you don't succeed press the delete key till you do :)
vargero
Posts: 4
Joined: Mon Jun 16, 2008 12:06 pm

Post by vargero »

Not only this to be correct :D
I want to make the camera turn acording to the mouse: when not moving the mouse, the camera don't move; when right, turn character right; when left ... << that's what i'm trying to do.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Re: Another Third Person Cam

Post by rogerborg »

It sounds like what you want to do is to create a target position for the camera that's based on its rotation and inclination. That's trig 101.

Code: Select all

vector3df cameraTarget = principalcam->getAbsolutePosition();
cameraTarget.X += 1000.f * sin(cameraOrbit * core::DEGTORAD) * cos(cameraAngle * core::DEGTORAD);
cameraTarget.Z += 1000.f * cos(cameraOrbit * core::DEGTORAD) * cos(cameraAngle * core::DEGTORAD);
cameraTarget.Y += 1000.f * sin(cameraAngle * core::DEGTORAD);

principalcam->setTarget(cameraTarget);
By the way, I'd recommend not posting clearly buggy code in future, since you may find that people waste their time and yours by debugging the parts that you aren't interested in.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply