Wednesday 23 February 2011

Week 22 - Proper Rotation!

This week I was focusing on attempting to get the planets to rotate properly around their tilted axis, instead of around the global z axis. I have to admit, I didn't think I would be able to fix this - but in the end, I managed it!

I started by fixing the rotateToAxis method. The intention of the method was to rotate the axis that the planet spins around (the z axis) by the number of degrees of axis tilt. However, if you remember from the previous blog, I couldn't get this to work at all and the planets disappeared when I tried to use it. After last weeks meeting with Rob, he pointed out that the vector being used would have to first be normalised, or the whole thing wouldn't work - the major point I had missed. Here is the reworked version of this method:


The dot product and cross vector methods called here have also been slightly adapted - they now only require one vector to be passed down - this simplifies things from the previous technique.

Once I had this method working, I still had the same issue - what was the vector that needed to be passed to this method? I knew it would be the vector that ran at the angle of the axis tilt, but that was it. Then the lightbulb moment - the rotation_angle variable in this method was the axis tilt.

Once I had realised this, I removed the call in initGL that rotates the planet by its axis tilt - after all, the rotateToAxis method would take care of this.


If you look back to the rotateToAxis method, you will see that there are commented out "cout" calls - these print the variables like rotation_angle to the console, so I can see what each variable is at each part of the calculation. I knew that the vector was rotated around the Z axis, so the vector I needed to input would be of the form (1,1,?), with the question mark changing for each planet. I discovered that increasing this number decreased the rotation angle, and decreasing the number increased the angle. So, with a lot of trial and error, I matched up the rotation_angle with the axis tilt value by adjusting the z value of the input vector. It's difficult to explain, but here are the input vectors for each planet, that might help to clear things up:


Once I had done this, all I needed to do is add a call to rotateToAxis before each planet is drawn:


and Voila! Planet's that rotate about their own axis. I'm really pleased I got this fixed, it gives the program a much more authentic look to it. The only other thing I added this week was a simple boolean switch to remove or add the stars in the background. This is an option on the menu now (hopefully you can also see that the planets are correctly tilted!):


And finally... I let the program run for a while (check the date!) and managed to get a snapshot with all the planets on screen. I'm a little unsure if Uranus' ring spin is correct, but this will be an issue with the model not the calculations if it isn't, I need to check that with Rob.


That's all this week. I just need to implement the 'planet views' option that will zoom to a planet and move the camera with it's orbit, and the program is completed! If I manage that by the end of the month, then I'll have hit my deadline.

Wednesday 16 February 2011

Week 21 - Rotation and Menus

This week I have been working on getting the planets rotating at the correct speeds and around their axis - quite difficult! - and implementing a pop up menu system, moving over some of the functionality I had coded into the keyboard controls.

Firstly, planet rotation. The first thing I had to do was work out a way to animate the rotation - this was fairly simple, and required a glRotatef call before drawing the planet in myDrawGLScene. For example, the Earth would rotate by "earthSpin" degrees every frame as the program runs. The variable earthSpin (there is a unique Spin variable for every planet) is updated in the method planetRot, which assigned the appropriate rotation value to each variable.

In order to calculate the correct rotation values, I needed a few things - firstly, how long it takes each planet to rotate 360 degrees, which I found at this website. Secondly, I needed to calculate how many frames it took the Julian day to increase by 1 as the program ran. Since the Julian day is currently set in the program to increase by 0.05 every frame, that means that it takes 20 frames for the day to increase by 1. Using this information I was able to work out the correct rotation values for each planet - the math is explained in more detail in the comments beside the code in the following screenshots of the planetRot function (click to expand):



each value has to be multipled by speedFactor, so that the rotation increases as the speed of orbit increases.

The next major problem was rotation around the axis of the planet. Each planet had already been rotated slightly by it's axis value - these rotations were all done around the Z axis of each planet, basically rotating around a vertical axis instead of the off centre one it needed to go around. This gave me a major headache, as opengl only allows for transformations around the X, Y and Z axes. I spent many hours reading information on matrix tranformations and aligning an axis to a vector but to be honest, I didn't really understand it too well. From the information and code examples I had come across, I developed the following function:


...which didn't work. It is meant to align the axis to a given vector, but I realised that the vector I was aligning it to (the vector position of the planet) would not be at the correct axis tilt anyway, and I have no idea how to get that vector. I have kept the method in the program for now, and it is something I will need to discuss with Rob tomorrow.

The solution I have been working on since that method failed is rotating by the axis value before the planet is drawn, then correcting that afterwards, like so:


This does seem to improve the look of the planets rotation, but I'm not sure if it's correct.

