Useful 2d math func| PointDirection()|Lengthdir() X&Y

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
roelor
Posts: 240
Joined: Wed Aug 13, 2008 8:06 am

Useful 2d math func| PointDirection()|Lengthdir() X&Y

Post by roelor »

Code: Select all

double RadToDeg ( double Value ) {
return Value*(180/3.14);
}

double PointDirection ( double X1, double Y1, double X2, double Y2 ) {
return RadToDeg ( atan2 ( Y2-Y1, X2-X1 ) );
}
// returns the x and y coordinates for a rotation and a distance.
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;
    }
Suggestions are welcome!
I have to thank the guys at the U3d (forum.ultimate3d.org) community for most of the math though.
I just put it into a function. (u3d is a 3d engine for an interprited language called gamemaker language.)


This is how to use it.

Code: Select all

//PointDirection(x1,y1,x2,y2);
PointDirection(0,0,0,10); //returns 180
//LengthDir_X(X,Distance,Rotation);
LengthDir_X(0,4,70);//1.37
//LengthDir_Y(Y,Distance,Rotation);
LengthDir_Y(0,4,70);//3.75
RadToDeg(); is self explainitory.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Just check irrMath.h and the vector classes, and you'll find most of those functions, if not all, under different names in Irrlicht.
roelor
Posts: 240
Joined: Wed Aug 13, 2008 8:06 am

Post by roelor »

Then, err.. Just use this for educational purposes...

*Whistles and slowly walks to the exit.*
Post Reply