# Gw Temp

## Tutorial - 'Trigonometry in RM2K/3' by laughy

An item about RPGMaker 2000 posted on

### Blurb

Laughy explains how to calculate sine and cosine in RM2K, along with the uses of such abilities in making much better RM2K mini-games!

### Body

This document is Laughy certified - number \$394AB-X34 in the national Laughy library of congress.

Laughy presents..

LAUGHY’S TRIGONOMETRY IN RM2K(3) GUIDE

by Laughy

HELLOOOOO, this is Laughy. Here is my first article for rm2k, which explains how to do those tricky trig functions (that is to say: sin, cos, and tan). At the end of this document we will have a laughy Q/A session which is fun. FUN I SAY. Okay, here I go.

Now first:

WHY?

There are a number of reasons one would want to use Trigonometry in Rm2k. Right now that number is one, since I can’t think of anything other than complicated mini-games. OH WAIT, maybe an rm2k calculator! That would be cool (although no one would play it).

Don’t believe me? Try out my sharp Bubble-Bobble demo, which I had planned to release but probably won't now (see Q/A time at end of document)

http://www.laughy.net/Bubble.zip

Notice how the ball comes out in the direction the pointer is facing. That ain’t no 5.3 million level fork ladies and gentlemen, that is pure sine and cosine goodness doing those calculations.

One can be creative with this to make all sorts of unique games that couldn’t be made before in Rm2k.

WHO?

You should have already taken trigonometry in school and understand sine and cosine (that is, what they are). If not, then go ahead and keep reading anyway, but don’t get frustrated if you can’t figure it out. Instead, borrow your older sister’s trigonometry book then after using secretly modify all the even homework problems. :x

Also if you use rm2k3 and not rm2k, the main idea should apply quite well, but the code included probably won’t work.

************************************************************************

WHAT?

Rather than attempt to making silly answers to “WHERE” and “WHEN”, I’m going to just dive right in.

Notation:

^ = power

/ = divided by

! = factorial

* = times (multiply)

The factorial of a number is x * (x – 1) * (x- 2) … * 1

So for x = 5, it would be

5! = 5 * 4 * 3 * 2 * 1 = 120

And for x = 10 it would be

10! = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = hours laughy has spent making nes emulator

So we want to make a cosine and sine routine using only what we have, which is additions, multiplications, divisions, and subtractions.

NOTE: Before thinking “rm2k can’t do decimals!!!!” I’ll explain later why it can so don’t worry

Well in Calculus one learns that any function that exists can be represented as an infinite SERIES. That is to say, a function like sine(x) can actually be represented as a bunch of numbers added together. Rather than attempt to explain why and how to calculate such a series, we will skip the Calculus lesson for the day and I will simply give them to you:

SINE(X) = x^1 / 1! - x^3 / 3! + x^5/5! - x^7/ 7! + x^9/9!....

O_O

Well there it is. Read it slowly! Anyway let’s step through it.

First of all, x has to be in RADIAN form.

So, if you wanted the sine for a 30 degree angle, you have to convert it to radians before it can go through that function. To convert an angle to a radian, simply multiply the angle by pi and divide by 180. So 30 degrees in radians is 30 * 3.14159 (pi) / 180 = .5236.

So now we have our angle in radians, and we put it in that function to calculate the sine of it. The function is an infinite series. That is, it goes on forever. You can see that every single time we go down the function, the numbers increase by two, and we are alternating between subtracting the next number and adding it. This is just the way the function works.

The farther you go down the infinite series, the more accurate your calculation will be. You can go down 3 steps and be pretty accurate, so for rm2k we use:

SINE(X) = x^1 / 1! - x^3 / 3! + x^5/5!

Or

SINE(X) = x – x cubed divided by 3 factorial + x to the 5th divided by 5 factorial.

Let’s try some examples to make sure it works.

Let’s take our angle 30. First we calculate the radians, which we did =

30 * 3.14159 (pi) / 180 = .5236

Now the first calculation in our series is x to the first power divided by 1 factorial.

1! = 1

.5236 ^ 1 / 1

Which is simply .5236, since x to the power of 1 is x, and anything divided by 1 is 1.

