Please help (3rd RPG camera)

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
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

Please help (3rd RPG camera)

Post by full_newbie »

Hello. I create 3rd rpg camera, but i am not understand, why dont work it.
I see any more article's and topic's: tombraider camera, 3rd camera, IrrCamera3D and more. I like gothic style camera.
This all find code and my code (not work):
main.cpp

Code: Select all

#include "IrrCamera3D.h"

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

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:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};

int main(int argc, char** argv)
{
IrrCamera3D cam_control;
MyEventReceiver receiver;
IrrlichtDevice *device =createDevice( video::EDT_OPENGL, dimension2d<u32>(1024, 768), 32, true, false, false, 0);
device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");

    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();

    guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
        rect<int>(10,10,200,22), true);

    device->getCursorControl()->setVisible(false);


	// add terrain scene node
	scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode(
		"media/terrain-heightmap.bmp",
		0,					// parent node
		-1,					// node id
		core::vector3df(0.f, 0.f, 0.f),		// position
		core::vector3df(0.f, 0.f, 0.f),		// rotation
		core::vector3df(40.f, 4.4f, 40.f),	// scale
		video::SColor ( 255, 255, 255, 255 ),	// vertexColor
		5,					// maxLOD
		scene::ETPS_17,				// patchSize
		4					// smoothFactor
		);

	terrain->setMaterialFlag(video::EMF_LIGHTING, false);

	terrain->setMaterialTexture(0,
			driver->getTexture("media/terrain-texture.jpg"));
	terrain->setMaterialTexture(1,
			driver->getTexture("media/detailmap3.jpg"));

	terrain->setMaterialType(video::EMT_DETAIL_MAP);

	terrain->scaleTexture(1.0f, 20.0f);

    IAnimatedMesh* mesh = smgr->getMesh("media/sydney.md2");
    IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );

vector3df(0,5,0));

cam_control.Create(smgr);
u32 then = device->getTimer()->getTime();
const f32 MOVEMENT_SPEED = 5.f;
    while(device->run())
    {
        const u32 now = device->getTimer()->getTime();
		const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
        then = now;
        if(receiver.IsKeyDown(irr::KEY_KEY_W))
			cam_control.moveForward(MOVEMENT_SPEED * frameDeltaTime);
		else if(receiver.IsKeyDown(irr::KEY_KEY_S))
			cam_control.moveBack(MOVEMENT_SPEED * frameDeltaTime);

		if(receiver.IsKeyDown(irr::KEY_KEY_A))
			cam_control.moveLeft(MOVEMENT_SPEED * frameDeltaTime);
		else if(receiver.IsKeyDown(irr::KEY_KEY_D))
			cam_control.moveRight(MOVEMENT_SPEED * frameDeltaTime);
        driver->beginScene(true, true, SColor(0,200,200,200));

        smgr->drawAll();
        guienv->drawAll();

        driver->endScene();
    }


    device->drop();

    return 0;
}

IrrCamera3D.h

Code: Select all

#pragma once
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class IrrCamera3D
{
public:
   f32 rotationX;
    f32 rotationY;
    core::vector3df direction;
   scene::ICameraSceneNode* camera;

   IrrCamera3D(void);
   virtual ~IrrCamera3D(void);

   virtual void Initialize();
   virtual bool Create(ISceneManager* level);
   virtual bool Cleanup();

   void turnRight(f32 i);
    void turnLeft(f32 i);
    void turnUp(f32 i);
    void turnDown(f32 i);

    void moveForward(f32 i);
    void moveBack(f32 i);
    void moveRight(f32 i);
    void moveLeft(f32 i);

    void setHeight(f32 i);
    void setPosition(core::vector3df pos);

    core::vector3df getPosition();
    core::vector3df getDirection();
    core::vector3df getTarget();

   f32 getHeading();
    f32 getPitch();

    f32 getFarValue();
    void setFarValue(f32 f);

    f32 getNearValue();
    void setNearValue(f32 n);

    f32 getFOV();
    void setFOV(f32 v);

    f32 getAspectRatio();
    void setAspectRatio(f32 a);
};

