terrain + node problem
terrain + node problem
for some reason, my character vibrates up and down when it is going down a terrain, im not entirely sure why, because i dont have a vector or anything pulling it down. can anyone explain why this is happening, and how to fix it. (i think its something wrong with my collision detection, but idk how it works too well)
ok here goes
Code: Select all
#include <irrlicht.h>
#include <iostream>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
using namespace std;
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif
int x = 0;
int y = 0;
int z = 0;
int count;
bool leftx = false;
bool rightx = false;
bool up = false;
bool down= false;
int charz = 2260;
int chary = -30;
int charx = 2630;
scene::IAnimatedMeshSceneNode* node = 0;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
event.KeyInput.PressedDown)
{
if(event.KeyInput.Key == KEY_KEY_W)
{
if (up == false)
{
node->setMD2Animation(EMAT_RUN);
up = true;
}
}
if(event.KeyInput.Key == KEY_KEY_S)
{
if (down == false)
{
down = true;
node->setMD2Animation(EMAT_RUN);
}
}
if(event.KeyInput.Key == KEY_KEY_A)
{
if (leftx == false)
{
leftx = true;
node->setMD2Animation(EMAT_RUN);
}
}
if(event.KeyInput.Key == KEY_KEY_D)
{
if (rightx == false)
{
node->setMD2Animation(EMAT_RUN);
rightx = true;
}
}
}
if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
!event.KeyInput.PressedDown)
{
if(event.KeyInput.Key == KEY_KEY_W)
{
if (up == true)
{
up =false;
}
}
if(event.KeyInput.Key == KEY_KEY_S)
{
if (down == true)
{
down = false;
}
}
if(event.KeyInput.Key == KEY_KEY_A)
{
if (leftx == true)
{
leftx = false;
}
}
if(event.KeyInput.Key == KEY_KEY_D)
{
rightx = false;
}
if(rightx == false && leftx == false && up == false && down == false)
node->setMD2Animation(EMAT_STAND);
}
return false;
}
};
int main()
{
MyEventReceiver receiver;
IrrlichtDevice *device = createDevice(EDT_OPENGL, core::dimension2d<s32>(640, 480), 32, false, false, false, &receiver);
device->setWindowCaption(L"Testing program");
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
IAnimatedMesh* mesh = smgr->getMesh("sydney.md2");
node = smgr->addAnimatedMeshSceneNode( mesh );
//ICameraSceneNode * cam = smgr->addCameraSceneNodeFPS(0, 0,0);
ICameraSceneNode * cam = smgr->addCameraSceneNode(0, vector3df(charx,chary+60,charz-120), vector3df(charx,chary,charz));
// camera->setPosition(core::vector3df(1900*2,255*2,3700*2));
cam->setTarget(core::vector3df(charx,chary,charz));
cam->setFarValue(12000.0f);
// disable mouse cursor
device->getCursorControl()->setVisible(false);
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.0f, 3.3f, 40.0f), // 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->setPosition(vector3df(-1000,-900,600));
terrain->scaleTexture(1.0f, 20.0f);
if (node)
{
node->setMaterialFlag(EMF_LIGHTING, false);
node->setMD2Animation (scene::EMAT_STAND );
node->setMaterialTexture( 0, driver->getTexture("sydney.bmp") );
//node->setPosition(vector3df(2397*2,343*2,2700*2));
node->setPosition(core::vector3df(charx,chary,charz));
}
// create triangle selector for the terrain
scene::ITriangleSelector* selector
= smgr->createTerrainTriangleSelector(terrain, 0);
terrain->setTriangleSelector(selector);
selector->drop();
ISceneNodeAnimator* anim2 = smgr->createCollisionResponseAnimator(
selector, node,core::vector3df(60,50,60),
core::vector3df(0,-9.8f,0),
core::vector3df(0,50,0));
node->addAnimator(anim2);
anim2->drop();
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
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"));
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
int lastFPS = -1;
while(device->run())
{
if (up == true)
{
core::vector3df v = node->getPosition();
v.Z += 5.0f;
node->setRotation(core::vector3df(0,270,0));
node->setPosition(v);
core::vector3df r = cam->getPosition();
cam->setTarget(v);
}
if (down == true)
{
core::vector3df v = node->getPosition();
v.Z -= 5.0f;
node->setRotation(core::vector3df(0,90,0));
node->setPosition(v);
core::vector3df r = cam->getPosition();
cam->setTarget(v);
}
if (leftx == true)
{
core::vector3df v = node->getPosition();
v.X -= 5.0f;
node->setRotation(core::vector3df(0,180,0));
node->setPosition(v);
vector3df r = cam->getPosition();
cam->setTarget(v);
}
if (rightx == true)
{
core::vector3df v = node->getPosition();
v.X += 5.0f;
node->setRotation(core::vector3df(0,0,0));
node->setPosition(v);
cam->setTarget(v);
}
if (rightx == true && down == true)
node->setRotation(core::vector3df(0,45,0));
if (rightx == true && up == true)
node->setRotation(core::vector3df(0,315,0));
if (leftx == true && down == true)
node->setRotation(core::vector3df(0,135,0));
if (leftx == true && up == true)
node->setRotation(core::vector3df(0,225,0));
if (device->isWindowActive())
{
vector3df r = node->getPosition();
r.Y += 60;
r.Z -=120;
cam->setPosition(r);
stringw str = L"Terrain Renderer - Irrlicht Engine [";
str += charx;
str += " : ";
str += chary;
str += " : ";
str += charz;
device->setWindowCaption(str.c_str());
driver->beginScene(true, true, 0 );
x++;
if (x>360)
x=0;
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
// display frames per second in window title
}
device->drop();
return 0;
}
The problem could come from the event receiver. You always return false in your onEvent function. I think you should change your onEvent function (search for the boolkeys method in the forum, it's a good and easy way to handle the keyboard).
My guess is that the boolean irrlicht return in this function is used to tell irrlicht that we have taken this event in account (please, correct me if i'm wrong), and in your case, this would mean that they are always called. But once again, it' pure speculation from me...
My guess is that the boolean irrlicht return in this function is used to tell irrlicht that we have taken this event in account (please, correct me if i'm wrong), and in your case, this would mean that they are always called. But once again, it' pure speculation from me...