Incorect work ShTlTerrainSceneNode+camera in IrrAI

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

Incorect work ShTlTerrainSceneNode+camera in IrrAI

Post by full_newbie »

hello everyone . Please help me.
If i use camera in IrrAI and ShTlTerrainSceneNode begin incorect work ShTlTerrainSceneNode.
This full code:

Code: Select all

#include <irrlicht.h>
#include "terrain/ShTlTerrainSceneNode.h"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

enum {UP, PRESSED, DOWN, RELEASED};
int keyState[KEY_KEY_CODES_COUNT] = {0};
bool paused = false;
bool quitGame = false;
IrrlichtDevice  *Device=createDevice(EDT_OPENGL, dimension2d<u32>(1024, 768), 32, true, false, false, 0);
IVideoDriver    *Driver=Device->getVideoDriver();
ISceneManager   *SceneManager=Device->getSceneManager();
IGUIEnvironment *Gui=Device->getGUIEnvironment();
scene::IAnimatedMeshSceneNode* playerNode = NULL;
scene::EMD2_ANIMATION_TYPE playerAnim;
void resetKeys() {

	for (int i = 0 ; i < KEY_KEY_CODES_COUNT ; ++i)
		keyState[i] = UP;

}
void updateKeys() {

	for(int i = 0 ; i < KEY_KEY_CODES_COUNT ; ++i)
		if (keyState[i] == RELEASED)
			keyState[i] = UP;
		else if (keyState[i] == PRESSED)
			keyState[i] = DOWN;

}
class MyEventReceiver : public IEventReceiver {

	public:
		virtual bool OnEvent(const SEvent& event) {

			if (event.EventType == EET_KEY_INPUT_EVENT) {

				// update keyStates
				if (event.KeyInput.PressedDown) {
					// If key was not down before
					if (keyState[event.KeyInput.Key] != DOWN)
						keyState[event.KeyInput.Key] = PRESSED; // Set to Pressed
					else
						// if key was down before
						keyState[event.KeyInput.Key] = DOWN; // Set to Down
				} else {
					// if the key is down
					if (keyState[event.KeyInput.Key] != UP)
						keyState[event.KeyInput.Key] = RELEASED; // Set to Released
				}

				switch(event.KeyInput.Key) {
					case KEY_ESCAPE: {
						if (!event.KeyInput.PressedDown) {
							quitGame = true;
							return true;
						}
						break;
					}
/*					case KEY_KEY_V: {
						if (!event.KeyInput.PressedDown) {
							static bool vis = false;
							vis = !vis;
							if (aimgr) aimgr->setDebugVisible(vis);
							return true;
						}
						break;
					}*/
					case KEY_KEY_P: {
						if (!event.KeyInput.PressedDown) {
							// Pause the scene
							paused = !paused;
//							if (pausedText) pausedText->setVisible(paused);
							if (Device)
								if (paused) // Freezes animators
									Device->getTimer()->setSpeed(0);
								else // Unfreezes animators
									Device->getTimer()->setSpeed(1);
							return true;
						}
						break;
					}
/*					case KEY_SPACE: { // Starts the conversation with the npc
						if (!event.KeyInput.PressedDown && character)
							character->startConvo(device->getTimer(), playerNode, playerText);
						return true;
					}*/
				}
			}

			return false;

		}

};