IrrCamera3D.cpp

Code: Select all

#include "IrrCamera3D.h"

IrrCamera3D::IrrCamera3D(void)
{
   Initialize();
}

IrrCamera3D::~IrrCamera3D(void)
{
   Cleanup();
}

void IrrCamera3D::Initialize()
{
}

bool IrrCamera3D::Create(scene::ISceneManager* level)
{
   camera = level->addCameraSceneNode();
   rotationX = 0.0f;
   rotationY = 0.0f;
   direction = core::vector3df(0,0,1);
   return true;
}

bool IrrCamera3D::Cleanup()
{
   // remove the camera
   return false;
}

void IrrCamera3D::turnRight(f32 i)
    {
        rotationY += i;
        if(rotationY>=360)rotationY-=360;
        if(rotationY<0)rotationY+=360;

        direction = core::vector3df(0,0,1);

        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
        matrix.rotateVect(direction);

        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }

void IrrCamera3D::turnLeft(f32 i)
    {
        rotationY -= i;
        if(rotationY>=360)rotationY-=360;
        if(rotationY<0)rotationY+=360;

        direction = core::vector3df(0,0,1);

        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
        matrix.rotateVect(direction);

        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }

    void IrrCamera3D::turnUp(f32 i)
    {
        rotationX += i;
        if(rotationX>=360)rotationX-=360;
        if(rotationX<0)rotationX+=360;

        direction = core::vector3df(0,0,1);

        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
        matrix.rotateVect(direction);

        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }

    void IrrCamera3D::turnDown(f32 i)
    {
        rotationX -= i;
        if(rotationX>=360)rotationX-=360;
        if(rotationX<0)rotationX+=360;

        direction = core::vector3df(0,0,1);

        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
        matrix.rotateVect(direction);

        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }

    void IrrCamera3D::moveForward(f32 i)
    {

      core::vector3df step = core::vector3df(0,0,i);

        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
        matrix.rotateVect(step);

        camera->setPosition(camera->getPosition() + step);
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();

    }

    void IrrCamera3D::moveBack(f32 i)
    {

        core::vector3df step = core::vector3df(0,0,-i);

        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
        matrix.rotateVect(step);

        camera->setPosition(camera->getPosition() + step);
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }

    void IrrCamera3D::moveRight(f32 i)
    {

        core::vector3df step = core::vector3df(i,0,0);

        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (0,rotationY,0));
        matrix.rotateVect(step);

        camera->setPosition(camera->getPosition() + step);
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }

    void IrrCamera3D::moveLeft(f32 i)
    {

        core::vector3df step = core::vector3df(-i,0,0);

        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (0,rotationY,0));
        matrix.rotateVect(step);

        camera->setPosition(camera->getPosition() + step);
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }

    void IrrCamera3D::setHeight(f32 i)
    {
        camera->setPosition(core::vector3df(camera->getPosition().X, i, camera->getPosition().Z));
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }

    void IrrCamera3D::setPosition(core::vector3df pos)
    {
        camera->setPosition(pos);
        camera->updateAbsolutePosition();
    }

    core::vector3df IrrCamera3D::getPosition()
    {
        return camera->getPosition();
    }

    core::vector3df IrrCamera3D::getDirection()
    {
        return direction;
    }

    core::vector3df IrrCamera3D::getTarget()
    {
        return camera->getTarget();
    }

    f32 IrrCamera3D::getHeading()
    {
        return rotationY;
    }

    f32 IrrCamera3D::getPitch()
    {
        return rotationX;
    }

    f32 IrrCamera3D::getFarValue()
    {
        return camera->getFarValue();
    }

    void IrrCamera3D::setFarValue(f32 f)
    {
        //camera->setFarValue(f);

    }

    f32 IrrCamera3D::getNearValue()
    {
        return camera->getNearValue();
    }

    void IrrCamera3D::setNearValue(f32 n)
    {
        camera->setNearValue(n);
    }

    f32 IrrCamera3D::getFOV()
    {
        return camera->getFOV();
    }

    void IrrCamera3D::setFOV(f32 v)
    {
        camera->setFOV(v);
    }

    f32 IrrCamera3D::getAspectRatio()
    {
        return camera->getAspectRatio();
    }

    void IrrCamera3D::setAspectRatio(f32 a)
    {
        camera->setAspectRatio(a);
    }


