Page **1** of **3**

### Trig and Maptools

Posted: **Mon Jun 29, 2009 11:39 am**

by **wandering_sage**

Hello

I'm a novice programmer who is using Map tools in an attempt to host a turn based space simulator for some friends and my own intellectual loafing.

The gist of the post is are there any trig functions available in map tools?

When your in your space ship and you rotate say 15 degrees and then gun your main engines (the ones on the back of the ship) Your new position (X,Y) can be determined using trig x + = math.cos(angle of rotation) * speed and y + = math.sine(angle of rotation) * speed

That's how you do it in VB are there any equivalent functions available in Map tools that i could use to achieve a similar effect

I hope to build a few macro's that automates movement taking relative velocity and rotation into account to help you maneuver your ship

### Re: Trig and Maptools

Posted: **Mon Jun 29, 2009 11:54 am**

by **jfrazierjr**

Here is the documentation of the list of built in "math" functions available in the RPtools projects:

http://lmwcs.com/rptools/wiki/Category: ... l_FunctionI believe someone posted that you can do a "long" form set of formula's using standard operations to get at sine/cosine stuff, but you have to know the formula(which I don't).

### Re: Trig and Maptools

Posted: **Mon Jun 29, 2009 12:00 pm**

by **wandering_sage**

Had that site bookmarked already didn't see anything about trig functions. Maybe I should add this to the wall of features

### Re: Trig and Maptools

Posted: **Mon Jun 29, 2009 12:45 pm**

by **RPTroll**

wandering_sage wrote:Had that site bookmarked already didn't see anything about trig functions. Maybe I should add this to the wall of features

Right in there with the 'mod' function.

### Re: Trig and Maptools

Posted: **Mon Jun 29, 2009 12:47 pm**

by **zEal**

**onCampaignLoad**Code: Select all

`[ defineFunction( "radians", "[email protected]", 1, 1 ) ]`

[ defineFunction( "degrees", "[email protected]", 1, 1 ) ]

[ defineFunction( "sine", "[email protected]", 1, 1 ) ]

[ defineFunction( "cosine", "[email protected]", 1, 1 ) ]

[ defineFunction( "factorial", "[email protected]", 1, 1 ) ]

[ defineFunction( "pi", "[email protected]", 1, 1 ) ]

[ defineFunction( "modulo", "[email protected]", 1, 1 ) ]

**radians**Code: Select all

`[ Degrees = arg( 0 ) ]`

[ macro.return = Degrees * ( pi() / 180 ) ]

**degrees**Code: Select all

`[ Radians = arg( 0 ) ]`

[ macro.return = Radians * ( 180 / pi() ) ]

**sine**Code: Select all

`[ Radians = arg( 0 ) ]`

[ Sine = Radians - ( power( Radians, 3 ) / factorial( 3 ) ) +

( power( Radians, 5 ) / factorial( 5 ) ) -

( power( Radians, 7 ) / factorial( 7 ) ) +

( power( Radians, 9 ) / factorial( 9 ) ) -

( power( Radians, 11 ) / factorial( 11 ) ) +

( power( Radians, 13 ) / factorial( 13 ) ) -

( power( Radians, 15 ) / factorial( 15 ) )

]

[ macro.return = Sine ]

**cosine**Code: Select all

`[ Radians = arg( 0 ) ]`

[ Cosine = 1 - ( power( Radians, 2 ) / factorial( 2 ) ) +

( power( Radians, 4 ) / factorial( 4 ) ) -

( power( Radians, 6 ) / factorial( 6 ) ) +

( power( Radians, 8 ) / factorial( 8 ) ) -

( power( Radians, 10 ) / factorial( 10 ) ) +

( power( Radians, 12 ) / factorial( 12 ) ) -

( power( Radians, 14 ) / factorial( 14 ) )

]

[ macro.return = Cosine ]

**factorial**Code: Select all

`[ Number = arg( 0 ) ]`

[ if( Number <= 1 ), code:

{

[ macro.return = 1 ]

};{

[ macro.return = Number * factorial( Number-1 ) ]

} ]

**pi**Code: Select all

`[ macro.return = 3.14159265358979323846 ] `

**modulo**Code: Select all

`[ Number = arg( 0 ) ]`

[ Divider = arg( 1 ) ]

[ macro.return = Number - ( Divider * floor( Number / Divider ) ) ]

The sine and cosine are accurate to around 16 decimal places.

### Re: Trig and Maptools

Posted: **Mon Jun 29, 2009 12:54 pm**

by **RPTroll**

zEal wrote:The sine and cosine are accurate to around 16 decimal places.

pffft. Like that's even close to the accuracy I'll need.