Now the second calculation in our series is x to the 3rd power divided by 3 factorial.

3! = 3 * 2 * 1 = 6

.5236 ^ 3 / 6

.5236 cubed is .1435, and divided by 6 gives us .0239.

The third calculation is x to the power of 5, divided by 5 factorial

5! = 5* 4 * 3 * 2 * 1 = 120

.5236 ^ 5 / 120

.5236 to the 5th is .03935 and divided by 120 gives us a wee number of .000328

So sine of 30 degrees is our first calculation minus our second plus our third.

.5236 - .0239 + .000328 = .500028

How close is this to a calculator’s sine function? Well sine of 30 is exactly .5. You can see with only 3 calculations we were a measly .000028 off! That’s fine for whatever you want to do with this.

Cosine is:

COS(X) = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8!....

As you can see we start by subtracting from one, and the numbers are even instead of odd. Again, going out 3 calculations (to the x^4) is sufficient:

COS(X) = 1 - x^2/2! + x^4/4!

************************************************************************

THE BIG QUESTION

”That’s great, but rm2k can’t do decimals!!!!!”

Well, I realized this is a problem right from the start, so I thought of way to go around that.

Let’s say you wanted to get the radian value for the angle of 30. So you plug in 30 and multiply it by 3.14159…. etc. Well, you can’t multiply by that in rm2k, and multiplying by just 3 would be inaccurate, so instead, we do a thing I call DECIMAL ASSUMING:

30 * 3,142 = 94,620

(3,142 = pi * 1,000 or 3.142 * 1,000)

The ACTUAL answer is 9.4230, however we ASSUME that the answer is 1000 too big! In order to calculate the sine and cosine of an angle in rm2k, your input for your calculations should be at least 1000 bigger than they should be, which gives you 4 or more decimal place accuracy.

COSINE OF AN ANGLE

Let’s do an example, this time with psuedo rm2k code.

Now “ANGLEINRADIANS” holds our angle in radians, but 1000 bigger than it should. We use this in our calculation. Now let’s try the second calculation of cosine with our angle:

x^2/2!

Well 30 * 3,142 / 180 is 524 in rm2k. So we square 524 and divide it by 2.

524 squared = 274,576 divided by 2 = 137,288.

This answer is 1,000 * 1,000 (1 million) too big (since we squared it) !!! The actual result from this calculation should be .137288

Onto the next calculation…

x^4/4!

…we have a problem folks. 524 to the 4th power is 75,391,979,776. That’s WAY too big for rm2k to handle. NOW WHAT?

Well here’s a small trick I learned. What is x^4/4! ? Well you can represent it as

(x^2 / 2!) squared divided by 6!!

WELL HEY! But 137,288 squared is 18,847,994,944.

Well how about we assume some more? Now let’s assume that the 137,288 number is actually 137. Now it’s only 1,000 bigger than what it should be. This shaved off three decimal places, decreasing accuracy, but WE HAVE NO CHOICE.

So…

137 squared = 18,768 / 6 = 3,128, which is again 1,000,000 too big (since we squared numbers that were 1000 too big each)

SETVARIABLE “CALCULATION2” to “CALCULATION1”

CHANGE VARIABLE “CALCULATION2” / 1000

CHANGE VARIABLE “CALCULATION2” * “CALCULATION2”

CHANGE VARIABLE “CALCULATION2” / 6

Well hey, we were able to do it, without using decimals.

So rm2k is holding two numbers right now. 137,288 (the result of our first calculation, which is 1,000,000 too big) and 3,128 (which is also 1,000,000 too big).

Our first calculation for cosine is 1. The one needs to be the same base as our other numbers, which is 1,000,000 too big. So the one is actually 1,000,000. But rm2k can’t hold 1,000,000! So we have to divide ALL numbers by 10 and assume it is only 100,000 bigger now:

100,000 – 1,3728 + 313 = 86585

This number is 100,000 bigger than it should be (since we divided by 10)

The actual cosine of 30 degrees is about .866. We were pretty dang close, off by .00016.

SETVARIABLE “COSINEOFANGLE” to 100,000

CHANGE VARIABLE “CALCULATION1” / 10

CHANGE VARIABLE “CALCULATION2” / 10

