Storage Informer
Storage Informer

Round and Round, In and Out

by on Oct.13, 2009, under Storage

Round and Round, In and Out

If you have been following my blogs, you know that I am porting a DirectX-based demo application to OpenGL as the first phase in the project to play the app on a netbook with an Atom processor. If youve missed a few or would like to read all of my blogs pertaining to this project, I have provided links to the previous entries at the end of this blog.
As of the last blog, I have the objects rendering and the cannon moving in conjunction with the mouse movements. Writing this reminds me that there is one more bit of code I put into the project that I should mention. I dont want the user being confused as to the slight difference between the cannon position and the mouse cursor, so I inserted the last line shown in the code snippet below:

code used to hide the cursor

As you can see, I create a pointer to a Graphics Window object and I set the useCursor option to false. This hides the cursor so the user can focus on where the cannon is positioned, rather than the cursor. Another bit of code I added sets the starting position of the cursor to the middle of the screen. Because I start the application with a window that is 800×600, the beginning position is (400, 300). I originally set this position to (0,0) since when you are setting angles, the center point is (0,0). However, it turns out that this is not correct for setting this function. Heres the line that sets the cursor:

code used to center the cursor

One thing I discovered is that you have to set this cursor position AFTER the window is realized, even though you hide the cursor previous to this.
Now that I have the cannon moving with the mouse and the camera following the cannon, I want to be able to move the cannon to the right and left as well as backwards and forwards. Although the cannon will move to the right or left, the actual movement I want to generate is a circular movement around the central object which is the castle. So the center of this circle is (0, 0, 0). The cannon is originally positioned at (2000, 9000, 180). At first I was thinking that the radius of the circle would be 9000. Remember, in OSG the z axis is up, so the y axis is the distance direction (for lack of a better description). The actual radius of my circle, then, is found by use of the Pythagorean Theory. The equation is below:

The radius then turns out to be approximately 9219.544. Using the radius as the distance from the Castle leaves me with the following code:

Moving the Cannon Right and Left

The change in location around the diameter of the circle with a radius of 9219.544 is determined by dividing 2PI by one less than an arbitrary number (g_scnNumCircleDivisions). The sin of this value is then multiplied by the radius found in the global g_fDistanceFromCastle . The height above ground is retained from the original position of the cannon. This vector is added to the circle center vector to obtain the correct position around the circle. I use g_v3TerrainPos as the center mark because it is centered on (0, 0, 0). To circle right, I add the delta amount to the total angle and to circle left I subtract it from the total.
The code for moving forward and backward is pretty much the same as above except that the changing value is the distance from the Castle (g_fDistanceFromCastle). This section also clamps the distance value so that the cannon cannot get too close or too far away from the castle. The code follows:

Code used to move the Cannon Forward and Back

Now the cannon is pretty flexible so the time has come to animate the bugs in their various paths. That will be the subject of my next blog.

For those of you who missed the previous blogs, here are some links:

Blog 1: "Threaded Graphics to MID: A New Journey"
Blog 2: "From DirectX to OpenGL – Wheels Up"
Blog 3: "OpenGL Graphic Formats and Conversion"
Blog 4: "Scene Graphs and Instancing"
Blog 5: "Load, Follow, or Get Out of the Way"


:, , , , ,

Leave a Reply

Powered by WP Hashcash

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...