• Advertisement
Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by the_cyberlord
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.

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....


2All[flaming], especially in prev. thread:
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by the_cyberlord
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)?


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 this post


Link to post
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 this post


Link to post
Share on other sites
Aha.
Readed other thread.

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!

Share this post


Link to post
Share on other sites
Quote:
Original post by the_cyberlord
and how does it work with another direction? (_direction.c)

.c may be needed for for orientation. For 3d direction, a and b is enough, no need for c . You may also store distance together with direction, but don't name it c because it will be confusing.


Direction is usually storen as 3 numbers x,y,z for speed-up.
Because, for given points O and P it's fast to find x,y,z direction from O to P. No need to do asin and acos, then do sin and cos. Also stepping along direction in x,y,z is fast and simple, as in my first code.

Share this post


Link to post
Share on other sites
read my previous thread, and you'll know why I want three direction values and a speed value, and I don't care if I call them a,b,c or x,y,z , I use classes in a good manner so I don't have to care about them.

Share this post


Link to post
Share on other sites
Quote:
Original post by the_cyberlord
read my previous thread, and you'll know why I want three direction values and a speed value, and I don't care if I call them a,b,c or x,y,z , I use

no i dont.

Share this post


Link to post
Share on other sites
you want tree directions because
Quote:

but I see all those 3D modelers store it in 3 values, how does that work?

?
If so, xyz direction is probably what you need. Unless you're asking about orientations without knowing what you're asking for.

Anw, why abc and not xyz and why care even if you "use classes in a good manner":
because of confusion like in that and previous thread. So people will not mix Euler angles or something with vectors. See, that xyz direction is nothing like a,b direction.

Share this post


Link to post
Share on other sites
Quote:
Original post by Eelco
Quote:
Original post by the_cyberlord
read my previous thread, and you'll know why I want three direction values and a speed value, and I don't care if I call them a,b,c or x,y,z , I use

no i dont.

seconded.

Share this post


Link to post
Share on other sites
It's just pure because of logic, for example in a plane game: you can turn around the z axis (rolling), the x axis (nose up/down), and the y axis (rudder turning). Of course you can all represent these directions with just 2 rotations, but that's not very logical when your programming your plane game.

Share this post


Link to post
Share on other sites
It seems you guys don't really want to help...
can you just error check this code then?

GLvoid direction_apply(point angle, GLfloat distance, point &_position) {
if (angle.x<= 90) { position.z = (cos(angle)*distance); position.y = (sin(angle)*distance); }else
if (angle.x<= 180) { position.z = -(cos(angle)*distance); position.y = (sin(angle)*distance); }else
if (angle.x<= 270) { position.z = -(cos(angle)*distance); position.y = -(sin(angle)*distance); }else
if (angle.x<= 360) { position.z = (cos(angle)*distance); position.y = -(sin(angle)*distance); }

if (angle.y<= 90) { position.x = (cos(angle)*distance); position.z = (sin(angle)*distance); }else
if (angle.y<= 180) { position.x = -(cos(angle)*distance); position.z = (sin(angle)*distance); }else
if (angle.y<= 270) { position.x = -(cos(angle)*distance); position.z = -(sin(angle)*distance); }else
if (angle.y<= 360) { position.x = (cos(angle)*distance); position.z = -(sin(angle)*distance); }

if (angle.z<= 90) { position.y = (cos(angle)*distance); position.x = (sin(angle)*distance); }else
if (angle.z<= 180) { position.y = -(cos(angle)*distance); position.x = (sin(angle)*distance); }else
if (angle.z<= 270) { position.y = -(cos(angle)*distance); position.x = -(sin(angle)*distance); }else
if (angle.z<= 360) { position.y = (cos(angle)*distance); position.x = -(sin(angle)*distance); } }

Share this post


Link to post
Share on other sites
Okay. Seems that by helpful replies, orientation and direction is completely mixed up in that and prev. threads to great extent.

Anyway.
As about your code,
0:it makes no sense because you work like angle is float or double "sin(angle)" and declare it as "point angle".
1: you don't need all those ifs as hplus0603 said
2: you have tonns of unnecessary code, becoz you owerwrite every cooedinate of position 2 times.
3: i *guess* it will work not in way you expect even if you'll fix 0,1,and 2

...and as about helpfullness.... if someone don't understand something, yes, it's gives no help, therefore, it's not helpful, that's logic. But if someone gives reply that is misundestood.... it's unhelpful.
go there
Read this
and
this
,get confused, then get somme book about linear algebra(in school library), and everything will be fine. It's really a suggestion, potentially helpful suggestion, not a sarcasm or something.

Share this post


Link to post
Share on other sites
Well, just to ask, can you rewrite the code so all time waiste is gone?
I corrected some things (like value overwrite, but I'm still not shure if it'll work.)
And about the if's, do sin and cos produce negative values as well??
Oh yeah of course, I forgot angle.x, angle.y, angle.z, Fixed now.
And are all the other two axises for every rotation axis in the right order?

GLvoid direction_apply(coords angle, GLfloat distance, point &_position) {

if (angle.x<= 90) { _position.z = (cos(angle.x)*distance); _position.y = (sin(angle.x)*distance); }else
if (angle.x<= 180) { _position.z = -(cos(angle.x)*distance); _position.y = (sin(angle.x)*distance); }else
if (angle.x<= 270) { _position.z = -(cos(angle.x)*distance); _position.y = -(sin(angle.x)*distance); }else
if (angle.x<= 360) { _position.z = (cos(angle.x)*distance); _position.y = -(sin(angle.x)*distance); }

if (angle.y<= 90) { _position.x = (cos(angle.y)*distance); _position.z += (sin(angle.y)*distance); }else
if (angle.y<= 180) { _position.x = -(cos(angle.y)*distance); _position.z += (sin(angle.y)*distance); }else
if (angle.y<= 270) { _position.x = -(cos(angle.y)*distance); _position.z += -(sin(angle.y)*distance); }else
if (angle.y<= 360) { _position.x = (cos(angle.y)*distance); _position.z += -(sin(angle.y)*distance); }

if (angle.z<= 90) { _position.y += (cos(angle.z)*distance); _position.x += (sin(angle.z)*distance); }else
if (angle.z<= 180) { _position.y += -(cos(angle.z)*distance); _position.x += (sin(angle.z)*distance); }else
if (angle.z<= 270) { _position.y += -(cos(angle.z)*distance); _position.x += -(sin(angle.z)*distance); }else
if (angle.z<= 360) { _position.y += (cos(angle.z)*distance); _position.x += -(sin(angle.z)*distance); } }

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement