Collision with terrain - Height changes collision triangle..

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
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Collision with terrain - Height changes collision triangle..

Post by suliman »

Hi
Using this code from the forum i can get collision with terrain under cursor. The trouble is as i raise or lower terrain the selected triangle is no longer UNDER the cursor. If i want to select the top of a hill for example, i need to place my mouse on the bottom of the hill. On a location with height 0 i instead select the tringle right under the mouse.

It seems to select the tringle that would be under the mouse if the terrain had no height... Is there any way to fix this so i always select what is ACTUALLY under the cursor?
Thanks

Code: Select all

        // move the arrow to the nearest vertex ...
        const position2di mousePosition = myDevice->getCursorControl()->getPosition(); 
        const line3df ray = gfx.smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(mousePosition, ppp.camera);
        vector3df pos2;
        triangle3df Tri;
        const scene::ISceneNode* hitNode;
        if(ppp.mouse.Y<SCREENY-150)
        if (gfx.smgr->getSceneCollisionManager()->getCollisionPoint(ray, selector, pos2, Tri,hitNode))
        {
                //arrow->setPosition(pos);
                static const s32 scale = MAP_SCALE; // terrain is scaled
                static const s32 size = HEIGHTMAP_SIZE; // heightmap pixel-size
                s32 x = (s32)(pos2.X / scale);
                s32 z = (s32)(pos2.Z / scale);
                s32 index = x * size + z;
        
NickLH
Posts: 7
Joined: Wed Aug 24, 2011 7:12 am

Re: Collision with terrain - Height changes collision triang

Post by NickLH »

Can you show us how you create the selector? That is the most likely place that things are going wrong.

From the API: http://irrlicht.sourceforge.net/docu/cl ... 62ea4e71f7

Triangle selectors can be used for doing collision detection. Don't use this selector for a huge amount of triangles like in Quake3 maps. Instead, use for example ISceneManager::createOctreeTriangleSelector(). Please note that the created triangle selector is not automaticly attached to the scene node. You will have to call ISceneNode::setTriangleSelector() for this. To create and attach a triangle selector is done like this:

Code: Select all

 
                ITriangleSelector* s = sceneManager->createTriangleSelector(yourMesh,
                                yourSceneNode);
                yourSceneNode->setTriangleSelector(s);
                s->drop();
 
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Collision with terrain - Height changes collision triang

Post by suliman »

Show where i created which selector?
It is the relevant code i showed you. At least to my understanding

Or do you mean this:

Code: Select all

        ITriangleSelector* terrainSelector = smgr->createTerrainTriangleSelector(terrain, 0);
 
IPORTANT
I also found out its not the height that messes up. Its the height-change since start! So when i change the height by axessing meshbuffer->vertices. The code above still does collision with the original heightmap! Any way around this?
NickLH
Posts: 7
Joined: Wed Aug 24, 2011 7:12 am

Re: Collision with terrain - Height changes collision triang

Post by NickLH »

Yes I meant that part. You don't seem to be assigning a scene node to the selector, nor are you setting the triangle selector to that scene mode as I indicated from the Irrlicht documentation. I have never played with the triangle selector yet, but this seems to be an obvious place to start looking.

If that doesn't work, then maybe recreating the triangle selector every time you update the mesh would work, since you indicated that it works on the original heightmap. Although I don't know if this would be too computationally intensive to do in real time.
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Collision with terrain - Height changes collision triang

Post by suliman »

This worked:

Code: Select all

        //forces selector to take terrain-changes into acount
        if(GUI.thisKeyhandler->keyDownUp(KEY_LBUTTON)){
                selector->drop();
                selector = gfx.smgr->createTerrainTriangleSelector(terr, 0);
        }
I just resets the selector to point at the terrain again after im done repainting the heightmap. It now "knows" the new topography of the terrain.
I should drop it before creating a new right?
Thanks for your help
E
hokan
Posts: 21
Joined: Sat Dec 15, 2012 8:49 am
Location: Russia

Re: Collision with terrain - Height changes collision triang

Post by hokan »

Sorry, if I bump old thread.
Does anybody know more elegant solution to top-message question, except refreshing all triangle selector?
I tired to use

Code: Select all

ITriangleSelector::getTriangles(...)
methods, but they only return copy of triabgle selector's triangles, not pointer to them, and there is (1.8 version) no function to change triangles with affect on selector. Or, what am I doing wrong?

I use this code to get triangles:

Code: Select all

 
scene::ITriangleSelector* pTriangleSelector = pTerrainSceneNode->getTriangleSelector();
core::array<core::triangle3df> triangles;
triangles.set_used(pTriangleSelector->getTriangleCount());
s32 numberOfTriangles = 0;
 
pTriangleSelector->getTriangles(&triangles[0], triangles.size(), numberOfTriangles, boundringBox); // trying to get only in-bounding-box triangles
 
Post Reply