• 10
• 9
• 12
• 14
• 15

# best way to get coordinates out of a 3D direction?

This topic is 4932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

what is the best way to get coordinates out of a 3D direction? before I used only 2 directions for my rotations (x,y) but now I have 3 of them (x,y,z) so I don't really know how to fix this code, or even if it's the best way to do it. Just changed the code because of my code redesign>> angle has now x,y,z. So this code now only applies to angle.y
GLvoid camera::change_dir(point &position, GLfloat angle, GLfloat distance) {
if (angle<= 90) { position.x = (cos(angle)*distance); position.z = (sin(angle)*distance); }else
if (angle<= 180) { position.x = -(cos(angle)*distance); position.z = (sin(angle)*distance); }else
if (angle<= 270) { position.x = -(cos(angle)*distance); position.z = -(sin(angle)*distance); }else
if (angle<= 360) { position.x = (cos(angle)*distance); position.z = -(sin(angle)*distance); } }



##### Share on other sites
You don't need to use those cases. sin() and cos() are defined in the entire range -pi to pi (actually, for any multiples of pi, too), so you can just plug them in.

Google for "rotation matrix" and you'll get lots of good examples. Or you can get some rotation-to-quat code, and then use quat-to-matrix.

Once you have a matrix, the upper-left 3x3 part of the matrix will contain your left, up, and front vectors (or right, up and back). Or if you're in left-handed space, your left-up-back or right-up-front.

##### Share on other sites
Oh man! I think I'll understand all of those things better with some example code...
Can someone write a function (I mean, the fastest way to do it), with this function declaration: ( point is a class with .x,.y,.z GLfloats)
EDIT: This is not with vectors, _direction contains degrees, and _position your orientating point
GLvoid direction_apply(point _direction, GLfloat distance, point &_position)

##### Share on other sites
If you have vector math lib (that "point" in math is called 3d vector or vector3 or something.)
,it's simply
{
position=direction*distance;
}
And written longer,
{
position.x=direction.x*distance;
position.y=direction.y*distance;
position.z=direction.z*distance;
}

It's assuming direction point is at distance 1 from 0,0,0 ,so after multiplying by _distance it's at _distance from 0,0,0 .

And to do such things you'll need to know linear algebra / vector math .

##### Share on other sites
Sorry but you're thinking I'm talking about 3D matrixes, I want the real coordinates that ly on distance _distance and direction _direction calculated from position _position. Look at the first example I gave, but that's only for y-axis rotations.

##### Share on other sites
Quote:
 Original post by the_cyberlordSorry but you're thinking I'm talking about 3D matrixes, I want the real coordinates that ly on distance _distance and direction _direction calculated from position _position. Look at the first example I gave, but that's only for y-axis rotations.

first example doesn't calculate anything from position, it only writes result to it.

My example is not related to matrices. "point _direction" is supposed to contain x,y,z that define direction how most people do it(how i do it in my everyday work, how my boss do it, how almost everyone does it). There is other way that stores direction in 2 angles, usually named a,b, not x,y,z.

Matrices,quaternions,euler angles, and other mathy blabla you there hear about shoulda be suggested for other purprose, to store orientation, not direction. In 2D, direction and orientation it's same things. In 3D, direction and orientation is different things, so the confusion in that thread. Imagine airplane that flies to some point. It's propeller spins while having same direction....

As i understand, he understand difference between direction and orientation. Stop scaring him with matrices and quaternions.

[edit ]
in summary: there you'll get more and more confused. Even reading hyper-advanced mathworld articles that refer to "unrelated things" can't be any more more confusing than gamedev.net. direction
Do some google searches for direction vector...
The best thing for you is to get some linear math book in library, then buy some books on same subject,etc.

##### Share on other sites
But that result is calculated eh ;-).
But can't you just create a function for me that uses this function declaration:
GLvoid direction_apply(point _direction, GLfloat distance, point &_position)?

##### Share on other sites
Quote:
 Original post by the_cyberlordBut that result is calculated eh ;-).But can't you just create a function for me that uses this function declaration:GLvoid direction_apply(point _direction, GLfloat distance, point &_position)?

GLvoid direction_apply(point _direction, GLfloat distance, point &_position){ _position.x=_direction.x*distance; _position.y=_direction.y*distance; _position.z=_direction.z*distance;}

##### Share on other sites
BUT THAT WONT WORK!!!!, we're talking about degrees...

GLvoid direction_apply(point _direction, GLfloat distance, point &_position)
{
_position.x=_direction.x*distance;
_position.y=_direction.y*distance;
_position.z=_direction.z*distance;
}

If I translate it to what the things really are:
_position.x=20°*10;
_position.y=30°*10;
_position.z=180°*10;
that has no result...
It has to be something like my first code!

##### Share on other sites
Aha.

If your direction contains x,y,z it's supposted to contain not angles but x,y,z cartesian coordinates.Coordinates designed especially so this code _will_ work.

With angles it woulda be

struct direction_in_polar_coorinates{
double a,b;// if you want, GLdouble , or GLfloat
// Imagine a tank. If "a" stores horisontal direction of "head", and "b" stores elevation of gun , our polar coordinates store direction of shot.
}
GLvoid direction_apply(direction_in_polar_coorinates _direction, GLfloat distance, point &_position){
_position.x=distance*cos(_direction.a)*cos(_direction.b);
_position.y=distance*sin(_direction.a)*cos(_direction.b);
_position.z=distance*sin(_direction.b);
}

edit:
And to clarify non-angle xyz direction in cartesian coordinates i'm talking about:
It's can be made using function like

void convert_spherical_to_cartesian(direction_in_polar_coorinates poldir, point &vecdir){
vecdir.x=cos(poldir.a)*cos(poldir.b);
vecdir.y=sin(poldir.a)*cos(poldir.b);
vecdir.z=sin(poldir.b);
}

See? If you pass vecdir made using "convert_spherical_to_cartesian"
to my first "direction_apply" , you'll get right result!