plugins/include/vector.inc
author Matt Woodrow <pred@alliedmods.net>
Fri Aug 08 00:17:37 2008 +0000 (2008-08-08)
changeset 2224 676c3b826f13
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Initial Import of beta structs extension.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     5  * =============================================================================
     6  *
     7  * This file is part of the SourceMod/SourcePawn SDK.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #if defined _vector_included
    34  #endinput
    35 #endif
    36 #define _vector_included
    37 
    38 /**
    39  * Calculates a vector's length.
    40  *
    41  * @param vec		Vector.
    42  * @param squared	If true, the result will be squared (for optimization).
    43  * @return			Vector length (magnitude).			
    44  */
    45 native Float:GetVectorLength(const Float:vec[3], bool:squared=false);
    46 
    47 /**
    48  * Calculates the distance between two vectors.
    49  *
    50  * @param vec1		First vector.
    51  * @param vec2		Second vector.
    52  * @param squared	If true, the result will be squared (for optimization).
    53  * @return			Vector distance.
    54  */
    55 native Float:GetVectorDistance(const Float:vec1[3], const Float:vec2[3], bool:squared=false);
    56 
    57 /**
    58  * Calculates the dot product of two vectors.
    59  *
    60  * @param vec1		First vector.
    61  * @param vec2		Second vector.
    62  * @return			Dot product of the two vectors.
    63  */
    64 native Float:GetVectorDotProduct(const Float:vec1[3], const Float:vec2[3]);
    65 
    66 /**
    67  * Computes the cross product of two vectors.  Any input array can be the same 
    68  * as the output array.
    69  *
    70  * @param vec1		First vector.
    71  * @param vec2		Second vector.
    72  * @param result	Resultant vector.
    73  * @noreturn
    74  */
    75 native GetVectorCrossProduct(const Float:vec1[3], const Float:vec2[3], Float:result[3]);
    76 
    77 /**
    78  * Normalizes a vector.  The input array can be the same as the output array.
    79  *
    80  * @param vec		Vector.
    81  * @param result	Resultant vector.
    82  * @return			Vector length.
    83  */
    84 native Float:NormalizeVector(const Float:vec[3], Float:result[3]);
    85 
    86 /**
    87  * Returns vectors in the direction of an angle.
    88  *
    89  * @param angle		Angle.
    90  * @param fwd		Forward vector buffer or NULL_VECTOR.
    91  * @param right		Right vector buffer or NULL_VECTOR.
    92  * @param up		Up vector buffer or NULL_VECTOR.
    93  * @noreturn
    94  */
    95 native GetAngleVectors(const Float:angle[3], Float:fwd[3], Float:right[3], Float:up[3]);
    96 
    97 /**
    98  * Returns angles from a vector.
    99  *
   100  * @param vec		Vector.
   101  * @param angle		Angle buffer.
   102  * @noreturn
   103  */
   104 native GetVectorAngles(const Float:vec[3], Float:angle[3]);
   105 
   106 /**
   107  * Returns direction vectors from a vector.
   108  *
   109  * @param vec		Vector.
   110  * @param right		Right vector buffer or NULL_VECTOR.
   111  * @param up		Up vector buffer or NULL_VECTOR.
   112  * @noreturn
   113  */
   114 native GetVectorVectors(const Float:vec[3], Float:right[3], Float:up[3]);
   115 
   116 /**
   117  * Adds two vectors.  It is safe to use either input buffer as an output 
   118  * buffer.
   119  * 
   120  * @param vec1		First vector.
   121  * @param vec2		Second vector.
   122  * @param result	Result buffer.
   123  * @noreturn
   124  */
   125 stock AddVectors(const Float:vec1[3], const Float:vec2[3], Float:result[3])
   126 {
   127 	result[0] = vec1[0] + vec2[0];
   128 	result[1] = vec1[1] + vec2[1];
   129 	result[2] = vec1[2] + vec2[2];
   130 }
   131 
   132 /**
   133  * Subtracts a vector from another vector.  It is safe to use either input 
   134  * buffer as an output buffer.
   135  * 
   136  * @param vec1		First vector.
   137  * @param vec2		Second vector to subtract from first.
   138  * @param result	Result buffer.
   139  * @noreturn
   140  */
   141 stock SubtractVectors(const Float:vec1[3], const Float:vec2[3], Float:result[3])
   142 {
   143 	result[0] = vec1[0] - vec2[0];
   144 	result[1] = vec1[1] - vec2[1];
   145 	result[2] = vec1[2] - vec2[2];
   146 }
   147 
   148 /**
   149  * Scales a vector.
   150  *
   151  * @param vec		Vector.
   152  * @param scale		Scale value.
   153  * @noreturn
   154  */
   155 stock ScaleVector(Float:vec[3], Float:scale)
   156 {
   157 	vec[0] *= scale;
   158 	vec[1] *= scale;
   159 	vec[2] *= scale;
   160 }
   161 
   162 /**
   163  * Negatives a vector.
   164  *
   165  * @param vec		Vector.
   166  * @noreturn
   167  */
   168 stock NegateVector(Float:vec[3])
   169 {
   170 	vec[0] = -vec[0];
   171 	vec[1] = -vec[1];
   172 	vec[2] = -vec[2];
   173 }
   174 
   175 /**
   176  * Builds a vector from two points by subtracting the points.
   177  *
   178  * @param pt1		First point (to be subtracted from the second).
   179  * @param pt2		Second point.
   180  * @param output	Output vector buffer.
   181  * @noreturn
   182  */
   183 stock MakeVectorFromPoints(const Float:pt1[3], const Float:pt2[3], Float:output[3])
   184 {
   185 	output[0] = pt2[0] - pt1[0];
   186 	output[1] = pt2[1] - pt1[1];
   187 	output[2] = pt2[2] - pt1[2];
   188 }