Tuesday, 31 January 2012

Angle between two vectors

Another use for inverse trigonometric functions is finding the angle between two vectors. Again there is more than one way to do it, and again the most straightforward approach is often not the best.

Monday, 30 January 2012

Inverse trigonometry

The functions asin, acos and atan are together the three principal inverse trigonometric functions; that is they are the inverse of the sine, cosine and tangent functions, the commonest trigonometric functions that describe all the ratios of the sides of a right-angled triangle.

Friday, 27 January 2012

New screenshot

A screenshot from something I'm working on. Yes, it has circles in it.

And a tip, if you're developing on a Mac. Turn on Web Sharing (under Sharing in System Preferences), then set the path Flash builds the SWF and HTML to something like "../../Sites/xxx", where xxx is the file name of each. Then you can access it in the browser using the IP address of your machine and the HTML file name, as above. No need to upload it to a separate site for network testing. It's still a local file so will work locally in the debugger or standalone Flash player just fine.

Thursday, 26 January 2012

Circle-line intersection

Yesterday's method for determining whether a circle crosses a line can also be used to find where the crossing points are. The approach uses the distance calculated in the hitLine function with some simple circle geometry.

Wednesday, 25 January 2012

Circle-line incidence

Problems involving circles and lines can be straightforward, if handled the right way. The main reason for this is the fact that the distance from a circle to a line is just the distance from its centre to the line minus the radius. I.e. if a circle radius 100 touches a line the circle's centre must be 100 units from the line.

Tuesday, 24 January 2012

More on circles

Yesterday I described circle-point and circle-circle intersection tests. These are very straightforward to do, and it is interesting to look at why this is so, as it can help solve other problems with circles.

Monday, 23 January 2012


Circles are used for many things in 2D games. Not only for things that are inherently circular (wheels, balls) but for many effects and to describe many situations. For example the range of a gun is often a circle, or at least circular.

Friday, 20 January 2012

Fast path member access

I noted on Wednesday that accessing members by repeatedly doing a lookup in a Vector is slower than doing that lookup once. But it was slower even than I expected. The code was doing the lookup eight times more than necessary, but was over twelve times slower (i.e. it took over twelve times as long). So I forked the code and re-wrote the tests to find out what was going on. The results of this are especially interesting.

Thursday, 19 January 2012

Wednesday, 18 January 2012

Dereferencing array members

The title refers to code like this

iSum += aData[iIndex];

Looking up in an Array (or Vector) requires the code to find the address of the object or value requested then retrieve whatever's at that address. It might also check whether it exists, and whether it is in range (and if not whether it should grow the Array or Vector to accomodate it).

Tuesday, 17 January 2012

Accessing members by name

One sometimes misunderstood feature of ActionScript is that as well as accessing members of a class directly and using accessors it's possible to do so using the name of the member, passed as a String to the class. Misunderstood as there is a right way to use this and a wrong way to use this. And the wrong way is incredibly slow, so much so it should be avoided at all times.

Monday, 16 January 2012

More on angles

In a recent post I asserted that angles are rarely needed for game mathematics. There are a few exceptions but far fewer than many people realise. But it's easy to doubt this assertion. What is there theory behind it? Is there a way to check when angles can be avoided? Not only can both questions be answered but the answer to them is the same.

Friday, 13 January 2012

setter perfomance test, conclusions

So far the accessor tests I've been doing have only been of getters, or more generally of accessor functions for retrieving members from classes. The obvious question is whether setters have the same performance penalty. The answer is yes, they do.

Thursday, 12 January 2012

getters and built-in classes

Following on from yesterday's post, an obvious question is whether getters and setters are used for built in classes. The answer is yes, but not for all of them, and it is important to know which.

I won't post code for this, as the code is straightforward but lengthy, and is easily inspected at wonderfl where I've created this program to demonstrate it. It defines two classes, one derived from Vector3D, one derived from Bitmap. The purpose of each is to compare accessing the built-in members of the classes with members added by the derived classes.

Wednesday, 11 January 2012

getters and setters are evil

ActionScript has a feature which I have not seen in any other programming language: getters and setters. These are special member functions which when used can be called like direct member access. In some ways this is like operator overloading in C++, except C++ does not allow the '.' operator to be overridden. Having seen how this works in ActionScript it seems like one innovation other languages should not copy.

Tuesday, 10 January 2012

Accessors are slow

This is the most important performance optimisation I learned writing ActionScript. It provides a significant performance boost, and in class-based code arises more often than almost all other optimisations. It also flies in the face of what I learned as a C++ programmer.

In C++ accessors are key to good programming practice. Their main use is to hide the implementation from the interface of a class, so the class can be updated without code using it having to be re-written. They make it easy to add code to record accesses for e.g. ref-counting or debugging purposes. Making members private and providing accessors for them documents the code as it indicates which members are safe to access directly which are not because they e.g. have side effects.

Monday, 9 January 2012

Variable speed rotation with complex numbers

I wrote only last week that complex numbers are best for mostly fixed or uniform speed rotations. This is true in general, but there are ways to use complex numbers when the speed varies, as long as it varies in a straightforward way. In particular if the speed increases linearly, so with uniform angular acceleration, it can be modelled with complex numbers.

Friday, 6 January 2012


Recent posts have focussed on how to avoid angles when programming. The reason for this is simple: angles are slow to work with. In particular the trigonometric functions used to work with angles are expensive compared to arithmetic operations. There are times though when it is impossible to avoid angles.

Thursday, 5 January 2012


I came across a few issues related to accuracy in my ballistics app, both expected and unexpected. As these have wider application than ballistics simulations and are interesting in their own right I thought them worth their own post.

Wednesday, 4 January 2012

Varying the rotation speed

One objection to rotating using complex numbers instead of angles is that it works best with a fixed rotation speed. This is correct: if the speed needs to vary from frame to frame then the 'delta' needs to be recalculated each frame, and it may be easier to just use the angle to calculate the rotation each time.

But as long as the speed is mostly fixed complex numbers work well. If for example the rotation speed changes in steps, but between these is constant, then the 'delta' needs only be recalculated at these steps.

Tuesday, 3 January 2012

Trig-free rotation blending

As mentioned yesterday complex numbers can be used to do rotations in two dimensions, by just multiplying by a suitable (complex) value. One particular application is rotation blending or interpolation, where the direction of something is blended smoothly over time. An example would be aiming a gun, where having it move over time to aim is more realistic and interesting.

Normally this would be done by blending angles, so an angle is updated in steps from one direction to another. But this is expensive as two trigonometric calculations are required each frame to update the direction or to transform whatever is being rotated. It is quicker to use complex numbers and avoid trigonometry altogether, except at the start.

Monday, 2 January 2012

Complex numbers

Complex numbers are a much under-appreciated topic in mathematics, or at least that's how it seems to me looking back on them. Very often they are introduced almost as a mathematical exercise, as a way of solving mathematical problems such as quadratics which are otherwise insoluble. But once this is done all it gives is an impossible solution.

For example in my ballistics application complex number solutions to the quadratic formula in it (given by a negative discriminant) arise when the target is unreachable. And many applications of complex numbers seem like this. Except the more you study mathematics and physics the more useful they become, arising in diverse areas such as dynamics, electro-magnetism, and quantum mechanics.