Trigonometry: Part 3
Page 1 of 2
<1 | 2>
Single-page view
Trigonometry: Part 3

This article originally appeared in Dev.Mag Issue 16, released in August 2007

Welcome back, fellow coders! Prepare yourselves for the final, bumper episode of the Trig Trilogy, where I cover a whole lot of awesome trig techniques for your game-making pleasure. We’ve got a lot to cover, so let’s jump right in. It’s time for my first trick! Now, as you can see, there is nothing up my sleeve...

Relative Angling 101

No matter what kind of game you’re working on, more often than not you’ll need to calculate the angle between two objects for some reason. It’s time to look at a chunk of code that can make your life very easy in this regard:

Equation

What this code does is to first calculate the difference in x and y coordinates between the two objects, then divides as per the arctan ratio. X1 and Y1 represent the coordinates of the object you want to calculate the angle from. X2 and Y2 are, obviously, the coordinates of the object whose angle you want to measure relative to the first object.

We perform the subtraction because all trig calculations assume that our origin is at point (0,0) on the Cartesian plane. As a result, we need to use relative measures, essentially making point (X1, Y1) our origin. If this all seems really confusing to you, worry not. I’ve summed it up for you in these two diagrams. As you can see, there’s a massive difference between the two depending on whether you subtract or not!

Angle calculated without subtraction
Angle calculated without subtraction
Angle calculated with subtraction
Angle calculated with subtraction

One final thing to note is that this algorithm will give you the angle in radians, as was discussed last month. Remember to convert the answer to degrees if that’s what your game requires!

And there you have it: all you need to know about calculating the relative angle between two objects! That wasn’t so complicated, was it?

There are several functions you can use in GM to calculate your angles. You can use the same algorithm as above or use the slightly altered arctan2(y,x) function, meaning you don’t have to divide first. By far the easiest, however, is to use the point_direction(x1,y1,x2,y2) function, which also converts the angle to degrees for you as a little added extra! The choice is yours.

Goin’ Round in Circles

One of the most prevalent problems that first-time coders have is making objects follow a circular path. There could be any number of uses for this - having a targeting reticle orbit around your game character, or perhaps a spinning-blade-on-a-chain trap for hapless players to wander into. Either way, if you’ve ever wanted to have one thing spinning around another, this is exactly what you need!

Fortunately, circular positioning is not at all difficult to do for us mighty trig-wielders. As always, let’s look at exactly what we’re trying to accomplish here in the form of a handy diagram.

The humble circle
The humble circle

Figure 1 depicts what is commonly referred to as a "circle". This shape is notable because it has a constant radius. That means that regardless of where you are on the circumference, you’ll be exactly the same distance away from the center. This gives us an important clue.

Shazam! Trig’d!
Shazam! Trig’d!

Figure 2 depicts our situation more clearly. What we really want to do in code terms is to calculate the x and y coordinates of an object while ensuring that, regardless of its angle to the center, it always remains within a set distance of that center. We can do that using the following two algorithms:

Equation

"CenterX" and "CenterY" refer to the coordinates of the position/object we want to rotate around. It’s important that we have these, otherwise (as with our angle calculations) the object is going to rotate around the screen/level origin (0,0) instead of the center we want. "Radius" is obviously the radius of our circle, and "Angle" is the relative angle between the center and the orbiting object. Changing the Angle will rotate the object around the center. Increasing or decreasing the Radius will move the object further from or closer to the center. Changing the CenterX and CenterY coordinates will, of course, move the center and the spinning object with it. That’s all there is to it!

A final note. If you’re implementing a spinning object where you keep increasing the angle to create rotation, it’s wise to reset the Angle to 0 once it exceeds the measure for one full rotation (either 360 degrees or 2π radians). Although it shouldn’t make much difference to your calculations, it certainly keeps things cleaner, and ensures that you know exactly what range of numbers your code is working with. This can make debugging a lot easier.

Game Maker makes things easy for you again by providing the lengthdir_x(len,dir) and lengthdir_y(len,dir) functions. These take your circle/ellipse radius (len) and direction (dir) and calculate the correct x and y values for you just like we did above. No mess, no fuss, and you can use them any time you need to resolve an angled line to x and y coordinates, not just with circles. Just remember to add the CenterX and CenterY coordinates to the final answers!



Words from the readers
Very valid point. This is a reprint of a rather old article, but I'm sure we can drag the author back to this to write up an "applied trig" piece of sorts. I'll shake him about a bit and see what falls out.
Posted by Nandrew at 15:37:37 on 21 June 2009
uhm the article stated in the beginning (article 1, paragraph 2, 3rd scentence):
"The aim of this series is to explain to you just how simple trigonometry really is, and how you can use it to solve some very common programming problems."
And yes you explained how simple trig is. However, other than breaking down Game maker formulas (that already exist outside the understanding of trig) there has not been much on the "solving of some very common problems". In fact the only problem that didn't revert back to a simple gml formula was the diaginal movement problem taken from part 2. Did I miss something? Not to say trig is worthless cause other's have invented the wheel already, Im just wondering what other common or practicle uses trig would have. Collisions detection, proper angular or parabolic movement, line of sight, damage control, statistical data manipulation, etc...
Posted by Dennis Tudor at 23:30:36 on 20 June 2009
Have your say: