Hello everyone,
Whenever I try to get the height of a terrainscenenode at the position of the camera, I get the following value: -340282346638528859811704183484516925440.000000
It seems that only when i do ->getHeight(0.f,0.f) that it returns a real value, for every other X or Z coordinate it will fails.
Does anyone know what is causing this?
Ps. The position of the terrainscenenode is 0,0,0, the scale is 40.f, 15.f, 40.f and the heightmap size is 257x257.
-Diho
ITerrainSceneNode getheight inf
Re: ITerrainSceneNode getheight inf
Can't tell anything without example showing the problem. That it can give values on other positions can be seen in the example 12.TerrainRendering. That writes the terrain-height at the position the camera into the title-bar.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: ITerrainSceneNode getheight inf
Here is my current code:
Hope you can help me.
-Diho
Code: Select all
#include <iostream>
#include <sstream>
#include <irrlicht.h>
#include "MyEventReceiver.h"
//using namespace std;
/*
* namespaces from the Irrlight engine
*/
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
scene::ICameraSceneNode *camera;
ITerrainSceneNode* terrainArray[10][10];
/*
* If Windows, then get rid of the console window
*/
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
bool placeCam() {
core::vector3df campos, setcam;
float Y1, Y2, Y3, Y;
float lift = 10; //may need to change depending on speed of cam/object
campos = camera->getAbsolutePosition();
int row = floor( campos.X / 10240.f);
int col = floor(campos.Z / 10240.f);
Y1 = terrainArray[1][1]->getHeight(10241.f, 10241.f);
Y2 = terrainArray[row][col]->getHeight(campos.X+3.1 , campos.Z+3.1);
Y3 = terrainArray[row][col]->getHeight(campos.X-.1 , campos.Z-.1);
Y = ((Y1 + Y2 + Y3)+lift)/3;
printf("Y1:%f\n", Y1);
printf("Y2:%f\n", Y2);
printf("Y3:%f\n", Y3);
printf("Y:%f\n", Y);
setcam = core::vector3df(campos.X, Y+5.0, campos.Z); //dont change 5.0, change lift variable
camera->setPosition(setcam);
return true;
}
int main() {
MyEventReceiver receiver;
IrrlichtDevice *device =
createDevice(video::EDT_OPENGL, dimension2d<u32>(640, 480), 16,
false, false, false, &receiver);
if (!device)
return 1;
device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");
IVideoDriver *driver = device->getVideoDriver();
ISceneManager *smgr = device->getSceneManager();
IGUIEnvironment *env = device->getGUIEnvironment();
env->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
rect<s32>(10, 10, 260, 22), true);
SKeyMap keyMap[2];
keyMap[0].Action = EKA_MOVE_FORWARD; //We want to associate this key with forward movement
keyMap[0].KeyCode = KEY_KEY_W; //We want to use the W key to move forward
keyMap[1].Action = EKA_MOVE_BACKWARD;
keyMap[1].KeyCode = KEY_KEY_S;
// add camera
camera =
smgr->addCameraSceneNodeFPS(0, 100.0f, 0.7f, -1, keyMap, 2, true);
camera->setPosition(core::vector3df(2700 * 2, 255 * 2, 2600 * 2));
camera->setTarget(core::vector3df(2397 * 2, 343 * 2, 2700 * 2));
camera->setFarValue(42000.0f);
// disable mouse cursor
device->getCursorControl()->setVisible(false);
// add terrain scene node
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
std::stringstream sstm;
sstm << "../../media/terrain/tiles/terrain-heightmap-" << i << "-" << j << ".png";
std::string string1 = sstm.str();
io::path path = string1.c_str();
float rot1, rot2, rot3;
rot1 = 0.f;
rot2 = -90.f;
rot3 = 0.f;
terrainArray[i][j] = smgr->addTerrainSceneNode(
path,
0, // parent node
-1, // node id
core::vector3df((f32)(i * 10240.f), 0.f, (f32)(j * 10240.f)), // position
core::vector3df(rot1, rot2, rot3), // rotation
core::vector3df(40.f, 15.0f, 40.f), // scale
video::SColor(255, 255, 255, 255), // vertexColor
4, // maxLOD
scene::ETPS_17, // patchSize
0 // smoothFactor
);
}
}
//printf("size:%f", terrainArray[0][0]->);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
terrainArray[i][j]->setMaterialFlag(video::EMF_LIGHTING, false);
terrainArray[i][j]->setMaterialTexture(0, driver->getTexture("../../media/terrain/terrain-texture.jpg"));
terrainArray[i][j]->setMaterialTexture(1, driver->getTexture("../../media/terrain/detailmap3.jpg"));
terrainArray[i][j]->setMaterialType(video::EMT_DETAIL_MAP);
terrainArray[i][j]->scaleTexture(1.0f, 20.0f);
scene::ITriangleSelector* selector = smgr->createTerrainTriangleSelector(terrainArray[i][j], 0);
terrainArray[i][j]->setTriangleSelector(selector);
// create collision response animator and attach it to the camera
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(selector, camera,
core::vector3df(60, 100, 60),
core::vector3df(0, 0, 0),
core::vector3df(0, 50, 0));
camera->addAnimator(anim);
anim->drop();
}
}
IMesh* mesh = smgr->getMesh("../../media/models/crate.md2");
IMeshSceneNode* node = smgr->addMeshSceneNode( mesh,
0, // parent node
-1, // node id
core::vector3df(2700 * 2, 550 * 2, 2600 * 2), // position
core::vector3df(0.f, 0.f, 0.f), // rotation
core::vector3df(20.f, 20.0f, 20.f), // scale
false
);
node->setMaterialFlag(EMF_LIGHTING, false);
node->setMaterialTexture(0, driver->getTexture("../../media/models/crate2.png"));
// create skybox and skydome
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
scene::ISceneNode *skybox = smgr->addSkyBoxSceneNode(
driver->getTexture("../../media/irrlicht2_up.jpg"),
driver->getTexture("../../media/irrlicht2_dn.jpg"),
driver->getTexture("../../media/irrlicht2_lf.jpg"),
driver->getTexture("../../media/irrlicht2_rt.jpg"),
driver->getTexture("../../media/irrlicht2_ft.jpg"),
driver->getTexture("../../media/irrlicht2_bk.jpg"));
scene::ISceneNode *skydome = smgr->addSkyDomeSceneNode(driver->getTexture("../../media/skydome.jpg"), 16, 8, 0.95f,
2.0f);
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
int lastFPS = -1;
while (device->run())
if (device->isWindowActive()) {
placeCam();
driver->beginScene(true, true, 0);
smgr->drawAll();
env->drawAll();
driver->endScene();
// display frames per second in window title
int fps = driver->getFPS();
if (lastFPS != fps) {
core::stringw str = L"Terrain Renderer - Irrlicht Engine [";
str += driver->getName();
str += "] FPS:";
str += fps;
// Also print terrain height of current camera position
// We can use camera position because terrain is located at coordinate origin
str += " Height: ";
// str += terrainArray[1][1]->getHeight(camera->getAbsolutePosition().X,
// camera->getAbsolutePosition().Z);
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
device->drop();
return 0;
}
-Diho
Re: ITerrainSceneNode getheight inf
It seems that, whenever I set the rotation of each terrain node to 0.f instead of -90, it works. I saw in CTerrainSceneNode.cpp that the scale vector is used quite a bit for calculating the height of a certain spot at the terrain.
I am glad that it works now, but I would like to keep my -90 rotation (see the rot2 variable in my code.). I changed it to 270, which, if I am not mistaking, should give the same results as -90. But even then I get this strange results.
I am glad that it works now, but I would like to keep my -90 rotation (see the rot2 variable in my code.). I changed it to 270, which, if I am not mistaking, should give the same results as -90. But even then I get this strange results.