Page 1 of 1

Collision Detection for anited model on terrain

Posted: Sun Mar 05, 2006 1:24 am
by Sinsemilla
Hi all,

I tried to let run an animated Model over a heightmap based terrain with automatic collision detection for the model. the problem is the collision doesnt work, the model makes crazy things.


many thanks for answers in future
Sinsemilla


The Code:
#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>

using namespace irr;
#pragma comment(lib, "Irrlicht.lib")

scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;

class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
// W & S Key Movement for the Camera
if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
!event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_KEY_W:
case KEY_KEY_S:
{
core::vector3df v = node->getPosition();
v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
node->setPosition(v);
}
return true;
}
}

return false;
}
};

int main()
{
MyEventReceiver receiver;
// Scene Manager & Driver
device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(1024, 768), 16, true, false, false, &receiver);

video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();

// Create Model
scene::IAnimatedMeshSceneNode* anms = smgr->addAnimatedMeshSceneNode(smgr->getMesh("res/sydney.md2"));

// Create Terrain
scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode( "res/terrain-heightmap.bmp");
terrain->setPosition(core::vector3df(0,0,0));
terrain->setScale(core::vector3df(40, 0, 40));
terrain->setMaterialFlag(video::EMF_LIGHTING, false);

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

terrain->setMaterialType(video::EMT_DETAIL_MAP);
terrain->scaleTexture(1.0f, 20.0f);

// Animation
scene::ISceneNodeAnimator* anim = smgr->createFlyCircleAnimator(core::vector3df(0,20,0), 20);
anms->addAnimator(anim);
anim->drop();

anms->setMaterialFlag(video::EMF_LIGHTING, false);
anms->setFrameLoop(320, 360);
anms->setAnimationSpeed(30);
anms->setRotation(core::vector3df(0,180.0f,0));
anms->setMaterialTexture(0, driver->getTexture("res/sydney.bmp"));

// Camera
scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(0,100.0f,1200.0f);
camera->setPosition(core::vector3df(0,20,0));
camera->setTarget(core::vector3df(0,343*2,0));
camera->setFarValue(12000.0f);

// Collision Detection
scene::ITriangleSelector* selector = smgr->createTerrainTriangleSelector(terrain);
core::aabbox3d<f32> box = anms->getBoundingBox();

anim = smgr->createCollisionResponseAnimator(selector, anms, box.MaxEdge - box.getCenter());
anms->addAnimator(anim);
anim->drop();

// drawing
int lastFPS = -1;

while(device->run())
{
driver->beginScene(true, true, video::SColor(255,113,113,133));
smgr->drawAll();
driver->endScene();

int fps = driver->getFPS();

if (lastFPS != fps)
{
wchar_t tmp[1024];
swprintf(tmp, 1024, L"Movement Example - Irrlicht Engine [%s] fps:%d",
driver->getName(), fps);

device->setWindowCaption(tmp);
lastFPS = fps;
}
}

device->drop();

return 0;
}

Posted: Sun Mar 05, 2006 10:37 pm
by r3i
:shock: What kind of thing it did??
:twisted: maybe you must exorcist it... :D

Try to set not only the radius of the ellipsoid but also gravity and translation.

If I've understand you wanna make your object fly in circle on the terrain, but it's imopossible because by default gravity is -100.0f so your model will pressed on the terrain!

try setting gravity as vector( 0, 0, 0 ) in order to not press him on the terrain.

P.S. : for next post explain what you wanna do, not only code!

Posted: Sun Mar 05, 2006 10:53 pm
by Sinsemilla
The model jumped all the time between the Ground and the point i used.

I wanted to let run the model on the Ground, not let it fly. It should just walk over the hills and planes, and i wanted to use the collision detection because the model shouldnt walk through the map but on it.

The only thing i need is to now, is which height has the Ground under the models feet?

Posted: Mon Mar 06, 2006 1:09 am
by r3i
ok, now is better.
If it jumps it's because its initial position is wrong respect to the ground.
Maybe initially you set a position of the model that intersects with the ground.

Try to setPosition of the model for example on ( 0, 30, 0 ) : it'll fall back and run and if run and not jumps you have a solution.

For knowing the height of your ground i think you must check the bounding boxes but It's not a precise measure.