void processKeys(int elapsedTime) {

	bool turning = false;
	bool walking = false;

	if (keyState[KEY_KEY_W] == PRESSED || keyState[KEY_KEY_W] == DOWN) {
		// Move forward
		core::vector3df move = core::vector3df(5,0,0);
		core::matrix4 mat;
		mat.setRotationDegrees(playerNode->getRotation());
		mat.transformVect(move);
		move *= 0.1f * elapsedTime;
		playerNode->setPosition(playerNode->getPosition() + move);
		if (playerAnim != scene::EMAT_RUN) {
			playerNode->setMD2Animation(scene::EMAT_RUN);
			playerAnim = scene::EMAT_RUN;
		}
		walking = true;
	}

	if(keyState[KEY_KEY_S] == PRESSED || keyState[KEY_KEY_S] == DOWN) {
		// Move backward
		core::vector3df move = core::vector3df(5,0,0);
		core::matrix4 mat;
		mat.setRotationDegrees(playerNode->getRotation());
		mat.transformVect(move);
		move *= 0.1f * elapsedTime;
		playerNode->setPosition(playerNode->getPosition() - move);
		if (playerAnim != scene::EMAT_RUN) {
			playerNode->setMD2Animation(scene::EMAT_RUN);
			playerAnim = scene::EMAT_RUN;
		}
		walking = true;
	}

	if(keyState[KEY_KEY_A] == PRESSED || keyState[KEY_KEY_A] == DOWN) {
		// Turn left
		core::vector3df rot = core::vector3df(0,10,0) * 0.1f * (f32)elapsedTime;
		playerNode->setRotation(playerNode->getRotation() - rot);
		if (!walking && playerAnim != scene::EMAT_STAND) {
			playerNode->setMD2Animation(scene::EMAT_STAND);
			playerAnim = scene::EMAT_STAND;
		}
		turning = true;
	}

	if(keyState[KEY_KEY_D] == PRESSED || keyState[KEY_KEY_D] == DOWN) {
		// Turn right
		core::vector3df rot = core::vector3df(0,10,0) * 0.1f * (f32)elapsedTime;
		playerNode->setRotation(playerNode->getRotation() + rot);
		if (!walking && playerAnim != scene::EMAT_STAND) {
			playerNode->setMD2Animation(scene::EMAT_STAND);
			playerAnim = scene::EMAT_STAND;
		}
		turning = true;
	}

	if (!walking && !turning && playerAnim != scene::EMAT_STAND) {
		// Stand still
		playerNode->setMD2Animation(scene::EMAT_STAND);
		playerAnim = scene::EMAT_STAND;
	}

}
int main(int argc, char** argv)
{
MyEventReceiver receiver;
Device->setEventReceiver(&receiver);
ShTlTerrainSceneNode* terrain;
 scene::IAnimatedMesh* mesh = SceneManager->getMesh("data/avatar/sydney.md2");
	if (!mesh) printf("couldn't load player mesh\n");
	playerNode = SceneManager->addAnimatedMeshSceneNode(mesh);
	if (!playerNode) printf("couldn't create player node\n");
	playerNode->setMaterialFlag(video::EMF_LIGHTING, false);
	playerNode->setMD2Animation(scene::EMAT_STAND);
	playerAnim = scene::EMAT_STAND;
	playerNode->setMaterialTexture(0, Driver->getTexture("data/avatar/sydney.jpg"));
	playerNode->OnAnimate(0);
	playerNode->setPosition(vector3df(-1200,-50,-1400));
	core::aabbox3d<f32> box = playerNode->getTransformedBoundingBox();
	playerNode->setPosition(box.MaxEdge - box.getCenter());
	scene::ICameraSceneNode* camera = SceneManager->addCameraSceneNode(playerNode);
	if (!camera) printf("Failed camera creation\n");
	camera->setPosition(((box.MaxEdge - box.getCenter())*2.0f)+core::vector3df(-100,25,0));
	camera->setTarget(playerNode->getPosition());

    terrain = new ShTlTerrainSceneNode(SceneManager, 500, 500, 1., 100);
	terrain->drop();
    terrain->loadHeightMap("data/world/111.bmp", 10, 0, 0);
	// setup terrain node
	terrain->setMaterialTexture(0, Driver->getTexture("data/world/terrain-texture.jpg"));
	terrain->setMaterialTexture(1, Driver->getTexture("data/world/detail_grass.jpg"));
	terrain->setMaterialFlag(video::EMF_LIGHTING, true);
	terrain->setMaterialFlag(video::EMF_FOG_ENABLE, true);
	core::vector3df terrainPos(-1200,-50,-1400);
    terrain->setPosition(terrainPos);
    terrain->follow(camera);

    scene::ISceneNode* skybox=SceneManager->addSkyBoxSceneNode(
		Driver->getTexture("data/world/irrlicht2_up.jpg"),
		Driver->getTexture("data/world/irrlicht2_dn.jpg"),
		Driver->getTexture("data/world/irrlicht2_lf.jpg"),
		Driver->getTexture("data/world/irrlicht2_rt.jpg"),
		Driver->getTexture("data/world/irrlicht2_ft.jpg"),
		Driver->getTexture("data/world/irrlicht2_bk.jpg"));

    int startTime = Device->getTimer()->getTime();
	int elapsedTime = 0;
    while(Device->run())
    {
        elapsedTime = Device->getTimer()->getTime() - startTime;
		startTime = Device->getTimer()->getTime();
		processKeys(elapsedTime);

		// Update third person cam
		camera->setTarget(playerNode->getPosition());
        Driver->beginScene(true, true, SColor(0,200,200,200));

        SceneManager->drawAll();
        Gui->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)
xirtamatrix
Posts: 219
Joined: Fri Feb 19, 2010 4:03 pm
Location: Estonia

Post by xirtamatrix »

Can you try to explain a bit more what exactly is the problem?
In what way the ShTlTerrainSceneNode is working incorrectly?
to live, is natural; to die, is not!
Post Reply