Player collision fails for custom scene node

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
Slywater
Posts: 7
Joined: Fri May 25, 2018 8:09 am

Player collision fails for custom scene node

Post by Slywater »

Hello.

I have attempted to make a mesh from triangles, but when I come to add the collision selectors, I fail to collide with the the mesh.

Here is the relevant code:

Code: Select all

            buf->recalculateBoundingBox();
            Mesh->recalculateBoundingBox();
            IMeshSceneNode* myNode = smgr->addMeshSceneNode(Mesh,0,IDFlag_IsPickable);
            myNode->setMaterialFlag(EMF_BACK_FACE_CULLING, false);
            myNode->setMaterialFlag(EMF_FRONT_FACE_CULLING, true);
            myNode->setMaterialFlag(EMF_LIGHTING, false);
            myNode->setMaterialFlag(EMF_NORMALIZE_NORMALS, true);
            ITriangleSelector* selector2 = 0;
            selector2 = smgr->createOctreeTriangleSelector(
            myNode->getMesh(), myNode, 12);
            myNode->setTriangleSelector(selector2);
            myNode->setDebugDataVisible(EDS_BBOX_BUFFERS);
            IMetaTriangleSelector* tSelector2 = smgr->createMetaTriangleSelector();
            tSelector2->addTriangleSelector(selector);
            ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
            tSelector2, cam, vector3df(2.5,10,2.5),
            vector3df(0,gStrength,0), vector3df(1,7.5,1));
            //selector->drop();
            cam->addAnimator(anim);
            //anim->drop();
I have consulted the API, checked the boundary boxes, and I have set it up just like my other meshes (which work). I suspect the collision if failing because the triangle selector isn't picking up the Scene Node made purely of triangles, but that seems counter-intuitive. For clarity, the meshes themselves are small triangles, which are combined to make the scene node.

I hope that's enough info,
Thanks.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Player collision fails for custom scene node

Post by CuteAlien »

Not easy to tell from small code-snippet and without test-case, but if you are still using Irrlicht 1.8 (and not svn trunk version of Irrlicht), there might have been some bug with MetaTriangleSelector in combination with OctreeTriangleSelectors. Been a while since I fixed that one, so not 100% sure right now if that could have causes problems in this case.
But just to test:

- You don't need a MetaTriangleSelector if you only have a single triangle-selector anyway. It's just there to combine a bunch of triangle-selectors into one. So for a start keep it simple and just use selector2 and see if that works
- Try using a simpler triangle selector. CreateTriangleSelector - which is not optimized at all - but likely has no problems in combination with MetaTriangleSelector.
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
Slywater
Posts: 7
Joined: Fri May 25, 2018 8:09 am

Re: Player collision fails for custom scene node

Post by Slywater »

Thanks for your speedy reply.

I tried the code both with a regular triangle selector fed into the metatriangle selector, and with just the triangle selector put into the camera animator.
For both, I can no longer collide with the rest of the map as I'm just using selector2 not selector1 (which means it should be working right?) Unfortunately, it did not fix the collisions for the custom scene node. You mentioned that there was a fix for selectors in the trunk version of Irrlicht (I am using 1.8.4). Even then, I removed the metatriangle selector altogether.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Player collision fails for custom scene node

Post by CuteAlien »

Does your custom scene node have mesh? The triangle-selector needs a mesh to create triangles. Alternatively you could write your own ITriangleSelector which returns triangles.
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
Slywater
Posts: 7
Joined: Fri May 25, 2018 8:09 am

Re: Player collision fails for custom scene node

Post by Slywater »

Well, it isn't so much a custom scene node as a class, but it's a mesh which is then put into a animated scene node, which is then fed into the triangle selector. The mesh is created as shown below (this is most likely the source of the problem):

