## Game Math c++ code snippets

Moderators: Despellanion, Dr. Best

# Game Math c++ code snippets

Forum God
Reikyrr
Forum God
Joined: March 16th, 2008, 12:42 pm
Math snippets here!
However I don't take full credit for it.

Checking for a collision point inside a triangle.
Returns 1 for true and 0 for false.
The first six arguments are for the coordinates of the triangle. Tha last two for the position to check.
[+] spoiler

Code: Select all

``````bool Collision_point_tr(int x1,int y1,int x2,int y2,int x3,int y3,int xx,int yy)
{
int a [4];
a[0] = abs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1));    a[1] = abs((x1-xx)*(y2-yy)-(x2-xx)*(y1-yy));
a[2] = abs((x2-xx)*(y3-yy)-(x3-xx)*(y2-yy));    a[3] = abs((x3-xx)*(y1-yy)-(x1-xx)*(y3-yy));

return (abs(a[1]+a[2]+a[3]-a[0]) <= 1/256);
}
``````
Returns Degrees
[+] spoiler
}
Gets direction from a point. *NOTE* needs RadToDeg(); to function properly.
Returns direction in degrees
[+] spoiler
double PointDirection ( double X1, double Y1, double X2, double Y2 ) {
return RadToDeg ( atan2 ( Y2-Y1, X2-X1 ) );
}
Moves a distance in a direction
Returns location
[+] spoiler

Code: Select all

``````double Lengthdir_X (double x, double dist, double dir) {
return x+cos(dir*3.14/180)*dist;
}
double Lengthdir_Y (double y, double dist, double dir) {
return y+sin(dir*3.14/180)*dist;
}
``````
That's it, if you guys want to add math snippets please do so. I will edit this post.
~Inspirational quote~

Forum God
Sothh
Forum God
Joined: March 25th, 2009, 4:12 pm
In PointDirection() what is atan2'?

Code: Select all

``````def RadToDeg(radials):

def PointDirection(x1,y1,x2,y2):
)

But I get an error:

PointDirection(0,0,50,50)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
PointDirection(0,0,50,50)
File "C:\Users\Sothh\Desktop\Math.py", line 5, in PointDirection
NameError: global name 'atan2' is not defined

?You can?t outrun Death forever.
But you can make the Bastard work for it.?

Major Korgo Korgar
?Last of The Lancers? - AFC 32
(Andromeda Ascendant Record Database)

Forum God
skarik
Forum God
Joined: October 4th, 2006, 11:56 pm
Try Arctan2.

It's the arctangent function that returns the angle in the correct quadrant unlike the regular arctanegent function.

Forum God
MysteriXYZ
Forum God
Joined: April 11th, 2007, 2:35 pm
Sothh wrote:In PointDirection() what is atan2'?

Code: Select all

``````def RadToDeg(radials):

def PointDirection(x1,y1,x2,y2):
)

But I get an error:

PointDirection(0,0,50,50)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
PointDirection(0,0,50,50)
File "C:\Users\Sothh\Desktop\Math.py", line 5, in PointDirection
NameError: global name 'atan2' is not defined
There's a nice Python math module at your disposal . You should really use the tools available to you .
U3D is like candy; after extensive consumption, it's Best to brush.

Forum God
Sothh
Forum God
Joined: March 25th, 2009, 4:12 pm
I got it working!

Thanks for these snippets!
?You can?t outrun Death forever.
But you can make the Bastard work for it.?

Major Korgo Korgar
?Last of The Lancers? - AFC 32
(Andromeda Ascendant Record Database)

Forum God
Sothh
Forum God
Joined: March 25th, 2009, 4:12 pm
I created a handy PointMath.py package for Python.

Have fun!
PointMath.py (345 Bytes)
?You can?t outrun Death forever.
But you can make the Bastard work for it.?

Major Korgo Korgar
?Last of The Lancers? - AFC 32
(Andromeda Ascendant Record Database)

Forum God
Sothh
Forum God
Joined: March 25th, 2009, 4:12 pm
Heres a snippet. It returns the distance between two points. Its in Python, but its not hard to change it for use in c++.

Code: Select all

``````def PointDistance(x1,y1,x2,y2):
return (x2-x1) + (y2-y1)``````
?You can?t outrun Death forever.
But you can make the Bastard work for it.?

Major Korgo Korgar
?Last of The Lancers? - AFC 32
(Andromeda Ascendant Record Database)

Forum God
Dr. Best
Forum God
Joined: March 16th, 2005, 8:47 pm
Interesting. So the distance from (1 1) to (0 0) is -2. I did not know that.

Forum God
Sothh
Forum God
Joined: March 25th, 2009, 4:12 pm
Dr. Best wrote:Interesting. So the distance from (1 1) to (0 0) is -2. I did not know that.
What???

Thats correct... you have to go backwards by two to get to 0,0 from your current position.

I am very confused...
?You can?t outrun Death forever.
But you can make the Bastard work for it.?

Major Korgo Korgar
?Last of The Lancers? - AFC 32
(Andromeda Ascendant Record Database)

Forum God
Gandalf20000
Forum God
Joined: March 4th, 2008, 9:19 pm