Now, onto opengl menus. The creation of the menu is all handled by a method called CreateMenus() in the main.cpp file. The handling of what each menu option does is taken care of by a method called myMenuResponder(int selection)  in the DrawSolarSystem class. The menu handles:

  • Listing the keyboard controls (submenu)
  • Controlling the scale of the distance between the planets and the Sun (submenu)
  • Controlling the scale of the planets (submenu)
  • Zooming into a view for each planet (submenu)
  • Changing the view between horitzontal and vertical
  • Switching to full screen mode
  • Resetting the scene to the load up config
  • Exiting the program
Here is the CreateMenus method that shows how all these options are programmed:



The very last line tells the program to call this menu whenever the right button of the mouse is clicked - this is how the user accesses it. Here is a small section of the myMenuResponder method. Each option in the above code is given a number, which is how these two methods are linked.


And finally, a couple of screenshots that demonstrate the program in vertical / horizontal view mode, and with the pop up menu up:



Note that the planets have been increased to their maximum scales, with the outer four planet's distances also decreased to their minimum value (as close as they can get to the Sun).

That's all for this week. Hopefully I'll be able to address the axis rotation and lighting/shadow issues for next week.

Wednesday 9 February 2011

Week 20 - User Interaction

Following a short break, it's back to work on the Honours Project. I'm aiming to have the program fully finished by the end of February, the features I'm aiming to improve/add are:

  • More user interaction. This has been partly addressed this week.
  • Improve lighting and shadows. Currently the planets don't have a 'dark side' where the Suns light doesn't hit them. I've no idea why this is as the lights are set up in the centre of the Sun, so I may not be able to solve this.
  • Implement pop up menus. All the user interaction is keyboard and mouse based at the moment, and there's too many functions going on to have them all implemented by a key press - I want to move some stuff over to menus, and also include a pop up with control details for the keyboard functions.
  • Rotate planets as they orbit. Currently the planets aren't rotating as they move, and implementing this has proved more difficult than I first thought it would be.
  • Improve movement controls. This is the least important update, so it may get cast aside if I run out of time, but I would like to improve the way the user moves around the 3D solar system - it's not brilliant but it works, so I wouldn't be too worried about leaving it if need be.

So onto the improvements in the user interaction that have been added this week. The major cosmetic change is the addition of a speed control. I had previously attempted to add this in using a variable called speedFactor which was adjusted everytime the user pressed a key - but could not get it to work. I solved this by moving the code which updates the julian date:


   julDate += (0.1 * speedFactor);

from the updateMercPosition function that it was in, into the main myDrawGLScene method, which fixed the issue of the speedFactor variable not being updated by a user key press. Now whenever the user presses 'm' the speed is increased (to a maximum of 25x), with n decreasing the speed (minimum of 1x). The program launches in 1x speed, and can be increased by the user to 25 times the launch speed. The speed is displayed on screen, beneath the two date displays like so:




The second piece of functionality that has been added is the ability for the user to increase the size of the individual planets. This was something discussed in my last meeting with Rob, and I decided that in order for the user to get a good look at the models (especially the smaller inner four planets) without lots of zooming and panning, this would be a great feature to implement. At the moment, only the planet Earth has this fucntionality coded, and it is controlled by the 'u' (to increase size to a max of 8x the original) and 'i' (to decrease size) keys. This is a feature I intend to move to the menu system, simply for the reason that I will run out of keyboard keys if I use two per planet. In order to code this, I introduced a variable earthSize, which is controlled in a similar way to the speed variable, increasing or decreasing the variable on a key press. I had to move the scaling function for the model from InitGL, where it was located in the display list, into myDrawGLScene, in order for it to update correctly whilst the program is running.


The final piece of functionality that I added this week was scaling the distances from the Sun. There are two options for the user in this case - they can increase/decrease the distance of the inner four planets from the Sun (currently using the 'f' and 'g' keys) or increase/decrease the distance of the outer four planets from the Sun. This gives the user the option to see the planets bunched in much more closely than the correct scale that the program launches in. This is controlled by two variables - innerScale and outerScale, with a default setting of 16 (purely for aesthetic reasons, although this could change). The positions of the planets that are defined in earth Update..Position method are multiplied by these variables, which are increased or decreased by a key press. The increase value for both variables is limited to 20, whilst different limits are applied for the inner and outer decrease values - in the inner case, so Mercury does not disappear into the Sun model, and in the outer case, so Jupiter and Mars' orbits don't collide.
The image below shows the outer planets decreased to their lower limit, Jupiter appearing very close to the inner four planets.


That's all for this week.