0

This is the exact thing I want: 3D Vector defined by 2 angles except reverse.

I want to go from 6 points (a 3D vector) to 2 angles (yaw and pitch. roll isn't necessary.)

Can anyone help? I'm not using a programming language anyone would know. It's GML, in GameMaker: Studio (yoyogames.com). I have a script that converts the mouse coordinates into a 3D vector from the camera position, and to convert that into directions, I started working it out.

I have the perfect code for yaw, but I can't figure out how to find the pitch. This is the code if you can understand it:

{
var mm,dX,dY,dZ,uX,uY,uZ,vX,vY,vZ,mX,mY,mZ, width, height, tFOV;
dX = argument3-argument0;
dY = argument4-argument1;
dZ = argument5-argument2;
mm = sqrt(dX*dX+dY*dY+dZ*dZ);
dX /= mm;
dY /= mm;
dZ /= mm;
uX = argument6;
uY = argument7;
uZ = argument8;
mm = uX*dX+uY*dY+uZ*dZ;
uX -= mm*dX;
uY -= mm*dY;
uZ -= mm*dZ
mm = sqrt(uX*uX+uY*uY+uZ*uZ);
uX /= mm;
uY /= mm;
uZ /= mm;
// v = u x d
vX = uY*dZ-dY*uZ;
vY = uZ*dX-dZ*uX;
vZ = uX*dY-dX*uY;
tFOV = tan(argument9*pi/360);
uX *= tFOV;
uY *= tFOV;
uZ *= tFOV;
vX *= tFOV*argument10;
vY *= tFOV*argument10;
vZ *= tFOV*argument10;
width = window_get_width();
height = window_get_height();
mX = dX+uX*(1-2*mouse_y/height)+vX*(2*mouse_x/width-1);
mY = dY+uY*(1-2*mouse_y/height)+vY*(2*mouse_x/width-1);
mZ = dZ+uZ*(1-2*mouse_y/height)+vZ*(2*mouse_x/width-1);
mm = sqrt(mX*mX+mY*mY+mZ*mZ);
global.mouse_dx = mX/mm;
global.mouse_dy = mY/mm;
global.mouse_dz = mZ/mm;
global.mouse_yaw = point_direction(0,0,global.mouse_dx,global.mouse_dy);
global.mouse_pitch = point_direction(0,0,dcos(global.mouse_dy),global.mouse_dz);
}

The only thing wrong with this is the

global.mouse_pitch = point_direction(0,0,dcos(global.mouse_dy),global.mouse_dz);

is wrong.

Some GML background: -dcos is used instead of cos, because dcos uses degrees while cos uses radians. -point_direction returns the direction between 2 coordinates (x1,y1,x2,y2) -sqrt returns the square root of a value -This is a script that uses 11 arguments. The arguments, in order, are:

xfrom, yfrom, zfrom, xto, yto, zto, xup, yup, zup, angle, aspect

from values are camera coordinates. to values are the coordinates of where the camera is looking. up values represent the 3D vector of "up" in 3D space. In my case, I use 0,0,1, so that positive z values are up. angle is the Field of View. aspect is the Aspect Ratio of the window.

I worked in paint for a while. I'll post what I did in paint: 2 perfect angle spheres and a sphere of the other 2 combined

Community
  • 1
  • 1
Anixias
  • 45
  • 1
  • 8
  • It is not at all clear what this code does, what you intend it to do, whether those two are the same, which variables represent the Cartesian coordinates of the vector, what `point_direction(...)` is supposed to calculate, whether your choice of arguments for `point_direction()` is correct, why you use `dcos` in the calculation of pitch but not of yaw, what `dcos` does, nor why your argument uses *eleven* arguments plus *two* calls to a window plus a complicated global variable. This code is not helpful; would a simple mathematical formula satisfy you? – Beta Jan 05 '16 at 05:22
  • All I'm looking for is converting a 3D vector into 2 angles, pitch and yaw. – Anixias Jan 05 '16 at 05:23
  • A 3D vector cannot be described by two scalar values. A *unit* vector can be (with **r:=1**). In any case, you probably want to start with the [cartesian-to-spherical](https://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates) equations, not some random code you pulled from the web. – MooseBoys Jan 05 '16 at 05:32
  • I seem to be the only one to understand what I'm trying to accomplish. Forget my script. How do I convert a 3D vector, defined by 6 (or 3) points into 2 angles (technically 3, but I won't need roll, just yaw and pitch)? What my code does is make a 3D vector, for example, (0,0,0) to (1,0,0.4) based on mouse coordinates. I have the math to convert that into 2 angles, though 1 of these angles is wrong. The yaw (angle on the xy plane) is the direction from 0,0 to 1,0. The pitch is where I'm stuck. Read the link at the top. I'm trying to do that **in reverse**, as in starting from the end. – Anixias Jan 05 '16 at 05:37

1 Answers1

2
yaw = atan(y/x)
pitch = atan(z/sqrt(x^2+y^2))

That is using one convention for the definition of x, y, z, pitch and yaw, and sure enough it turns 3 coordinates into two angles. If it doesn't work for you -- and it probably won't -- then you must put some more effort into understanding the mathematics involved and defining what you're trying to do.

EDIT: To use the convention of the other question you cite,

alpha = atan(z/x)
beta = atan(y/z)
Beta
  • 96,650
  • 16
  • 149
  • 150