I posted this question in the GAMBAS user mailing list, archived here: http://old.nabble.com/gb3%3A-OpenGL-Rotate-and-Translate-logic-td33886871.html I’ll completely rewrite it here out of respect (link provided for reference).
I have a top-down game where all objects, including the camera, are described by (x,y) coordinates. The camera is always at the center of the render window, though the camera’s coordinates are arbitrary (Camera.WorldX, Camera.WorldY). The camera has an orientation (Camera.Orientation), which rotates the game world much like this video (Contra III Stage 2, SNES):
I’d like to set up the matrix transformations in advance (zoom, translation, rotation), so that all subsequent rendering operations can use objects’ native world coordinates and appear in the correct positions (camera in center of the screen, world rotated by the camera’s orientation). I’d like the matrix transformations to do the work so I can just use the raw game object coordinates, basically.
My code works fine with no rotation:
ScreenOffsetX = sWidth / 2 / 128 / Zoom.Current
ScreenOffsetY = sHeight / 2 / 128 / Zoom.Current
Gl.LoadIdentity()
Gl.Scalef(128 * Zoom.Current, 128 * Zoom.Current, 1)
Gl.Translatef(- Camera.WorldX + ScreenOffsetX, - Camera.WorldY + ScreenOffsetY, 0)
http://eightvirtues.com/sanctimonia/misc/Everything%20But%20Rotation.ogv
I need to combine what works in the video above with rotation around the center of the screen (camera as origin), but have been unsuccessful. I’ve been working on this for days and am starting to go crazy. I’ve researched 'til my eyes have bled and used trial and error for hours on end.
As mentioned in the GAMBAS mailing list, I have functions to rotate an arbitrary point about an arbitrary origin, and I suspect that I may need to use these functions to translate the rotated matrix along its pre-rotated (x,y) path so that the “camera” is centered on the render window post-rotation. Any help would be appreciated. I’m dying here.