Saturday, January 30, 2010

Agent Rotation: Groundwork

After I figured out the two-dimensional rotation math, I changed my agent's "direction" arrow.  Initially, it was a collection of lines (GL_LINES) which were rotated around the z axis by the required angle, using the OpenGL rotated function.  Instead of using my home-grown rotation math to determine the new vertices for each endpoint of each of the arrow's lines, I decided to keep it simple.

I still need to update the direction vector manually.  However, I can use OpenGL's functions to rotate as many points at a time as I require (that is, the entire agent 3D object).  In order to determine that my vector rotation and the OpenGL rotation were in-sync, I decided to draw a representation of the direction vector in the world coordinate space (translated to the origin of the agent object) as a simple line, and to draw a more complex agent object in its own coordinate space (rotated with OpenGL).  Here are the steps for each frame:
  • Initialize the canvas
  • Translate to set the camera's position
  • Draw the board, using world coordinates
  • Push the OpenGL transformation matrix (this allows us to draw each agent individually, without regards to the others)
  • For each agent...

    • Translate to the agent's position
    • Push the transformation matrix again (this allows us to draw the direction vector after the object itself has been drawn)
    • Rotate around the z axis, to put OpenGL into the agent's coordinate space
    • Draw the agent object
    • Pop the previously-pushed transformation matrix, to return to world coordinates (centered at the origin of the current agent)
    • Draw the direction vector
    • Pop the transformation matrix again, to center once again at the world's origin (permitting the next Translate to move the coordinate system center to the origin of the next agent)
What I initially found was that the direction vector and the object did not stay aligned.  The reason for this is that OpenGL's rotation functions are calculated in degrees, while the standard library sin() and cos() functions work with radians.  I converted the angle in my own rotation equations from degrees into radians, in an attempt to keep my OpenGL code as clean as possible.  The resulting conversion ensured that my OpenGL-rotated object remains aligned with the object I rotated myself.

No comments:

Post a Comment