[fixed] Triangle selector with animated meshes

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

[fixed] Triangle selector with animated meshes

Post by greenya »

Hello.

The problem is that triangle selector uses only single frame (probably frame #0) for detecting collisions.
Simply start 07.Collision example and move mouse near animated mesh scene nodes.

Image

P.S.: checked with trunk rev. 3990.
Reiko
Posts: 105
Joined: Sun Aug 16, 2009 7:06 am
Location: Australia

Re: Triangle selector uses single frame with animated meshes

Post by Reiko »

Since this bug (and the other bug I reported) affects me, I'm I taking a look at the engine source regarding the triangle selector, since I'm sure you guys are busy and have more important parts of the engine to work on.

The reason for this bug is because, the triangle selector gets the mesh for the current frame when the triangle selector is created. But after that, it keeps the same mesh always.

This can be proven by adding the following to the bottom of if (device->isWindowActive()) in example 7:

Code: Select all

                selector = smgr->createTriangleSelector(node);
                node->setTriangleSelector(selector);
                selector->drop();
This will recreate the triangle selector on every frame. The last node created is the far right one, so "node" here refers to that one (the green fella with the helmet and tail).

With this, you will see the collision detection is working correctly with the animation.

According to CTriangleSelector's constructor, it does indeed use the current frame, and not frame 0, hence why recreating the triangle selector every frame solves the problem.

Now, this bug shouldn't be happening because, there's a function CTriangleSelector::update(), which checks if the mesh in the triangle selector is for a different frame, and updates the mesh in the TriangleSelector if necessary.

Now the problem:
There are three getTriangles() functions:

Code: Select all

//! Gets all triangles.
void CTriangleSelector::getTriangles(core::triangle3df* triangles,
                                        s32 arraySize, s32& outTriangleCount,
                                        const core::matrix4* transform) const

Code: Select all

//! Gets all triangles which lie within a specific bounding box.
void CTriangleSelector::getTriangles(core::triangle3df* triangles,
                                        s32 arraySize, s32& outTriangleCount,
                                        const core::aabbox3d<f32>& box,
                                        const core::matrix4* transform) const

Code: Select all

//! Gets all triangles which have or may have contact with a 3d line.
void CTriangleSelector::getTriangles(core::triangle3df* triangles,
                                        s32 arraySize, s32& outTriangleCount,
                                        const core::line3d<f32>& line,
                                        const core::matrix4* transform) const
But if you look at the code for them, only the first one actually calls update() at the start!
The example 7 uses both the 2nd and 3rd getTriangles() functions at different times, hence, the mesh is never updated.

So you need to add this to the start of the 2nd and 3rd getTriangles functions:

Code: Select all

// Update my triangles if necessary
        update();
Also, the constructors should be updated, so that they will initialize the variable "LastMeshFrame"

For this constructor

Code: Select all

CTriangleSelector::CTriangleSelector(IAnimatedMeshSceneNode* node)
: SceneNode(reinterpret_cast<ISceneNode*>(node)), AnimatedNode(node)
{
I'd recommend maybe this:

Change:

Code: Select all

        IMesh * mesh = animatedMesh->getMesh((s32)AnimatedNode->getFrameNr());
To:

Code: Select all

        LastMeshFrame = (s32)AnimatedNode->getFrameNr();
        IMesh * mesh = animatedMesh->getMesh(LastMeshFrame);
This way the LastMeshFrame is initialized from the beginning. Otherwise, the variable isn't initialized, and update() will always be called an extra time the first time, even if the mesh didn't change (since it checks if the animated scene node's current frame = LastMeshFrame)

For the other constructors, if I understand correctly, I think initializing this variable is not necessary, since it wont actually be used. Still, I think its good practice to initialize it on all constructors anyway, and prevents overlooking it if the code is touched again later.

Anyway, that will solve this problem. My problem still isnt solved yet, will look into that now and post in the thread I created if I find something.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Triangle selector uses single frame with animated meshes

Post by hybrid »

Hmm, okay. That explains why I always thought that this is working. It was just bugged due to the changes I made to triangle selectors a few weeks ago. Anyway, I also changed the initialization in 1.7 which still had a rpoper update for all triangle selector calls. So I'll call this one fixed and we have to find out why your setup is different from this one.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: [fixed] Triangle selector with animated meshes

Post by robmar »

So what´s the status with calling update, is it necessary or handled internally under 1.7.3 and 1.8?
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Re: [fixed] Triangle selector with animated meshes

Post by christianclavet »

Wow! And all the time I was thinking that feature was not supported and I had to resort to "collision boxes" attached to the bones...

So if this is "fixed" we could check collision on a skinned animated mesh?
Done some tests on skinned animated meshes (doors) (Irrlicht 1.8.0) and the collision is only detected at the idle frame (when I created the collision triangles) Could we "buffer" thoses triangles selectors (metatriangleselector style) so when we reach a certain frame, there would not be CPU overhead to get to the collision triangle?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [fixed] Triangle selector with animated meshes

Post by CuteAlien »

I think (not tested right now) it should update when you use node->setTriangleSelector for the triangleselector which you have created.
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
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: [fixed] Triangle selector with animated meshes

Post by robmar »

the docs say that update must be called after animation frame changes...
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [fixed] Triangle selector with animated meshes

Post by CuteAlien »

CTriangleSelector::Update is called automatically each time getTriangles is called now (that was the bugfix mentioned above).
Not sure right now - where in the docs does it tell that some update should be called manually?

@christianclavet: There's no cache so far. Caching the 'Triangles' in CTriangleSelector might indeed be an useful feature. But you wrote like it does not work at all right now?
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
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: [fixed] Triangle selector with animated meshes

Post by robmar »

saw it in the header last week... I´ll try to find it.
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Re: [fixed] Triangle selector with animated meshes

Post by christianclavet »

Hi!

@CuteAlien:
Perhaps I do it all wrong. I did not manually update the triangle selector in 1.8.0 and the collision was not working on an animated model (door was skinned and animated to open). Perhaps if I update manually the triangle selector of all my models at each update would fix the problem.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [fixed] Triangle selector with animated meshes

Post by CuteAlien »

There should be no need to update manually. It's just important to call setTriangleSelector for the node I think. At least I see no reason for a manual update when looking at the code, I guess I'd have to write a test-case to be certain.
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
Post Reply