Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.

Join our community!

If you are already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Game Math c++ code snippets; Can be adapted to use in other languages
Topic Started: Dec 12 2009, 10:28 PM (2,497 Views)
Reikyrr
Forum God
[ *  *  *  *  *  * ]
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.

Collision_point_tr (x1,y1,x2,y2,x3,y3,xx,yy);

Changing Radials to Degrees.
Returns Degrees
RadToDeg(Radials

Gets direction from a point. *NOTE* needs RadToDeg(); to function properly.
Returns direction in degrees
PointDirection( X1,Y1,X2,Y2

Moves a distance in a direction
Returns location
lengthdir


That's it, if you guys want to add math snippets please do so. I will edit this post.
Edited by Reikyrr, Dec 12 2009, 10:30 PM.
~Inspirational quote~
Offline Profile Quote Post Goto Top
 
Sothh
Member Avatar
Shaman Of Time
[ *  *  *  *  *  * ]
In PointDirection() what is atan2'?

Code:
 
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)
Offline Profile Quote Post Goto Top
 
skarik
Member Avatar
kitten eating scum
[ *  *  *  *  *  * ]
Try Arctan2.

It's the arctangent function that returns the angle in the correct quadrant unlike the regular arctanegent function.
Blog|EHS
Offline Profile Quote Post Goto Top
 
MysteriXYZ
Member Avatar
Master Matrix Masher
[ *  *  *  *  *  * ]
Sothh
Jan 10 2010, 07:12 PM
In PointDirection() what is atan2'?

Code:
 
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.
Offline Profile Quote Post Goto Top
 
Sothh
Member Avatar
Shaman Of Time
[ *  *  *  *  *  * ]
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)
Offline Profile Quote Post Goto Top
 
Sothh
Member Avatar
Shaman Of Time
[ *  *  *  *  *  * ]
I created a handy PointMath.py package for Python.

Have fun!
Attached to this post:
Attachments: 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)
Offline Profile Quote Post Goto Top
 
Sothh
Member Avatar
Shaman Of Time
[ *  *  *  *  *  * ]
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:
 
def PointDistance(x1,y1,x2,y2):
return (x2-x1) + (y2-y1)
Edited by Sothh, Feb 24 2010, 06:57 PM.
“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)
Offline Profile Quote Post Goto Top
 
Dr. Best
Member Avatar
Administrator
[ *  *  *  *  *  * ]
Interesting. So the distance from (1 1) to (0 0) is -2. I did not know that.
Offline Profile Quote Post Goto Top
 
Sothh
Member Avatar
Shaman Of Time
[ *  *  *  *  *  * ]
Dr. Best
Feb 24 2010, 07:50 PM
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)
Offline Profile Quote Post Goto Top
 
Gandalf20000
Member Avatar
Geek
[ *  *  *  *  *  * ]
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.
Offline Profile Quote Post Goto Top
 
Sothh
Member Avatar
Shaman Of Time
[ *  *  *  *  *  * ]
Gandalf20000
Feb 25 2010, 01:00 AM
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.
I fix my code (I think):

Code:
 
def PointDistance(x1,y1,x2,y2):
if (x2-x1) + (y2-y1) > 0:
return (x2-x1) + (y2-y1)
else:
return -((x2-x1) + (y2-y1))


I will try to get yours working though.
“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)
Offline Profile Quote Post Goto Top
 
skarik
Member Avatar
kitten eating scum
[ *  *  *  *  *  * ]
Sothh, the distance from 0,0 to 1,1 is √2, not 2. You're not thinking at all about diagonals, and so your formula sort of returns the Manhattan distance. Use the Pythagorean theorem instead.
Blog|EHS
Offline Profile Quote Post Goto Top
 
Sothh
Member Avatar
Shaman Of Time
[ *  *  *  *  *  * ]
Synexn-Prime
Feb 25 2010, 06:11 PM
Sothh, the distance from 0,0 to 1,1 is √2, not 2. You're not thinking at all about diagonals, and so your formula sort of returns the Manhattan distance. Use the Pythagorean theorem instead.
Its supposed to return the Manhatten distance!
“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)
Offline Profile Quote Post Goto Top
 
zeo
I like turtles.
[ *  *  *  *  * ]
I'll try contributing.

This will find the area of any 2d triangle.
Code:
 


double TriangleArea(float x1,float y1,float x2,float y2,float x3,float y3)
{
double d1,d2;
d1 = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
d2 = sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3));
return(0.5*d1*d2*(d2/d1));
}


Oh yeah, and you need the cmath library included. (Do I really need to mention this :P?)
Its useful foor.. I dunno.
Edited by zeo, Feb 26 2010, 12:13 AM.
49 46 20 59 4F 55 20 43 41 4E 20 52 45 41 44 20 54 48 49 53 20 59 4F 55 20 52 45 41 4C 4C 59 20 4E 45 45 44 20 54 4F 20 47 45 54 20 4C 41 49 44 3A 29
recur-recur-recur-recur-recursion-sion-sion-sion-STACK OVERFLOW
Offline Profile Quote Post Goto Top
 
Gandalf20000
Member Avatar
Geek
[ *  *  *  *  *  * ]
Sothh
Feb 25 2010, 06:30 PM
Synexn-Prime
Feb 25 2010, 06:11 PM
Sothh, the distance from 0,0 to 1,1 is √2, not 2. You're not thinking at all about diagonals, and so your formula sort of returns the Manhattan distance. Use the Pythagorean theorem instead.
Its supposed to return the Manhatten distance!
Um, even if it's Manhattan taxi distance, you're still never going to have negative distance... You should be asking for the absolute value after finding the end result.
Offline Profile Quote Post Goto Top
 
Go to Next Page
« Previous Topic · Tutorials · Next Topic »
Add Reply