0

So for a quick sample in pictures:

This is normal: normal

this is after rotating 180 deg on either the X or Y axis: rotated

I don't see why this is happening at all. I'm using OpenTK to render a simple Bullet physics scene. The code is straight forward and it almost seems like there's something wrong in the way the matrix is handled. It's straight-forward render code:

        GL.PushMatrix();
        GL.MultMatrix(Body.MotionState.WorldTransform.ToArray());
        GL.Scale(HalfX * 2, HalfY * 2, HalfZ * 2);
        GL.Color4(Color4.Blue);
        GL.Begin(PrimitiveType.Lines);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(0, 0, 10);
        GL.End();
        GL.Color4(Color4.Yellow);
        GL.Begin(PrimitiveType.Lines);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(0, 10, 0);
        GL.End();
        GL.Color4(Color4.Green);
        GL.Begin(PrimitiveType.Lines);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(10, 0, 0);
        GL.End();
        if (Body.ActivationState == ActivationState.ActiveTag)
        {
            GL.Color4(Color4.Blue);
        }
        else if (Mass == 0)
        {
            GL.Color4(Color4.Red);
        }
        else
        {
            GL.Color4(Color4.Green);
        }
        model.Draw();
        GL.PopMatrix();

I've tried breaking it down to it's components: the translation vector is fine, scaling is fine, rotating on the Z axis appears fine... it's when you add rotations on the X or Y axis that it starts flying. I have console output going: the box is at exactly 6.9999 on the Z axis in both images.

Where am I going wrong? What am I missing? How do I fix this?!

mcmonkey4eva
  • 1,359
  • 7
  • 19
  • Can you show the code that calculates WorldTransform? (Unrelated to the bug, but you only need to call `GL.Begin()`, `GL.End()` once.) – The Fiddler Oct 22 '14 at 09:29
  • I cannot, that's calculated by the Bullet Physics Engine. I'd venture a guess that it's accurate, being a major engine. Would it help if I showed the decomposed-matrix code? (IE, the code where I took each component of the vector and applied it separately)? – mcmonkey4eva Oct 22 '14 at 23:39
  • So Bullet calculates a matrix and you pass that directly to OpenGL via `GL.MultMatrix()` is that correct? In that case OpenTK does not modify your matrix, it simply passes it directly to the driver - so the problem could be a mismatch between Bullet and OpenGL. Showing the decomposed matrix ops might help understand what is going wrong. – The Fiddler Oct 23 '14 at 09:44
  • To clarify what's going on, two things: A: a gif of the rotation: http://i.imgur.com/bHREzMH.gif B: the decomposition code: https://gist.github.com/mcmonkey4eva/c7d11eb8f19928a8dc5b - also, I'd like to reiterate that pos.Z is definitely remaining at 6.999 (bouncing slightly up then back down during the rotation process) – mcmonkey4eva Oct 23 '14 at 23:21
  • It looks like it's rotating around a point a few units up the Z axis, like there's a hidden translate... but there isn't. – mcmonkey4eva Oct 23 '14 at 23:31
  • Here's full source if it helps: https://github.com/mcmonkey4eva/BulletSharpTest - you can run it and see it happen yourself – mcmonkey4eva Oct 25 '14 at 05:14
  • Okay so... `PushAttrib(AttribMask.AllAttribBits); PushMatrix();` in my /TEXTFONT LOADING CODE/ fixed it. Somehow, some weird attribute set in the code that loads fonts to later render made GL.Rotate rotate around (0, 0, 1) instead of (0, 0, 0)... OpenGL sure is temperamental... – mcmonkey4eva Oct 25 '14 at 19:51

1 Answers1

1

Okay so... PushAttrib(AttribMask.AllAttribBits); PushMatrix(); in my /TEXTFONT LOADING CODE/ fixed it. Somehow, some weird attribute set in the code that loads fonts to later render made GL.Rotate rotate around (0, 0, 1) instead of (0, 0, 0)... OpenGL sure is temperamental...

So the lesson here is... never assume unrelated code is truly unrelated when dealing with OpenGL.

mcmonkey4eva
  • 1,359
  • 7
  • 19
  • Glad to see that you've found the culprit. In general, things are much better in modern OpenGL which no longer uses the matrix stack. – The Fiddler Oct 26 '14 at 16:57