CHANGE VARIABLE “COSINEOFANGLE” – “CALCULATION1”

CHANGE VARIABLE “COSINEOFANGLE” + “CALCULATION2”

Remember our answer is 100,000 too big. That’s important. Most likely you will divide it by 100 or 1000 again for actual use.

USE:

You may be thinking, “Well, if I divide it by 100,000 I get 0!”. This is true, but when do you use cosine alone? More often, one uses angle identities with it. If I wanted to calculate the x increment value my ball should move by if I was at an angle of 30 degrees, then one knows that the x would be cos(30) * the hypotenuse. Remember this: The hypotenuse in this case is the SPEED you want the ball to go. First divide your answer by 100 (which would get us 866) and multiply by, say, 3 (our speed), getting us 2598. Since our answer is 1,000 too big still, our X value would round to 3. This may seem like a gross amount of inaccuracy, but recall that we only need to divide by 1000 when placing the ball. As the ball moves, we can continuously add our NON divided by number (2598) for the x value. Thus, the second time the ball moves, it will increment another 2598 in the x, which is 5196. Divide this by 1000 and we get an X value of 5. The placement of the ball is incrementing very accurately, but since putting pictures on screen needs to be whole numbers, ALL games have to round off their decimals when placing pictures. That’s what you are doing by dividing by 1000 each time.

SINE and the CODE:

Doing the sine is just a tad more complicated, but still very possible and accurate. The code for the sine, as well as the cosine, is in the demo I included at the top. All the code is in the second to right event on the bottom left of the screen, page 3.

IMPORTANT

There are some peculiarities you should know about:

If an angle is above 45 degrees, then the square of it’s radian values will be too big. Thus, remember that the sin of an angle above 45 is the cos of an angle below (90 – Angle), and the cos of an angle above 45 is the sin of an angle below (90 – Angle). Example:

SIN OF 75 degrees = COS of 15 DEGREES

COS OF 75 degrees = SIN of 15 DEGREES

So if you want to calculate an angle above 45, then subtract from 90 then calculate THAT angle, then switch the sin and cos when you are done. This is done in my code.

If an angle is above 90 degrees, the only difference is that the cos is negative of whatever it is (180 - Angle). If you wanted the sine/cos of 120 degrees, you would calculate it for 60 degrees and then multiply the cos by -1. This is done in my code.

If an angle is above 180 degrees, the sine would be negative, thus you would need to calculate (360 – Angle) and then multiply the sin by -1 when you are done. This is not done in my code, since I did not calculate angles above 180.

Tangent has its own infinite series, however don’t worry about it. Just calculate the sin/cos for that angle and divide the cos into the sin. However you should know:

Dividing two numbers that are equally “too big” gives a result that is not “too big”, yet may be very inaccurate. For instance if the sine of an angle is 30,000 and is 100,000 too big, and the cos is 50,000 and is 100,000 too big, then dividing them will result in 0 (which is NOT too accurate). To get an accurate number divide the number you will be dividing with by maybe 1,000 or so. In this case, 50,000 / 1,000 = 50. NOW divide, resulting in 600, which is 1,000 too big (because you divided by 50,000 by 1,000).

************************************************************************

LAUGHY Q/A TIME

Q) HUH?

A) If you have ANY questions or need help in any part email me Laugh10704@hotmail.com. Also e-mail Laugh10704@yahoo.com, since the hotmail account may be full of porn spam and thus I may not get your message.

Q) Who you be?

A) My name is Jordan Laughlin and I am 18 years old

Q) You going to school?

A) Yes, I’m currently attending Cal Poly State University in California in my second year, going for a masters in Computer Science. Drop by if you’re ever in the area! ^_^

Q) What you working on?

A) I am working on an NES emulator, which is pretty darn good. I was working on the above demo, however after doing the sine/cosine calculations I lost the time vs interest ratio. If anyone would like to pick up the game where I left off feel free to do so. I could help you with picture detection and such.

Q) OMG I LOVE U LAUGHY AND I’M FEMALE AND SINGLE

A) Yes, I get that a lot. If you would like to be one of my BILLIONS of female fans drop me an e-mail ^_^

A) Fushigi Yuugi