3DP.h

Code: Select all

/*Particular Zlib License /LIBRARY(C).

3DP.h
3DP.cpp

Licenзa ZLIB/LIBPNG
Disponнvel, no original, em: http://www.opensource.org/licenses/zlib-license.php
Versгo 1.01
Licenзa ZLIB/LIBPNG

Direitos autorais Copyright (C) 2008 Lucian. 2009 Lucian,cуdigo original encontrado em um forum.

Este programa de computador й fornecido "como estб",
 sem garantias de qualquer espйcie, expressas ou tбcitas. 
Em nenhuma hipуtese os autores serгo responsбveis por 
quaisquer danos decorrentes do uso deste programa de computador.
Outorga-se permissгo para qualquer um usar este programa de 
computador para quaisquer fins, incluindo utilizaзхes comerciais,
e para o modificar e redistribuir livremente, sujeita аs seguintes restriзхes:


1. A origem deste programa de computador nгo pode ser apresentada
de forma errфnea; vocк nгo pode reivindicar ter escrito o programa
de computador original. Se vocк usar esse programa de computador em
um produto, um reconhecimento de crйditos na documentaзгo do produto
seria apreciada, mas nгo й exigida.


2. Versхes alteradas do cуdigo-fonte devem estar marcadas como tais,
e nгo podem ser apresentadas forma errфnea, como sendo o programa de
computador original.

3. Este aviso nгo pode ser alterado ou removido de qualquer distribuiзгo de cуdigo fonte.*/

  #ifndef _3DP_h_
  #define _3DP_h_ 
 #include <irrlicht.h>

    #define EPSILON 0.001f

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


 


	class CScene3PCamera : public irr::scene::ISceneNodeAnimator,public irr::IEventReceiver
    {

    public:

    //! Construtor
    //! player: Objeto a Seguir
    //! Distanciaancia: The initial Distanciaance from the player
    //! initAngleY: The initial horizontal rotatation
    //! initAngleZ: The initial vertical rotation
    //! targetOffset: The offset from the object's center at which the camera looks
    //! minDistancia/maxDistancia: Distanciaance bounding
    //! minAngle/maxAngle: Rotation bounding. -89 ==> looking up at player, 89 ==> looking down
    //! boxSize: The size of the box in which mouse movements do not result in camera movements
  
    CScene3PCamera(
    irr::scene::ISceneManager *manager,
    irr::gui::ICursorControl *cursor,
    irr::scene::ISceneNode *player,
    irr::f32 Distanciaancia = 50.0f,
    irr::f32 initAngleY = 180.0f,
    irr::f32 initAngleZ = 10.0f,
	bool RotatePlayer = false,
    irr::core::vector3df targetOffset = irr::core::vector3df(0,0,0),
    irr::f32 minDistancia = 20.0f,
    irr::f32 maxDistancia = 200.0f,
    irr::f32 minAngle = -45.0f,
    irr::f32 maxAngle = 89.0f,
    irr::f32 boxSize = 0.0f,
    irr::f32 rotationSpeed = 60.0f);

    //! Destructor
    virtual ~CScene3PCamera(void);

    //! animates the scene node
    virtual void animateNode(ISceneNode* node, u32 timeMs);

    //! Process an input event
   virtual ISceneNodeAnimator* createClone (ISceneNode *node, ISceneManager *newManager=0)
    {
        return new CScene3PCamera(Manager,Cursor,Player,Distancia,AngleY,
			AngleZ,false,TargetOffset,MinDistancia,MaxDistancia,MinAngle,
			MaxAngle,BoxSize,RotationSpeed);
    }
   // virtual bool OnEvent(irr::SEvent event);
    virtual bool OnEvent(const SEvent& event);
    //! Get/set active status
    int isActive();
    void setActive(bool status);

	    virtual ESCENE_NODE_ANIMATOR_TYPE getType(void) const
    {
        return ESNAT_UNKNOWN;
    }


		    virtual bool isEventReceiverEnabled(void) const
    {
        return false;
    }
    //! Get/set box size
    irr::f32 getBoxSize();
    void setBoxSize(irr::f32 newSize);

    //! Map zoom in/zoom out buttons

    //! Access the camera's current orientation
    irr::f32 getOrientation();
	bool XRotatePlayer;

	   // current states
    irr::f32 Distancia;
    irr::f32 AngleY;
    irr::f32 AngleZ;

    private:

    int Active;//bool Active;
/*
    // current states
    irr::f32 Distancia;
    irr::f32 AngleY;
    irr::f32 AngleZ;
*/


    // boundaries
    irr::core::vector3df TargetOffset;
    irr::f32 MinDistancia;
    irr::f32 MaxDistancia;
    irr::f32 MinAngle;
    irr::f32 MaxAngle;
    irr::f32 BoxSize;

    // Motion
    irr::f32 RotationSpeed;
  

    //! Node to follow
    irr::scene::ISceneNode *Player;

    irr::scene::ISceneManager *Manager;
    irr::gui::ICursorControl *Cursor;

	
    //! Puts the cursor back in the box
    void updateCursorPosition();
    };





#endif
3DP.cpp

Code: Select all

#include "3DP.h"
    CScene3PCamera::CScene3PCamera(
    irr::scene::ISceneManager *manager,
    irr::gui::ICursorControl *cursor,
    irr::scene::ISceneNode *player,
    irr::f32 Distanciaancia,irr::f32 initAngleY,
    irr::f32 initAngleZ,
	bool RotatePlayer,
    irr::core::vector3df targetOffset,
    irr::f32 minDistancia,
    irr::f32 maxDistancia,
    irr::f32 minAngle,
    irr::f32 maxAngle,
    irr::f32 boxSize,
    irr::f32 rotationSpeed)
    : Active(true), Manager(manager), Cursor(cursor), Player(player),
    Distancia(Distanciaancia),AngleY(initAngleY),AngleZ(initAngleZ),XRotatePlayer(RotatePlayer),
    TargetOffset(targetOffset),
    MinDistancia(minDistancia), MaxDistancia(maxDistancia), MinAngle(-maxAngle), MaxAngle(-minAngle),
    BoxSize(boxSize), RotationSpeed(rotationSpeed)
    {
 

    Cursor->setPosition(0.5f,0.5f);

    // Ensure Distanciaance is bounded correctly
    if(Distancia < MinDistancia) Distancia = MinDistancia;
    else if(Distancia > MaxDistancia) Distancia = MaxDistancia;

    // Bound MinAngle/MaxAngle to avoid problematic areas
    if(MinAngle < -89.0f) MinAngle = -89.0f;
    if(MinAngle > 89.0f) MinAngle = 89.0f;
    if(MaxAngle < -89.0f) MaxAngle = -89.0f;
    if(MaxAngle > 89.0f) MaxAngle = 89.0f;
    if(minAngle > maxAngle) MaxAngle = MinAngle+1.0f;

    // Ensure Vertical Rotation Angle is bounded correctly
    if(AngleZ < MinAngle) AngleZ = MinAngle;
    else if(AngleZ > MaxAngle) AngleZ = MaxAngle;
    }

    //! Destructor
    CScene3PCamera::~CScene3PCamera(void)
    {
    }

    //! Puts the cursor back in the box
    void CScene3PCamera::updateCursorPosition()
    {
    irr::core::position2d<irr::f32> pos = Cursor->getRelativePosition();

    if(pos.X < (0.5f-BoxSize)) pos.X = (0.5f-BoxSize);
    if(pos.X > (0.5f+BoxSize)) pos.X = (0.5f+BoxSize);

    if(pos.Y < (0.5f-BoxSize)) pos.Y = (0.5f-BoxSize);
    if(pos.Y > (0.5f+BoxSize)) pos.Y = (0.5f+BoxSize);

    Cursor->setPosition(pos);
    }

    //! Process an input event
  bool CScene3PCamera::OnEvent(const irr::SEvent& event)
    {
    if(!Active)
    {
    return false;
    }

    if(event.EventType == irr::EET_KEY_INPUT_EVENT)
    {

    }

    return false;
    }

    //! Get/set active status
    int CScene3PCamera::isActive()
    {
    return Active;
    }

    void CScene3PCamera::setActive(bool status)
    {
    // reset the cursor only if we are switching back to active from inactive
    if(!Active && status)
    {
    updateCursorPosition();
    }

    Active = status;
    }

    //! Get/set box size
    irr::f32 CScene3PCamera::getBoxSize()
    {
    return BoxSize;
    }

    void CScene3PCamera::setBoxSize(irr::f32 newSize)
    {
    BoxSize = newSize;
    updateCursorPosition();
    }

 

    //! Access the camera's current orientation
    irr::f32 CScene3PCamera::getOrientation()
    {
    return AngleY;
    }


    //! animates the scene node
    void CScene3PCamera::animateNode(irr::scene::ISceneNode *node, irr::u32 timeMs)
    {
    // make sure you don't go attaching this animator to anything other than a camera
    irr::scene::ICameraSceneNode *camera = (irr::scene::ICameraSceneNode*)node;

    if(Manager->getActiveCamera() != camera)
    {
    return;
    }

    if(Active)
    {
    // Camera is active, rotate as necessary
    irr::core::position2d<irr::f32> pos = Cursor->getRelativePosition();

    if(pos.X < 0.5f-BoxSize-EPSILON)
    {
    AngleY -= (pos.X-(0.5f-BoxSize))*RotationSpeed;//Decrement of this statement gives the correct movement to the mouse.... by smartwhiz
    }

    if(pos.X > 0.5f+BoxSize+EPSILON)
    {
    AngleY -= (pos.X-(0.5f+BoxSize))*RotationSpeed;//Decrement of this statement gives the correct movement to the mouse.... by smartwhiz
    }

    // So we don't get huge rotation numbers
    if(AngleY > 360.0f)
    {
    AngleY += 360.0f;
    }

    if(AngleY < 0.0f)
    {
    AngleY -= 360.0f;
    }

    if(pos.Y < 0.5f-BoxSize-EPSILON)
    {
    AngleZ -= (pos.Y-(0.5f-BoxSize))*RotationSpeed;
    }

    if(pos.Y > 0.5f+BoxSize+EPSILON)
    {
    AngleZ -= (pos.Y-(0.5f+BoxSize))*RotationSpeed;
    }

    // Ensure Vertical Rotation Angle is bounded correctly
    if(AngleZ < MinAngle) AngleZ = MinAngle;
    else if(AngleZ > MaxAngle) AngleZ = MaxAngle;

    //keep the player in the view angle that of the camera, this is the change made......... by smartwhiz
	if (XRotatePlayer == true){
	Player->setRotation(irr::core::vector3df(0,-(AngleY+180),0));
	}


    updateCursorPosition();
    }

    // Create translation vector
    irr::core::vector3df translation(Distancia,0,0);
    translation.rotateXYBy(-AngleZ,irr::core::vector3df(0,0,0));
    translation.rotateXZBy(AngleY,irr::core::vector3df(0,0,0));

    // Assumes the camera is *not* a child of the player node
    camera->setTarget(Player->getPosition()+TargetOffset);
    camera->setPosition(Player->getPosition()+translation+TargetOffset);
    }


   
