Moderators: Despellanion, Dr. Best

Game Math c++ code snippets

Reikyrr
Forum God
Reikyrr
Forum God
Joined: March 16th, 2008, 12:42 pm

December 12th, 2009, 8:28 pm #1

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);
    }
Changing Radials to Degrees.
Returns Degrees
[+] spoiler
double RadToDeg ( double Radials) {
return Radials*(180/3.14);
}
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~
Quote
Like
Share

Sothh
Forum God
Sothh
Forum God
Joined: March 25th, 2009, 4:12 pm

January 10th, 2010, 5:12 pm #2

In PointDirection() what is atan2'?

Code: Select all

def RadToDeg(radials):
    return Radials*(180/3.14)

def PointDirection(x1,y1,x2,y2):
    return RadToDeg(atan2(2-y1,x2-x1)
)

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
return RadToDeg(atan2(2-y1,x2-x1))
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)
Quote
Like
Share

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

January 10th, 2010, 5:18 pm #3

Try Arctan2.

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

MysteriXYZ
Forum God
Joined: April 11th, 2007, 2:35 pm

January 10th, 2010, 6:52 pm #4

Sothh wrote:In PointDirection() what is atan2'?

Code: Select all

def RadToDeg(radials):
    return Radials*(180/3.14)

def PointDirection(x1,y1,x2,y2):
    return RadToDeg(atan2(2-y1,x2-x1)
)

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
return RadToDeg(atan2(2-y1,x2-x1))
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.
Quote
Like
Share

Sothh
Forum God
Sothh
Forum God
Joined: March 25th, 2009, 4:12 pm

January 10th, 2010, 8:58 pm #5

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)
Quote
Like
Share

Sothh
Forum God
Sothh
Forum God
Joined: March 25th, 2009, 4:12 pm

February 23rd, 2010, 5:38 pm #6

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)
Quote
Like
Share

Sothh
Forum God
Sothh
Forum God
Joined: March 25th, 2009, 4:12 pm

February 24th, 2010, 4:56 pm #7

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)
Quote
Like
Share

Dr. Best
Forum God
Dr. Best
Forum God
Joined: March 16th, 2005, 8:47 pm

February 24th, 2010, 5:50 pm #8

Interesting. So the distance from (1 1) to (0 0) is -2. I did not know that.
Quote
Like
Share

Sothh
Forum God
Sothh
Forum God
Joined: March 25th, 2009, 4:12 pm

February 24th, 2010, 6:01 pm #9

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... :doh:
?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)
Quote
Like
Share

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

February 24th, 2010, 11:00 pm #10

Try this formula instead:

d=√((x2-x1)2+(y2-y1)2)

Try and use that other formula, and you're as stupid as my geometry teacher at my old high school that tried to add in the distance formula when he should have been subtracting.
Quote
Like
Share