Showing posts with label Maths. Show all posts
Showing posts with label Maths. Show all posts

Monday, 14 May 2012

Angle between two 3D vectors

This is something I noticed the other day. Someone had posted a method for finding the angle between two vectors in three dimensions, using the dot product and inverse cosine. But there is better approach, i.e. one that is generally more efficient, is certainly more accurate for some vectors, and can be more informative.

Wednesday, 22 February 2012

Collision theory

My last two posts have shown how to do basic collisions without explaining why they work. In many cases it's not important to know why, and a lot can be done with just bouncing balls off walls and the floor. But the theory shows where the results come from, how they fit together and most important how to use it to solve other collision problems.

Friday, 3 February 2012

Circle-circle intersection

Intersecting circles can be done in a number ways. One approach is algebraic: take the formulae for the circles and solve them to obtain values for the x and y coordinates. Another approach derives the line that joins the points of intersection then intersects that line with the circle.

But the method I use is more direct, and I think mathematically more robust. By direct I mean it calculates the result directly and does not depend on intermediate results, such as calculating the line or one of x and y first. As such there's much less chance the order of calculation will affect the result.

Thursday, 2 February 2012

Perp dot product

I have used the perp dot product in a couple of posts but not explained it. It has a page at Mathworld but no Wikipedia article so I should perhaps explain it here.



The perp dot product is a product between two vectors in two-dimensions, and is obtained by taking the dot product of one vector with the perpendicular of the other. The perpendicular is simply a vector at right angles to the vector it is based on with the same magnitude. It is obtained by rotating through 90° or π/2 radians, or by multiplying by the complex number i in the complex plane.

Wednesday, 1 February 2012

Lines

One problem that's been touched on in previous posts is how to deal with straight lines. In particular how to represent them efficiently and accurately. Making the wrong choice is not that disastrous but it can cause problems in some situations.

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.

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

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.

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.

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

Angles

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

Accuracy

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.

Friday, 30 December 2011

Angle-free rotation

In my last post I showed how to aim a gun at a target above or below the gun to fire a ballistic missile, i.e. one moving under gravity. The mathematics was entirely angle and trigonometry free, but I noted that the angle can be calculated, in case it's need to e.g. rotate a gun turret. But it's not actually needed for that: it's possible to generate the rotation matrix to line up an object with a direction, without using angles.

Monday, 26 December 2011

Direction

Many games need to calculate the direction, or bearing, between game objects. For example the direction of the player or other game character from a weapon, so the game knows  where to aim the weapon.


The natural way to do this is using angles: when describing the direction of something we commonly use an angular measure, often a crude one such as "North West" or "Two O'clock", distinguishing between eight, twelve or more directions. For more precision degrees can be used, which as numbers can have arbitrary precision.


But often angles are a poor way to measure direction. In particular in games it is rarely best to use angles, for two reasons.


Monday, 19 December 2011

More matrices

Last week I wrote about one way to use matrices, passing values to the Matrix constructor to make a transform or scale matrix. Matrices made this way can be passed to functions that need them, minimising the number of lines and temporary variables. This approach is fine for simple uses but breaks down for more complex applications, as the values passed to the constructor require multiple calculation steps, too much to do inline. Fortunately there is another, easier, way.