Triangle mesh house glitch

Post Reply
Random#9001
Posts: 10
Joined: Mon Mar 16, 2015 12:59 pm

Triangle mesh house glitch

Post by Random#9001 »

I have a triangle mesh house that I want to use in my game, however if i go to far into a corner I get a break.

I assume this is because some sort of normalized vector is returning zero in the code and this is the place where it breaks:

/**@brief Normalize this vector
* x^2 + y^2 + z^2 = 1 */
SIMD_FORCE_INLINE btVector3& normalize()
{

btAssert(!fuzzyZero());

I am using a character class to move around, and is there a patch or workaround that can fix this?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Triangle mesh house glitch

Post by drleviathan »

http://www.bulletphysics.org/Bullet/php ... =9&t=10458

I think I encountered this same problem when using the btKinematicCharacterController. When I looked into it I discovered that there is a bug in the getNormalizedVector() helper function in btKinematicCharacterController.cpp file. The intention of the function is to protect against trying to normalize zero-length vectors by returning <0,0,0> rather than trying to divide by zero, however it would try to normalize the vector _before_ checking its length. Inside the btVector3::normalized() method there is an assert on zero-length vectors so the helper function would assert before it got to the part where it solved the problem:

Code: Select all

// static helper method
static btVector3
getNormalizedVector(const btVector3& v)
{
    btVector3 n = v.normalized();
    if (n.length() < SIMD_EPSILON) {
        n.setValue(0, 0, 0);
    }
    return n;
}
(BTW, this isn't the only flaw in the btKinematicCharacterController.) My solution was to copy that class into a new one called CharacterController inside my own project and then changed getNormalizedVector() to work as follows:

Code: Select all

// static helper method
static btVector3 getNormalizedVector(const btVector3& v) {
    // NOTE: check the length first, then normalize 
    // --> avoids assert when trying to normalize zero-length vectors
    btScalar vLength = v.length();
    if (vLength < FLT_EPSILON) {
        return btVector3(0.0f, 0.0f, 0.0f);
    }
    btVector3 n = v;
    n /= vLength;
    return n;
}
Random#9001
Posts: 10
Joined: Mon Mar 16, 2015 12:59 pm

Re: Triangle mesh house glitch

Post by Random#9001 »

How did you modify the file and include it into the project? Im a little lost on how that works.

(Nevermind I'm just a bit tired this morning, I just had to create a new class.)(Duh)

Also the fix didn't work completely, I had to actually comment out the btAssert(!fuzzeyZero()) to get it to work.
Im pretty sure I tried that before implementing your fix and it didn't work, so maybe the combination of the two were what finally got it working? I dont know, either way its working now so thanks if it was your answer and that commenting out of the fuzzyzero together. Also will there be problems since I commented out the fuzzyzero?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Triangle mesh house glitch

Post by drleviathan »

In short, I pretend that I wrote btKinematicCharacterController from scratch and then I modify it.

The longer story: In my case I've got a large project with several files that were written from scratch. These live in directories that are completely independent of Bullet and its Demos (I use Bullet as a 3rd party library, the same as OpenSSL or any other specialized lib). Each C++ class has its own .h and .cpp files so I just added two new files for the CharacterController class, which started off as complete copies of btKinematicCharacterController and then I started modifying them.

Note: the btKinematicCharacterController code is provided with a zlib license which means that my modified code needs to retain the license text, with notes that I modified it, however this is compatible with my project and I've kept the license text in place. If it is not compatible with yours then officially you would be required to write your controller from scratch but base it on the btCharacterControllerInterface API.
Post Reply