logo

 

     
 
Home
Site Map
Search
 
:: Bitwise Courses ::
 
Bitwise Dusty Archives
 
 
 

rss

 
 

ruby in steel

learn aikido in north devon

Learn Aikido in North Devon

 


Section :: visual basic

- Format For Printing...

Visual Basic - To Infinity and Beyond!

Dermot gets into orbit … just.
Saturday 7 October 2006.
 

Last month I looked at a simple ’rocket’ represented by a Visual Basic 6 control. While it worked, it did have one or two limitations. First, there was the somewhat rectangular appearance to the control. Let’s face it, most rockets are not square. Secondly, the Earth’s gravity was treated as being constant. Of course, the strength of the Earth’s gravity varies with distance from its centre.

Zip - 7.2 kb
Right-click to download the source code

See also Previous Part of this series

The Rocket control is in fact a type of ’cellular automaton’. In spite of having a complicated sounding name, these things are really very simple. That’s actually the essence of cellular automata. They are simple because they only know about local conditions - the nearby temperature, pressure, gravity and so on. One of the reasons for using cellular automata is that you can directly solve some very complicated problems. The alternative is to work through some of the nastier varieties of equations that lurk in the undergrowth of physics, chemistry and biology.

But another reason for using cellular automata is to explain and understand how things work without getting lost in tedious mathematics. Understanding the basic principles of a situation is often more important than knowing how to solve an equation. The principles of a rocket moving around the Earth are indeed simple - but the resulting motion can be quite complicated. Using the Rocket control, you should be able to experiment with various settings and observe how the motion of the Rocket changes as a result.

Nosing around

A rocket is pointed mainly to reduce air resistance. Once you’re in space it doesn’t matter what shape it is. But in a Visual Basic program, being pointed has another handy little advantage - you can tell the direction the device is going in. In order to get a pointed rocket, I built a (yet another) control called NoseCone. This looks like a simple triangle with the narrowest part of the triangle pointing in the rocket’s direction.

The main Rocket control manipulates the NoseCone control via two methods - Initialise and Rotate. Initialise does what you’d expect it to, assembling a pointed graphic by drawing three lines with the standard Visual Basic Line command. The triangle is drawn so that its centre is in the middle of the control and initially it’s pointing upwards.

It would have been quite easy to rotate the triangle using a subroutine, but I decided to use another feature of Visual Basic - a ’class’. A class is a bit like a control, but simpler. Here, the Vector class represents a point and has two properties, X and Y, representing the point’s co-ordinates.

Rotating a set of points about another point (which is what we’ll do with the nose cone) involves a well known algorithm. You represent the points as Vectors and rotate them using a rotation ’matrix’. The values are stored in internal variables, mvarx and mvary, held as Single (that’s how co-ordinate points are stored in Visual Basic). The method Rotate performs a simple rotation on the point through the angle, theta:

Public Sub Rotate(theta As Double)
Dim tx, ty As Single
tx = mvarx * Cos(theta) + mvary * Sin(theta)
ty = -mvarx * Sin(theta) + mvary * Cos(theta)
mvarx = tx
mvary = ty
End Sub

Gravity’s Rainbow

Once you’ve got the nose cone embedded in the Rocket control, all you have to do is call the Rotate method to reflect the change in angle due to the forces acting on the rocket. As in my previous article, most of the work is done in the Rocket’s Tick method. As well as using the thrust produced by the ejection of the Rocket’s fuel, Tick now takes account of Newton’s inverse square gravitation law to find the local acceleration due to gravity.

While the code is simple, there are a couple of points to notice. The Earth is represented by a circular Shape in the containing Form, so it has to be accessed via the Extender object as we did in the previous article. The dimensions of the Earth are correct but scaled down by a factor of 4,000. The Earth’s real radius is 6371 km while the radius of the Shape is 1593 ’twips’. Unfortunately, the Rocket isn’t to scale - you have to be able to see it!

As I pointed out last month, one of the problems with the first experimental rocket was that if it went off screen, you’d effectively lost it. To get round that problem, I’ve added a number of Public variables to the Rocket control. You can use the Add-In wizard to do this, but it produces something of an overkill. All we want to do here is communicate the internal state of the rocket - its position, speed and the force of gravity on it - back to its container. If you use the wizard to generate these properties, you’ll also get methods to store and restore data to and from disk. Since these properties only have a meaning when the control is active, there’s no point in doing this. A simple Public declaration will do the trick with the timer posting the values out to the containing form.

The use of co-ordinates requires a little explanation as well. Newtonian gravity is essentially a ’central’ force - it points towards the centre. Here, the gravity is provided by the Earth, and so it makes sense to use co-ordinates which define the Earth’s centre to be at (0,0). The setting up of these co-ordinates is done in the Rocket Initialisation method.

Finally, messing about with the Rocket’s angle is tricky. You have to take into account dividing by zero and the fact that the in built function Atn isn’t that bright when it comes to getting the correct sign for thrust angle. I took a simple minded but clear (hopefully) approach to this.


Here’s how the Tick method defines the distances and velocities of the Rocket. The force of gravity always points to the Earth’s centre while the thrust is in the direction of the rocket’s velocity.

Into Orbit

You’ll find the code for the new Rocket and its little universe in the download zip. If you load Orbiter.vbp and run it, you’ll see two text boxes on the top right hand side of the form. These are for entering the Rocket’s thrust (to be precise, the velocity of the exhaust) and the initial angle of the rocket to the vertical. It’s possible to use the control’s property pages to enter these but you’ll find entering data this way quicker for playing around with. They are simple text entry fields and do no error checking - so be careful what you enter. Also, if you want to, you can add other Rocket parameters as well.


The rocket never stops falling. As the rocket increases speed, it lands further away. Eventually, it falls at the same rate as the earth curves - it goes into orbit.

Initially, to check out that everything is OK, just enter 400 in the Thrust field, leave the Angle at zero and click the Start button. You should see the triangular rocket gracefully rise to the top of the form, flip over and just as gracefully crash. There’s insufficient juice to get into orbit. Click the Stop button to reset the program. Now set the Angle to 5 - that is 5 degrees from the vertical - and set the Thrust to 450. Press Start and you should see the Rocket take off and move into what initially seems a nice circular orbit, but crash half way round the globe. Incidentally, this is how Inter-Continental Ballistic Missiles (ICBMs) work but, being peacefully inclined, the Bitwise Rocket is not plutonium tipped. Next, set the angle to 7.44 and try again. You’ll see that the Rocket gets nearly all the way round before crashing (see Forever Falling at the end of this article). Now, set the angle to 7.45 - the Rocket should achieve orbit - at last!


This is what the result looks like. If you set the thrust to around 450 and the angle to around 8 you’ll get a decent orbit. But try experimenting to how the various rocket settings affect the orbit (or lack of it).

If you look carefully at the read-outs and the Rocket, you’ll notice two things. First, the Rocket goes more or less straight up at first. This is due to the Rocket motor which then stops quite soon after launch. Once it’s cut out, the Rocket falls towards the Earth. But if its direction and speed are correct, it will never hit the Earth. Secondly, the read-outs will indicate the velocity and gravity strength that the Rocket feels at any one time. You’ll see that the gravity is not zero, but varies with the Rocket’s position - just like the real thing.

Finally, if you want to head for the stars, just use the initial setting of 560 Thrust and 0 Angle. The Rocket will set out for Infinity, never to return.


FOREVER FALLING

Being in orbit is sometimes referred to as ’free fall’. This is the condition where astronauts (and other objects) float around in an apparently weightless condition. But if you use the Rocket program to launch your test probe into a simple orbit, you’ll see that the rocket still feels gravity - it’s not ’zero-g’ by any means. There are in fact two ways to feel no weight. One is to be in a place a long way from a planet or a star where the gravitational field is very small; the other is to be in a lift where the cable has snapped. In the second case, both you and the lift are falling freely (hence the name). And in the short time before you and the lift become intimately acquainted with the bottom of the lift shaft, you will indeed feel no weight.

A rocket, once its motor has stopped, also falls just like you and the lift. However, the rocket has an advantage over the lift shaft - the Earth’s surface is curved. With increasing speed, the rocket lands a good bit further than it would have done if the Earth was flat. Eventually, if you increase the speed enough, it will just fall back onto the place it started from. If you increase the speed a little bit over that, it will never land, but will go on around again (you may have to duck, though). If there are no other influences, air resistance for example, the rocket will continue to orbit the Earth forever, always falling - and everything on board will have no weight.

AddThis Social Bookmark Button

Forum

  • Visual Basic - To Infinity and Beyond!
    10 October 2006, by Mike Woodhouse

    Given that VB6 is "officially" supported on Vista, while VS2002/3 aren’t and VS2005 has "issues" (what were Microsoft thinking? Or more pertintently why weren’t they thinking?) this series could hardly be more timely. I knew my 15 years of VB hadn’t been wasted!

    • Visual Basic - To Infinity and Beyond!
      10 October 2006

      I must have written the original of this article about 5-6 years ago. Coming back to the code, I’m struck by how simple the code is in comparison to VB .NET or C#. There’s no humungous class hierarchy to work through or any complicated inheritance structure. Yet, it is clearly possible to produce quite complicated programs using a relatively simple syntax.

      I wouldn’t want to use VB6 for the stuff I’m writing now, but I cannot for the life of me figure out what Microsoft is up to here. There’s clearly a market for something simpler than VB .NET and MS isn’t satisfying it.

      It seems to me commercially crazy not to satisfy this need. No wonder MS stock price hasn’t moved in 5 years if it can’t see something as obvious as this.

      The only reason I can think of is that it would put some people in the VB .NET group out of a job.

      • Visual Basic - To Infinity and Beyond!
        20 March 2010, by ibsteve2u

        Where there is a void, nature rushes in: http://www.jabaco.org/

        Open source project to fill the hole Microsoft shot in its foot.


Home