recomputing an AABB for a model (opengl)

Started by
4 comments, last by lexington 8 years, 10 months ago

Hey all,

I have been working on creating an AABB class that can recompute the AABB of an object with the original AABB points when the model is first loaded in. I have been using the book "Real Time Collision Detection to try and create this function but I'm running into an error.

When rotating the my object I first use Euler angles which I then turn into a quaternion which from then I use the quaternion to create a 4x4 matrix for my Model matrix and I turn it into a 3x3 matrix to recompute the Min/Max. If I only change one element of rotation (say I just change the pitch to 90 to my model is facing up) then the new value are correct but as soon as I change two elements of the Euler angles (say pitch 90 and yaw 90) then values become incorrect. (I should note that though debugging I have noticed that the values become worst when both pitch and yaw are around 90 though the values still don't seem accurate if say pitch was 90 and yaw is 45.)

Below I will post the code for the player changing the Euler angles, the taking of the Euler angles into a quad and the function that does the recomputing of the min/max. (note the Euler is in radians )

code for the changing of the angle


  if(turnkeys[0] == true && rotation.x < glm::radians(78.0f) || rotation.x > glm::radians(270.0f) && turnkeys[0] == true)
  {
    rotation.x += 1.0f * deltaTs; //change the pitch
  }
  else if(turnkeys[1] == true && !(rotation.x < glm::radians(280.0f) && (rotation.x > glm::radians(90.0f))))
  {
    rotation.x -= 1.0f * deltaTs;
  }
  
  if(turnkeys[2] == true)
  {
    rotation.y +=  1.0f * deltaTs;; //change the roll
  }
  else if(turnkeys[3] == true)
  {
    rotation.y -= 1.0f * deltaTs;
  }

how I change the Euler angles into a quaternion (glm)


myquat = glm::quat(rotation);

and lastly the code that i use to recompute the min/max (with help from real time collision detection)


 glm::mat3 rotationmatrix = glm::toMat3(rotation);

    for(int i = 0; i < 3; i++)
    {
      currentmin[i] = 0.0f;
      currentmax[i] = 0.0f;

      for(int j = 0; j < 3; j++)
      {
        float e = rotationmatrix[i][j] * orimin[j];
        float f = rotationmatrix[i][j] * origmax[j];
        if(e < f)
        {
          currentmin[i] += e;
          currentmax[i] += f;
        }
        else
        {
          currentmin[i] += f;
          currentmax[i] += e;
        }


      }
    }

Any help you can provide will be greatly appropriated and any additional information on the maths side will be greatly welcomed.

Thanks for your guys time and help in advance smile.png

PS: sorry if it would have been better if i posted this in the open gl part i was not sure which place would be better considering its both maths based and with open gl functions

Advertisement

Hey all,

I have been working on creating an AABB class that can recompute the AABB of an object with the original AABB points when the model is first loaded in.

To clarify: you have a set of points in 3D, and you want to compute the minimum axis aligned box that encloses them? Then you can easily compute that by first making the AABB span an "infinitely negative" size, and then iterating over the points, enclosing them one by one. Here is some code that does that: https://github.com/juj/MathGeoLib/blob/master/src/Geometry/AABB.cpp#L93

I'm not sure how rotations, euler angles and quaternions relate to that however, so perhaps that is not the problem, but instead you want to rotate an AABB, and then recompute a new AABB that encloses the rotated AABB? If so, then you can achieve that as shown by the code in here: https://github.com/juj/MathGeoLib/blob/master/src/Geometry/AABB.cpp#L471 .

@Clb

Yer the idea is to wrap the rotated AABB in a new AABB.

orimin and orimax is the first computed AABB when the model is loaded in (in local space)

I hope that clarify's it.

arr don't worry I realised that the function is based of column major and the mat3 from the quaternion is row major so I inversed the matrix and it and the values are correct.

feel free to correct me if im missing something


... column major and the mat3 from the quaternion is row major so I inversed the matrix

If it's only a matter of column- vs. row-major, you shouldn't need to invert the matrix - transposing should be sufficient.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

If it's only a matter of column- vs. row-major, you shouldn't need to invert the matrix - transposing should be sufficient.

arr dumb mistake

thanks smile.png

This topic is closed to new replies.

Advertisement