Code: Select all

            FastNoise tNoise; // Create a FastNoise object
            tNoise.SetNoiseType(FastNoise::SimplexFractal); // Set the desired noise type
            tNoise.SetFrequency(0.004);
            tNoise.SetFractalOctaves(6);
            tNoise.SetFractalLacunarity(2.0);
            tNoise.SetFractalGain(0.5);
            tNoise.SetFractalType(FastNoise::Billow);
            SMesh* Mesh = new SMesh();
            SMeshBuffer *buf = 0;
            buf = new SMeshBuffer();
            Mesh->addMeshBuffer(buf);
            buf->drop();
            int vCount = bSize*bSize*6;
            buf->Vertices.reallocate(vCount);
            buf->Vertices.set_used(vCount);
            int i = 0;
            for (int z=0; z<bSize; ++z)
            {
                for (int x=0; x<bSize; ++x)
                {
                    buf->Vertices[i] = S3DVertex(nrX+x*scale*2,1000+tNoise.GetNoise(nrX/scale/2+x,nrZ/scale/2+z)*100,nrZ+z*scale*2, 0,90,0, SColor(255,255,0,255), 0, 0);
                    buf->Vertices[i+1] = S3DVertex(nrX+(x+1)*scale*2,1000+tNoise.GetNoise(nrX/scale/2+x+1,nrZ/scale/2+z)*100,nrZ+z*scale*2, 0,90,0, SColor(255,0,255,255), 0, 0);
                    buf->Vertices[i+2] = S3DVertex(nrX+x*scale*2,1000+tNoise.GetNoise(nrX/scale/2+x,nrZ/scale/2+z+1)*100,nrZ+(z+1)*scale*2, 0,90,0, SColor(255,255,0,255), 0, 0);
                    buf->Vertices[i+3] = S3DVertex(nrX+(x+1)*scale*2,1000+tNoise.GetNoise(nrX/scale/2+x+1,nrZ/scale/2+z+1)*100,nrZ+(z+1)*scale*2, 0,90,0, SColor(255,0,255,255), 0, 0);
                    buf->Vertices[i+4] = S3DVertex(nrX+x*scale*2,1000+tNoise.GetNoise(nrX/scale/2+x,nrZ/scale/2+z+1)*100,nrZ+(z+1)*scale*2, 0,90,0, SColor(255,0,255,255), 0, 0);
                    buf->Vertices[i+5] = S3DVertex(nrX+(x+1)*scale*2,1000+tNoise.GetNoise(nrX/scale/2+x+1,nrZ/scale/2+z)*100,nrZ+z*scale*2, 0,90,0, SColor(255,255,0,255), 0, 0);
                    i+=6;
                }
            }
            buf->Indices.reallocate(vCount);
            buf->Indices.set_used(vCount);
            for (int j=0; j<vCount; ++j) {buf->Indices[j]=j;}
            buf->recalculateBoundingBox();
            Mesh->recalculateBoundingBox();
            IMeshSceneNode* terrain = smgr->addMeshSceneNode(Mesh,0,IDFlag_IsPickable);
            terrain->setMaterialFlag(EMF_BACK_FACE_CULLING, false);
            terrain->setMaterialFlag(EMF_FRONT_FACE_CULLING, true);
            terrain->setMaterialFlag(EMF_LIGHTING, false);
            terrain->setMaterialFlag(EMF_NORMALIZE_NORMALS, true);
            ITriangleSelector* selector2 = 0;
            selector2 = smgr->createTriangleSelector(
            terrain->getMesh(), terrain);
            terrain->setTriangleSelector(selector2);
            terrain->setDebugDataVisible(EDS_BBOX_BUFFERS);
            //IMetaTriangleSelector* tSelector2 = smgr->createMetaTriangleSelector();
            //tSelector2->addTriangleSelector(selector2);
            ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
            selector2, cam, vector3df(2.5,10,2.5),
            vector3df(0,gStrength,0), vector3df(1,7.5,1));
            //selector->drop();
            cam->addAnimator(anim);
            //anim->drop();
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Player collision fails for custom scene node

Post by CuteAlien »

Doesn't really look wrong on first view. Especially if it works for other nodes.
Can you create a test-program from that which I can compile? (Otherwise I have to write my own example from that snippet and no idea if I can reproduce it like that). If it's too long to post on forum then you can put it on some service like bitbucket or github (but it should be reduced to minimum to reproduce the problem).

edit: Just noticing - culling front-faces instead of backfaces is a little bit strange - you should order your vertices or indices the other way round. I think triangles collide only from one side - so in this case they likely collide from below - not from above. Note that the normals play no role in this - the vertex-order (aka the indices) do matter (clockwise vs anti-clockwise).
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
Slywater
Posts: 7
Joined: Fri May 25, 2018 8:09 am

Re: Player collision fails for custom scene node

Post by Slywater »

Luckily, I won't need to send any code, as I took a look at the bounding box and noticed that collisions only took place on the underside of the mesh just like you predicted. Inverting the vertex order did indeed solve the problem, and in turn flipped the faces back to normal. I'll remember that collisions only take place on one side from now on.

Thanks again!
Post Reply