I don't know how combine all, and create gothic style 3rd RPG camera.
Please help newbie.
Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Please help (3rd RPG camera)

Post by randomMesh »

full_newbie wrote:I create 3rd rpg camera, but i am not understand, why dont work it.
What does not work? What do you see that is different from what you expect?
"Whoops..."
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

Post by full_newbie »

I can not combine this code's(newbie in c++). How work this code? What is write? I need very easy sample on this theme. You can give an example a code that there was mesh (ninjia) and he run on terrain, how in a gothic style? I can find sample, where ninja run on terrain, but there was camera Line Age style.
Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
monchito
Posts: 59
Joined: Thu Mar 08, 2007 9:38 pm

Post by monchito »

use something like

Code: Select all

device->setEventReceiver(&receiver);
and

Code: Select all

  vector3df pos = sydneyNode->getPosition();
  float height = terrain->getHeight( pos.X,pos.Z );
  pos.Y = height;
  sydneyNode->setPosition( pos );
Last edited by monchito on Mon Apr 05, 2010 3:11 pm, edited 1 time in total.
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

Post by full_newbie »

I write this in my project, but it incorect work. This url for download my project (see yours eye's): http://best-scripts.uz/3d_camera.zip (3.26MB)
Where my error?
Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
monchito
Posts: 59
Joined: Thu Mar 08, 2007 9:38 pm

Post by monchito »

Code: Select all

 
void MovePlayer( ISceneNode* node, const f32 distance)
{
  if (!node) return;

  vector3df pos = node->getPosition();
  float rot = node->getRotation().Y;
  pos.X += distance * cos(rot * DEGTORAD);
  pos.Z -= distance * sin(rot * DEGTORAD);
  node->setPosition(pos);
}

//---------------------------------- 
  IrrCamera3D cam_control;
  cam_control.Create(smgr);
  cam_control.setPosition( vector3df(0,0,-80) ); //near player
  cam_control.setHeight(300);  // above ground
  cam_control.turnDown(-35);   // look down

  u32 then = device->getTimer()->getTime();
  const f32 MOVEMENT_SPEED = 25.f;   //adjust speed

  while(device->run())
  {
    // move player...adjust position... etc
    //...
    vector3df pos = node->getPosition();
    float height = terrain->getHeight( pos.X,pos.Z );
    pos.Y = height;
    node->setPosition( pos + vector3df(0,20,0) ); // add offset height
    printf("\nheight: %.2f",height );  //debug

    const u32 now = device->getTimer()->getTime();
    const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in
    ....
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

Post by full_newbie »

Yes, camera is move, but mesh isn't move and camera isn't rotate when i move mouse. How will use 3DP code (3DP.cpp)? I like gothic style camera, but isn't work my code. How combine 3DP.cpp and IrrCamera3D.cpp? And add coment's. All many thanks.
This is my change main.cpp:

Code: Select all

#include "IrrCamera3D.h"
//#include "IrrCharacterControll.h"

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

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 == irr::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];
};