heh. Great stuff. Thanks!

### Re: Trig and Maptools

Posted: **Mon Jun 29, 2009 1:43 pm**

by **zEal**

RPTroll wrote:pffft. Like that's even close to the accuracy I'll need.

You'd think so, but when performing a series of trig operations, the lack of precision starts to add up and the answer might just end up wrong.

Now if someone that has better understanding of 'natural mathematics', would chime in with how to figure out tangent, arc sine, arc cosine, and arc tangent, we would be all set.

### Re: Trig and Maptools

Posted: **Mon Jun 29, 2009 2:03 pm**

by **zylosan**

A related question to this topic.

Is it even possible to represnt a three axis grip on map tools. (X,Y,Z)

### Re: Trig and Maptools

Posted: **Mon Jun 29, 2009 2:21 pm**

by **wandering_sage**

Are you talking about representing a tokens elevation on the map? If So just make some custom States with a lil diagonal line who's length is proportionate to there elevation

### Re: Trig and Maptools

Posted: **Mon Jun 29, 2009 2:30 pm**

by **RPTroll**

You could also use a vertical health bar to represent altitude.

### Re: Trig and Maptools

Posted: **Tue Jun 30, 2009 7:35 pm**

by **Akodo Makama**

zEal wrote:RPTroll wrote:pffft. Like that's even close to the accuracy I'll need.

You'd think so, but when performing a series of trig operations, the lack of precision starts to add up and the answer might just end up wrong.

Now if someone that has better understanding of 'natural mathematics', would chime in with how to figure out tangent, arc sine, arc cosine, and arc tangent, we would be all set.

Tan(x)= sin(x)/cos(x)

arcsin(x) = x + 1/2 (x^3/3) + (1/2)(3/4)(x^5/5) + (1/2)(3/4)(5/6)(x^7/7) +...

arctan(x) = x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - x^11/11 +...

arccos(x) = pi()/2 -x -x^3/6 -3x^5/40 - 5x^7/112 - 35x^9/1152 - 63x^11/2816 -...

### Re: Trig and Maptools

Posted: **Fri Jul 03, 2009 5:09 am**

by **zEal**

Akodo Makama wrote:Tan(x)= sin(x)/cos(x)

arcsin(x) = x + 1/2 (x^3/3) + (1/2)(3/4)(x^5/5) + (1/2)(3/4)(5/6)(x^7/7) +...

arctan(x) = x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - x^11/11 +...

arccos(x) = pi()/2 -x -x^3/6 -3x^5/40 - 5x^7/112 - 35x^9/1152 - 63x^11/2816 -...

Good stuff

Could you explain how to add precision to arc cosine? Those numbers seem completely arbitrary to me as I can't seem to grok a pattern.

### Re: Trig and Maptools

Posted: **Fri Jul 03, 2009 4:08 pm**

by **Akodo Makama**

zEal wrote:Akodo Makama wrote:Tan(x)= sin(x)/cos(x)

arcsin(x) = x + 1/2 (x^3/3) + (1/2)(3/4)(x^5/5) + (1/2)(3/4)(5/6)(x^7/7) +...

arctan(x) = x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - x^11/11 +...

arccos(x) = pi()/2 -x -x^3/6 -3x^5/40 - 5x^7/112 - 35x^9/1152 - 63x^11/2816 -...

Good stuff

Could you explain how to add precision to arc cosine? Those numbers seem completely arbitrary to me as I can't seem to grok a pattern.

I just copypasted what MatLab spit out at me.

### Re: Trig and Maptools

Posted: **Fri Jul 03, 2009 5:12 pm**

by **Brigand**

arcsin(x) = x + 1/2 (x^3/3) + (1/2)(3/4)(x^5/5) + (1/2)(3/4)(5/6)(x^7/7)

The pattern looks pretty simple to me: x + a/b (x^c/c)

Add two to c and then fill in the rest backwards, subtracting one for each a/b pair required to however precise you need it to be.

### Re: Trig and Maptools

Posted: **Fri Jul 03, 2009 7:35 pm**

by **zEal**

Akodo Makama wrote:I just copypasted what MatLab spit out at me.

Is there any way of having it spit out more?

Here's a corrected/improved modulo. The original didn't properly handle non-absolute values or floating point numbers. When performing modulo on floating point numbers, you could end up with a number with a lot of decimal places. Use

Trim Number if need be.

Code: Select all

`[ Number = arg( 0 ) ]`

[ Divider = arg( 1 ) ]

[ Modulo = Divider * ( ( abs( Number ) / Divider ) - floor( abs( Number ) / Divider) ) ]

[ macro.return = if( abs( Number ) == Number, Modulo, -Modulo ) ]