Login 
Search Journals
Some Basic Trig Functions Part 1
Location: BlogsCrashDome's JournalScripting Topics    
Posted by: CrashDome 6/30/2006
I Just recently produced a set of simple scripts for the fellows at WGL which moves a respawn marker to prevent camping by the defensive team. Since this script involves a bit of trigonometry, I thought I would mark this in the journals for future reference and for others to learn from. These are used often but hard to find usually.

I Just recently produced a set of simple scripts for the fellows at WGL which moves a respawn marker to prevent camping by the defensive team.

Since this script involves a bit of trigonometry, I thought I would mark this in the journals for future reference and for others to learn from. These are used often but hard to find usually.

 

Simple and Useful trig functions for OFP

 

Lesson 1: To determine the direction from point A to point B:

 

First we need the find relative difference in position from point A and B. In a Right Triangle, this would be the length of our adjacent and opposite legs. Assuming we have either an [x,y] or [x,y,z] positional array for each point (_posA and _posB in example) we will do the following to get a 2D relative position:

 

_relX = (_posA select 0 ) – (_posB select 0);

_relY = (_posA select 1 ) – (_posB select 1);

 

Once we have the relative position of point B from point A, we can set point A as the origin and find the direction (or angle) from point A to point B. This sounds complicated, but is really rather simple because it takes a simple command to figure out. Enter the ATAN2 command….

 

_dir = _relX atan2 _relY

 

ATAN2 will take the X and Y values and give you an angle in degrees that point B is from point A. The actual measurement of degrees starts north and goes from 0 to 180 along the right (or positive x axis) and goes from 0 to -180 along the left (or negative x axis).

 

atan2.jpg

 

To some script writers, this might be enough depending on the application, but to some they want the full 0 to 360 degree range of angle. That is useful in the cases where you use a command needing a 360 degree value such as setDir command.

 

To transform to 360 degrees do the following:

 

if (_dir < 0) then {_dir = 360 + _dir);    // for functions (sqf files)

 

or for sqs files

? (_dir < 0) : _dir = 360 + _dir    

 {EDIT: Typo! Corrected the sign from minus to plus)

Now you have a value that can be used in directional commands or for your own purposes!!

 

 

Lesson 2: To determine a point along an axis (direction) which is a specified distance

 

This function is a bit more confusing but can be simplified through trigonometry to create a very effective and efficient means to plot a point from an origin.

 

Let’s look at this from the perspective of an example:

 

findPoint.jpg

Let’s say you got unit A and he is looking/heading in direction D.

Let’s say you want to spawn a tank directally in front of him/her exactly 100 meters away.

How do you find the exact point that is 100 meters away in direction D??

 

Here’s how: (assume _Dir is direction and _Dist is distance)

 

First we need to get the sine and cosine of the direction (direction must be 0 to 360 – see above function to find that)

 

_sinDir = sin (_Dir);

_cosDir = cos (_Dir);

 

Now we get the relative position from our origin by figuring the X and Y axis (Right Triangle)

 

_relX = _sinDir * _Dist;

_relY = _cosDir * _Dist;

 

Now we know that or point is X and Y distance away from our origin, we add it to our origin point.

 

_newPos = [(origin select 0) + _relX , (origin select 1) + _relY];

 

Voila!!!

 

 

I hope this has helped someone and shown that complicated material need not be complicated scripting. Obviously, these functions can be compressed and optimized to use less variables and lines, but I figured I’d leave it this way for easy of understanding.

Permalink |  Trackback

Comments (2)   Add Comment
Re: Some Basic Trig Functions Part 1    By HurricaneJim on 7/6/2006
Thanks a lot for this, Crash! I always hate figuring the formulas for these types of problems. This should help!

Baby name meaning and origin for Sine    By TrackBack on 8/5/2009
Description for the baby name Sine, the origins of the name and its meaning
# Baby-Parenting.com


Your name:
Title:
Comment:
Add Comment   Cancel 
 
Copyright (c) 2012 Sinews of War: Development Center