void MovePlayer( ISceneNode* node, const f32 distance)
{
  if (!node) return;

  vector3df pos = node->getPosition();
  float rot = node->getRotation().Y;
  pos.X += distance * cos(rot * DEGTORAD);
  pos.Z -= distance * sin(rot * DEGTORAD);
  node->setPosition(pos);
}

int main(int argc, char** argv)
{
MyEventReceiver receiver;
IrrlichtDevice *device =createDevice( video::EDT_OPENGL, dimension2d<u32>(1024, 768), 32, true, false, false, 0);
device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");

    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();
    device->setEventReceiver(&receiver);

    guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
        rect<int>(10,10,200,22), true);

    device->getCursorControl()->setVisible(false);

	// add terrain scene node
	scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode(
		"media/terrain-heightmap.bmp",
		0,					// parent node
		-1,					// node id
		core::vector3df(0.f, 0.f, 0.f),		// position
		core::vector3df(0.f, 0.f, 0.f),		// rotation
		core::vector3df(40.f, 4.4f, 40.f),	// scale
		video::SColor ( 255, 255, 255, 255 ),	// vertexColor
		5,					// maxLOD
		scene::ETPS_17,				// patchSize
		4					// smoothFactor
		);

	terrain->setMaterialFlag(video::EMF_LIGHTING, false);

	terrain->setMaterialTexture(0,
			driver->getTexture("media/terrain-texture.jpg"));
	terrain->setMaterialTexture(1,
			driver->getTexture("media/detailmap3.jpg"));

	terrain->setMaterialType(video::EMT_DETAIL_MAP);

	terrain->scaleTexture(1.0f, 20.0f);

    IAnimatedMesh* mesh = smgr->getMesh("media/sydney.md2");
    IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
    vector3df pos = node->getPosition();
    float height = terrain->getHeight( pos.X,pos.Z );
    pos.Z = height;
    node->setPosition( pos );

    if (node)
    {
        node->setMaterialFlag(EMF_LIGHTING, false);
        node->setFrameLoop(0, 310);
        node->setMaterialTexture( 0, driver->getTexture("media/sydney.bmp") );

    }


