Working out 3d destination based on angles and distance - c

I want to work out if I have an object in space, and I know how fast it will travel in a second and at what angle it is going, where it will end up on the x y and z.
`I have already tried using the same equation for x, and y, but neither quite come out right, and I think it is a logic error.
Idealy, I'd want to know: 1, how to calculate it. I'd expect a result of: {x, y, z} where x y and z are finishing coordinates.
Additional code:
dx=distance traveled on x
dy=distance traveled on y
dz=distance traveled on z:
xb = dx * sin(angle1);
y= dy * cos(angle1);
z= dz * cos(angle2);

Related

How to render 3D axes given pitch, roll, and yaw?

I'm attempting to render a simple axes display like the following, except using simple 2D lines.
I have angle values for the pitch, yaw, and roll that the axes should display. (e.g. PI/2, PI/4, PI*3/2)
I have a function that can render 2D lines given start and end points in 2D space.
How can I properly render a rotated set of axes given the angles? I don't care about z-indexing (so if sometimes the lines incorrectly show up on top of each other, that is OK as long as they point the correct direction).
What I've tried
I know that the start points for all the lines will just be in the center of the axis, and we'll say the center is at (0, 0) and the length of the axes will be 100. That leaves me to calculate the endpoints for each of the 3 axes.
I have defined the X axis to be left-to-right, the Y axis to be up-and-down and the Z axis to be back-forward (i.e. out of the screen).
Pitch is rotation around the X axis, roll is rotation around Z axis, and yaw is rotation around Y axis.
To calculate the X-axis end point I've done:
x = cos(roll) * cos(yaw) * 100;
y = sin(-roll) * 100;
To calculate the Y-axis end point I've done:
x = cos(roll + PI/2) * 100;
y = sin(-roll - PI/2) * sin(PI/2 - pitch) * 100;
To calculate the Z-axis end point I've done:
x = cos(PI/2 - yaw) * 100;
y = sin(PI - pitch) * 100;
It's probably evident that I don't really know what I'm doing. I feel like I am taking a very naive approach when I should be using something more advanced like matrices. If it makes a difference, I'm using C, but psuedocode is fine. Any help would be appreciated.
First, you need to agree on an order of the rotations. In the following, I assume the order x, y, z (pitch, yaw, roll).
The end points are simply the column vectors of the according rotation matrix. Then, you need to project the 3d points onto the 2d screen. It seems as if you use a simple orthogonal projection (removing the z-coordinate). So here are the results:
x1 = 100 (cos yaw * cos roll)
y1 = 100 (cos pitch * sin roll + cos roll * sin pitch * sin yaw)
x2 = 100 (-cos yaw * sin roll)
y2 = 100 (cos pitch * cos roll - sin pitch * sin yaw * sin roll)
x3 = 100 (sin yaw)
y3 = 100 (-cos yaw * sin pitch)

Finding the angle a vector makes over variable axes

The typical way to find the angle a vector makes from the x axis (assuming the x axis runs left to right, and the y axis runs bottom to top) is:
double vector_angle = atan2( y , x )
However, I want my axes to have their origin at a point on a circle so that the x axis runs from the point on the edge of the circle through the centre of the circle, and the y axis runs tangent to the circle at that point (which would thus be perpendicular to the x axis).
Assumedly the code would still be the same, but now adjusted by a distance k and an angle theta, perhaps:
double y_position = ( y + k ) * theta;
double x_position = ( x + k ) * theta;
double vector_angle = atan2( y_position, x_position );
But I'm not sure about this. This is a generalised problem for a touch-based application where I would like to have a general way to move a sprite (in cocos2d) using swipe motions which is always a constant distance from the center of a circle.
Here, B is the origin of the vector which could be transformed by a rotation theta. For example, if we transformed the circle and point B by 90 degrees, B would be at (4, 0) and the line B->A would be along the axis at 4 (y = 4). I would like to get the angle in node-space of point B, when under transform.
Arctan returns the correct angle for the vector where the "x axis" is the line perpendicular to your tangent.

MATLAB, interpolation of 3D curve crossing xy plane

In Matlab, I have a 3D-curve (array with 3 coordinates) that crosses the xy-plane in a 3 dimensional coordinate system. The "curve" is just a bunch of x,y,z points and its shape is elliptical that spans across the 3D space and spirals towards the origin. It crosses the xy plane several times and I would like to interpolate the x and y coordinates when the curve intersects the xy plane (i.e. when z=0). How do I do so?
Assuming you have 3 vectors x, y, and z with coordinates,
crossidx = find(diff(sign(z)) ~= 0); % z(zcross) and z(zcross+1) have different signs
z1 = z(crossidx);
z2 = z(crossidx+1);
dz = z2 - z1;
alpha = -z1;
beta = z2;
xcross = (beta*x(crossidx) + alpha*x(crossidx+1))./dz;
ycross = (beta*y(crossidx) + alpha*y(crossidx+1))./dz;
zcross = 0;
hold on; plot3(xcross, ycross, zcross, '*');
It should work for either positive to negative or negative to positive crossing. I wrote the code assuming only 1 zero crossing, but I think it would also work for any number of crossings.

Bounding box for 2 points in a plane

I have 2 points A and B in a plane. What I need to find is the points w, x, y and z so that I can have a uniform bounding box.
The conditions are a line formed by wx and yz are parallel to AB.
Similarly wBz and xAy are parallel must be parallel.
Also note that angle zwx and wxy are right angles. Basically wxyz has to be a square.
z
/ /
B /
/ /
w /
/ y
/ /
/ A
/ /
x
Basically finding w, x, y and z is easy if line AB is parallel to x-axis or if AB is parallel to y-axis. I'm having trouble determining the points w,x,y and z when line AB is in an angle with x-axis (slope of line AB could be positive or negative).
Any comments/suggestions is highly appreciated. Thanks!
Treat A and B as vectors in your plane, (xa, ya) and (xb, yb). Take the vector difference, to generate a vector, C, that points from A to B.
C = A - B = (xa - xb, ya - yb) = (xc, yc)
Rotate this vector 90 degrees in each direction, and scale by a half, to get D = (xd, yd) and E = (xe, ye).
D = (-yc/2, +xc/2)
E = -D = (+yc/2, -xc/2)
Use vector arithmetic to get the four points of the square.
w = B + D
x = A + D
y = A + E
z = B + E
EDIT: Fat fingers.
EDIT2: Forgot the factor of a half.
EDIT3: Vector rotation reference, as requested.
To figure out the vector rotation, one can, in general, perform multiplication with a rotation matrix. In this case, the sin and cos factors of +/- pi/2 end up being +/- 1.
If matrix multiplication isn't your thing, draw on paper (or just imagine) a sample vector in any quadrant. Now rotate the paper 90 deg in either direction and see how the x and y components get swapped around and negated.
neirbowjs answer translated to more optimized solution, if optimization floats your boat.
Vars you know (Ax, Ay, Bx, By);
Vars you solve for (Wx, Wy, Xx, Xy, Yx, Yy,Zx, Zy);
float dx = By - Ay / 2;
float dy = Bx - Ax / 2;
float Wx = Ax - dx;
float Wy = Ay + dy;
float Zx = Ax + dx;
float Zy = Ay - dy;
float Xx = Bx - dx;
float Xy = By + dy;
float Yx = Bx + dx;
float Yy = By - dy;

How to find X if you know Y in a line segment WPF

I have a start point(X1,Y1) and end point(X2,Y2) I am drawing a line segment in WPF between these points , I want to calculate the value of X between this line segment if we know the Y value?
If (x,y) is on the segment, and you know y, and y1 != y2, then
x = x1 + (x2-x1)*((y-y1)/(y2-y1))
If y1 == y2, then any x between x1 and x2 is possible, so you can't really get one solution.
Basic high school geometry: y = mx + b, where m is the slope of the line and b is the value of y at x==0.
This site may be of use to you.

Resources