Vice Underdogs

Scripting => Script Showroom => Topic started by: Charley on July 25, 2011, 11:08:40 am

Title: Useful Small Functions/Snippets
Post by: Charley on July 25, 2011, 11:08:40 am
Here are some simple functions I wrote that are both useful within scripts and for becoming more acquainted with Squirrel.

Distance

This function returns the distance, in metres, between point (x1, y1) and (x2,y2). It uses simple Pythagoras.

Code: [Select]
function Distance(x1, y1, x2, y2)
{
local dist = sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)));
return dist;
}

InRadius

This function also uses Pythagoras. It returns whether point (x2,y2) is within the specified radius (rad, in metres) of point (x1,y1).

Code: [Select]
function InRadius(x1, y1, x2, y2, rad)
{
//where 1 = centre coord and 2 = player.pos
if (sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1))) < rad)
{
return true;
}
else if (sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1))) > rad)
{
return false;
}
}

Direction

This function returns the direction (North/East/South/West or in between) that point (x2,y2) is in from point (x1,y1).

Code: [Select]
function Direction(x1, y1, x2, y2)
{
//all values must be floats.
x1 = x1.tofloat();
x2 = x2.tofloat();
y1 = y1.tofloat();
y2 = y2.tofloat();
// Added those ^ just in case you forget :P
local m = (y2-y1)/(x2-x1);
if ((m >= 6) || (m <= -6))
{
if (y2 > y1) return "North";
else return "South";
}
if ((m < 6) && (m >= 0.5))
{
if (y2 > y1) return "North East";
else return "South West";
}
else if ((m < 0.5) && (m > -0.5))
{
if (x2 > x1) return "East";
else return "West";
}
else if ((m <= -0.5) && (m > -6))
{
if (y2 > y1) return "North West";
else return "South East";
}
}
Title: Re: Distance, InRadius, Direction
Post by: Skirmant on November 07, 2011, 08:30:26 pm
Nice. Especially the direction one  ;D

I'll just point out a few extra ones for people that suck at math. I heard Squirrel allows function overloading so I think there should be no problem in giving them the same names as long as the parameters differ.

1D Distance

Code: [Select]
function Distance(x1, x2) {
 local dis = x1 - x2;
 if (dis < 0) dis = -dis;
 return dis;
}

3D Distance

Code: [Select]
function Distance(x1, y1, z1, x2, y2, z2 ) {
return sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)) + ((z2 - z1)*(z2 - z1)));
}

3D Radius

Code: [Select]
function InRadius(x1, y1, z1, x2, y2, z2, rad) {
if (sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)) + ((z2 - z1)*(z2 - z1))) < rad) return true;
else return false;
}
Title: Re: Distance, InRadius, Direction
Post by: aXXo on January 08, 2012, 01:03:10 pm
Code: [Select]
//Function to get the coordinates of where a player is looking at.
//range represents the distance ahead of the player at which you want the point to be placed.
function GetPlayer2DLookAtPos( player, range )
{
local angle = player.Angle + 90; //To point North at 90 degrees (It is at 0 degree by default)

if ( angle > 360 )
angle -= 360;

local slope = tan( angle * ( pi/180 ) );

local x2 = null, y2 = null;

if ( angle >= 135 && angle <= 225 )
x2 = player.Pos.x - range;
else if ( angle > 225 && angle <= 315 )
y2 = player.Pos.y - range;
else if ( angle > 315 || angle <= 45 )
x2 = player.Pos.x + range;
else if ( angle >= 45 && angle < 135 )
y2 = player.Pos.y + range;

if ( !y2 )
y2 = ( ( x2 - player.Pos.x ) * slope ) + player.Pos.y;

else if ( !x2 )
x2 = (( y2 - player.Pos.y )/slope) + player.Pos.x;

return Vector( x2, y2, player.Pos.z);
}

Example Usage:
Code: [Select]
GetPlayer2DLookAtPos( player, 2 )Returns coordinates of a point 2 steps "ahead" of a player.
Title: Re: Distance, InRadius, Direction
Post by: Thijn on January 09, 2012, 07:18:35 am
Ow thats a nifty function axxo
Title: Re: Distance, InRadius, Direction
Post by: Charley on January 09, 2012, 11:54:33 am
I'm looking forward to testing that aXXo, trig ftw :p

One small thing for anyone that's new,

Code: [Select]
local x2 = null, y2 = null;
can just be
Code: [Select]
local x2, y2;
Makes no difference in functionality, just looks a bit neater imo.
Title: Re: Useful Small Functions/Snippets
Post by: Charley on May 09, 2012, 09:18:31 am
I've changed the name of this thread so that anyone with useful small functions or snippets can post them. If they're deemed useful enough and working then they'll stay.