IrrCamera3D cam_control;
cam_control.Create(smgr);
cam_control.setPosition( vector3df(0,0,-80) );
cam_control.setHeight(300);
cam_control.turnDown(-35);
u32 then = device->getTimer()->getTime();
const f32 MOVEMENT_SPEED = 25.f;
    while(device->run())
    {
        vector3df pos = node->getPosition();
    float height = terrain->getHeight( pos.X,pos.Z );
    pos.Y = height;
    node->setPosition( pos + vector3df(0,20,0) ); // add offset height
    printf("\nheight: %.2f",height );
        const u32 now = device->getTimer()->getTime();
		const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
        then = now;
        if(receiver.IsKeyDown(irr::KEY_KEY_W))
			cam_control.moveForward(MOVEMENT_SPEED * frameDeltaTime);
		else if(receiver.IsKeyDown(irr::KEY_KEY_S))
			cam_control.moveBack(MOVEMENT_SPEED * frameDeltaTime);

		if(receiver.IsKeyDown(irr::KEY_KEY_A))
			cam_control.moveLeft(MOVEMENT_SPEED * frameDeltaTime);
		else if(receiver.IsKeyDown(irr::KEY_KEY_D))
			cam_control.moveRight(MOVEMENT_SPEED * frameDeltaTime);
        driver->beginScene(true, true, SColor(0,200,200,200));

        smgr->drawAll();
        guienv->drawAll();

        driver->endScene();
    }

    device->drop();

    return 0;
}



Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
monchito
Posts: 59
Joined: Thu Mar 08, 2007 9:38 pm

Post by monchito »

Code: Select all

#include "IrrCamera3D.cpp"
#include "3DP.cpp"

using namespace irr;
...
do not mix the two cameras.

Code: Select all

/*
  IrrCamera3D cam_control;
  cam_control.Create(smgr);
  cam_control.setPosition( vector3df(0,0,-80) );
  cam_control.setHeight(300);  // above ground
  cam_control.turnDown(-35);   // look down
*/
  //...
  ICameraSceneNode* cam = smgr->addCameraSceneNode();
  cam->setPosition( vector3df(0,80, -40));
  cam->setTarget( node->getPosition() );
  CScene3PCamera CamDP( smgr,  //manager
              device->getCursorControl(), //cursor
              node);          // player

  CamDP.setBoxSize(200);
  CamDP.animateNode(node,   // player
                    3000);  // millseconds

  u32 then = device->getTimer()->getTime();
  //...
Post Reply