How to rotate an object so that it is directed exactly to the coordinates of another object? - vector

There is an object. At zero coordinates, the object looks forward, you can say a line comes out of its coordinates. This is the direction of the object. You can imagine with a car. In front of the car is where it looks. Its coordinates in x, y, z are known, as well as its slope in x, y, z in radians or degrees.
The coordinates of the second object are known.
How can you find out how many degrees or radians you need to rotate the car on all axes so that it is directed exactly to the coordinate of the second object?
The object can be rotated along all axes as shown in the figure. The values can be in degrees or radians.
I tried to find a solution, but I found only ready-made functions for this in the Unity, Unreal Engine engines

To find out how many degrees or radians you need to rotate an object along all axes (pitch, yaw, and roll), you can use a Delta Find function. This function takes two rotations as input: the current rotation of the object and the target rotation. It returns the rotation that will make the object face the target rotation.
// Assume that Object1 has a known starting rotation and you want to rotate it to a specific target rotation
FRotator StartingRotation = Object1->GetActorRotation();
FRotator TargetRotation = ...; // Set the target rotation to the desired value
// Calculate the rotation needed to make Object1 face the target rotation
FRotator DeltaRotation = FindDeltaRotation(StartingRotation, TargetRotation);
// Add the calculated delta rotation to the starting rotation to get the final rotation
FRotator FinalRotation = StartingRotation + DeltaRotation;
// Set the rotation of Object1 to the final rotation
Object1->SetActorRotation(FinalRotation);
This will rotate Object1 so that it is facing the target rotation. Note that the Find Delta Rotation function will rotate the object around all axes (pitch, yaw, and roll) as needed to achieve the desired orientation.
All FindDeltaRotation is:
FRotator FindDeltaRotation(const FRotator& A, const FRotator& B)
{
return B - A;
}

Related

How would I get the vector3 rotation needed to rotate towards vector3 coordinates

So the biggest issue with all the answers I've seen is that I cannot use quaternions. I need to rotate a camera to face a vector3 coordinate position but I can only use x, y, and z for the rotation. I've looked for awhile and can't really figure it out.
I have a raycast hitting a point, I use the point for the target coordinates I need the camera to face, using the cameras position I need to get a vector 3 rotation that I can set the camera to in order for the camera to be pointing directly at the coordinates
So the biggest issue with all the answers I've seen is that I cannot use quaternions.
This is plain wrong. If you can use Lua, you can use quaternions. Simply write your own quaternion implementation in pure Lua (or port an existing one).
I need to rotate a camera to face a vector3 coordinate position but I
can only use x, y, and z for the rotation. I've looked for awhile and
can't really figure it out.
An X, Y & Z rotation vector means you're using Euler angles (which still leaves multiple questions concerning orientation and order of rotation application open).
I have a raycast hitting a point, I use the point for the target coordinates I need the camera to face, using the cameras position I need to get a vector 3 rotation that I can set the camera to in order for the camera to be pointing directly at the coordinates
First you'll have to determine the direction the point is from the camera using the camera pos. You haven't specified which vector library you use, so I'll assume the following:
vector.new creates a new vector from a table;
+ and - on two vectors perform addition / subtraction;
the components can be accessed as .x, .y, .z
local direction = raycast_hit_pos - camera_pos
-- x/z-rotation
local function horizontal_rotation(direction)
local xz_dist = math.sqrt(direction.x^2 + direction.z^2)
return math.atan2(direction.y, xz_dist)
end
-- y-rotation
local function vertical_rotation(direction)
return -math.atan2(direction.x, direction.z)
end
-- gets rotation in radians for a z-facing object
function get_rotation(direction)
return vector.new{
x = horizontal_rotation(direction),
y = vertical_rotation(direction),
z = 0
}
end
Depending on orientation and the meaning of your rotation axes you might have to shuffle x, y and z around a bit, flipping some signs.

How to draw elliptical sector with Bresenham's algorithm?

How can I draw filled elliptical sector using Bresenham's algorithm and bitmap object with DrawPixel method?
I have written method for drawing ellipse, but this method uses symmetry and passes only first quadrant. This algorithm is not situable for sectors. Of course, I can write 8 cycles, but I think it's not the most elegant solution of the task.
On integer math the usual parametrization is by using limiting lines (in CW or CCW direction) instead of your angles. So if you can convert those angles to such (you need sin,cos for that but just once) then you can use integer math based rendering for this. As I mentioned in the comment bresenham is not a good approach for a sector of ellipse as you would need to compute the internal iterators and counters state for the start point of interpolation and also it will give you just the circumference points instead of filled shape.
There are many approaches out there for this here a simple one:
convert ellipse to circle
simply by rescaling the smaller radius axis
loop through bbox of such circle
simple 2 nested for loops covering the outscribed square to our circle
check if point inside circle
simply check if x^2 + y^2 <= r^2 while the circle is centered by (0,0)
check if point lies between edge lines
so it should be CW with one edge and CCW with the other. You can exploit the cross product for this (its z coordinate polarity will tell you if point is CW or CCW against the tested edge line)
but this will work only up to 180 deg slices so you need also add some checking for the quadrants to avoid false negatives. But those are just few ifs on top of this.
if all conditions are met conver the point back to ellipse and render
Here small C++ example of this:
void elliptic_arc(int x0,int y0,int rx,int ry,int a0,int a1,DWORD c)
{
// variables
int x, y, r,
xx,yy,rr,
xa,ya,xb,yb, // a0,a1 edge points with radius r
mx,my,cx,cy,sx,sy,i,a;
// my Pixel access (you can ignore it and use your style of gfx access)
int **Pixels=Main->pyx; // Pixels[y][x]
int xs=Main->xs; // resolution
int ys=Main->ys;
// init variables
r=rx; if (r<ry) r=ry; rr=r*r; // r=max(rx,ry)
mx=(rx<<10)/r; // scale from circle to ellipse (fixed point)
my=(ry<<10)/r;
xa=+double(r)*cos(double(a0)*M_PI/180.0);
ya=+double(r)*sin(double(a0)*M_PI/180.0);
xb=+double(r)*cos(double(a1)*M_PI/180.0);
yb=+double(r)*sin(double(a1)*M_PI/180.0);
// render
for (y=-r,yy=y*y,cy=(y*my)>>10,sy=y0+cy;y<=+r;y++,yy=y*y,cy=(y*my)>>10,sy=y0+cy) if ((sy>=0)&&(sy<ys))
for (x=-r,xx=x*x,cx=(x*mx)>>10,sx=x0+cx;x<=+r;x++,xx=x*x,cx=(x*mx)>>10,sx=x0+cx) if ((sx>=0)&&(sx<xs))
if (xx+yy<=rr) // inside circle
{
if ((cx>=0)&&(cy>=0)) a= 0;// actual quadrant
if ((cx< 0)&&(cy>=0)) a= 90;
if ((cx>=0)&&(cy< 0)) a=270;
if ((cx< 0)&&(cy< 0)) a=180;
if ((a >=a0)||((cx*ya)-(cy*xa)<=0)) // x,y is above a0 in clockwise direction
if ((a+90<=a1)||((cx*yb)-(cy*xb)>=0))
Pixels[sy][sx]=c;
}
}
beware both angles must be in <0,360> range. My screen has y pointing down so if a0<a1 it will be CW direction which matches the routione. If you use a1<a0 then the range will be skipped and the rest of ellipse will be rendered instead.
This approach uses a0,a1 as real angles !!!
To avoid divides inside loop I used 10 bit fixed point scales instead.
You can simply divide this to 4 quadrants to avoid 4 if inside loops to improve performance.
x,y is point in circular scale centered by (0,0)
cx,cy is point in elliptic scale centered by (0,0)
sx,sy is point in elliptic scale translated to ellipse center position
Beware my pixel access is Pixels[y][x] but most apis use Pixels[x][y] so do not forget to change it to your api to avoid access violations or 90deg rotation of the result ...

How to find view point coordinates?

I have azimuth , elevation and direction vector of the sun.. i want to place a view point on sun ray direction with some distance. Can anyone describe or provide a link to a resource that will help me understand and implement the required steps?
I used cartesian coordinate system to find direction vector from azimuth and elevation.and then for find
viewport origin.image for this question
x = distance
y = distance* tan azimuth
z = distance * tan elevation.
i want to find that distance value... how?
azimutal coordinate system is referencing to NEH (geometric North East High(Up)) reference frame !!!
in your link to image it is referencing to -Y axis which is not true unless you are not rendering the world but doing some nonlinear graph-plot projection so which one it is?
btw here ECEF/WGS84 and NEH you can find out how to compute NEH for WGS84
As I can see you have bad computation between coordinates so just to be clear this is how it looks like:
on the left is global Earth view and one NEH computed for its position (its origin). In the middle is surface aligned side view and on the right is surface aligned top view. Blue magenta green are input azimutal coordinates, Brown are x,y,z cartesian projections (where the coordinate is on its axis) so:
Dist'= Dist *cos(Elev );
z = Dist *sin(Elev );
x = Dist'*cos(Azimut);
y =-Dist'*sin(Azimut);
if you use different reference frame or axis orientations then change it accordingly ...
I suspect you use 4x4 homogenous transform matrices
for representing coordinate systems and also to hold your view-port so look here:
transform matrix anatomy
constructing the view-port
You need X,Y,Z axis vectors and O origin position. O you already have (at least you think) and Z axis is the ray direction so you should have it too. Now just compute X,Y as alignment to something (else the view will rotate around the ray) I use NEH for that so:
view.Z=Ray.Dir // ray direction
view.Y=NEH.Z // NEH up vector
view.X=view.Y x view.Z // cross product make view.X axis perpendicular to Y ansd Z
view.Y=view.Z x view.X // just to make all three axises perpendicular to each other
view.O=ground position - (distance*Ray.Dir);
To make it a valid view_port you have to:
view = inverse(view)*projection_matrix;
You need inverse matrix computation for that
if you want the whole thing
Then you also want to add the Sun/Earth position computation in that case look here:
complete Earth-Sun position by Kepler's equation
The distance
Now that is clear what is behind you just need to set the distance if you want to set it to Sun then it will be distance=1.0 AU; (astronomical unit) but that is huge distance and if you have perspective your earth will be very small instead use some closer distance to match your view size look here:
How to position the camera so that the object always has the same size

How can I convert 3D space coordinates to 2D space coordinates?

I'm using a Lua API. I'm trying to transform 3D space coordinates to 2D space coordinates. I've asked google but I can't find anything apart from snippets using OpenGL function. I don't need source code or anything just a simple way on how to do it? I can acquire the Camera's perspective, the original position to be transformed, window size and aspect ratio if that's any help? Thanks in advance for any suggestions :)
If you're talking about transforming world-space (x,y,z) coordinates to screen-space (u,v) coordinates, then the basic approach is:
u = x / z;
v = y / z;
If the camera is not at the origin, transform (x,y,z) by the view matrix before the projection matrix. You may also want to adjust for the camera perspective, aspect ratio etc., in which case I'd refer to this Wikipedia article.
My apologies if you're looking for something specific to the Lua API, which I am not familiar with.
For 3d projection you will need a line of field of view.
For example on a 400*400 window ;
200 will be ok.
As you can see in the given image.
Actually you can increase or decrease this 200 value according to your screen size because using wrong value can stretch your 3d model.
IMAGE
So you can use the given formula to convert your 3d points to 2d points.
Here, x or y means new x or y coordinate after perspective projection and oldx and oldy means old x and y coordinates.
x=(200÷(200+z))*(oldx);
Same formula for y also.
Y=(200÷(200+z))*(oldy);
Here is a java code example for converting 3d coordinates to 2d coordinates with depth of z axis.
// 'double[] a' indicates your 3d coordinates.eg: a=[x,y,z];
// int b indicates whether you wanted to return your 2d x coordinate or y coordinate. 0 will return x and 1 will return y.
// if your image stretches then you can adjust the fovl(field of view line).
public static double PERSPECTIVE_PROJECTION(double[] a,int b){
int fovl=200;
double oldpos=a[b];
double z=a[2];
double newpos=(double)(fovl/(fovl+z))*oldpos;
return newpos;
}

3d Parabolic Trajectory

I'm trying to figure out some calculations using arcs in 3d space but am a bit lost. Lets say that I want to animate an arc in 3d space to connect 2 x,y,z coordinates (both coordinates have a z value of 0, and are just points on a plane). I'm controlling the arc by sending it a starting x,y,z position, a rotation, a velocity, and a gravity value. If I know both the x,y,z coordinates that need to be connected, is there a way to calculate what the necessary rotation, velocity, and gravity values to connect it from the starting x,y,z coordinate to the ending one?
Thanks.
EDIT: Thanks tom10. To clarify, I'm making "arcs" by creating a parabola with particles. I'm trying to figure out how to ( by starting a parabola formed by a series particles with an beginning x,y,z,velocity,rotation,and gravity) determine where it will in end(the last x,y,z coordinates). So if it if these are the two coordinates that need to be connected:
x1=240;
y1=140;
z1=0;
x2=300;
y2=200;
z2=0;
how can the rotation, velocity, and gravity of this parabola be calculated using only these variables start the formation of the parabola:
x1=240;
y1=140;
z1=0;
rotation;
velocity;
gravity;
I am trying to keep the angle a constant value.
This link describes the ballistic trajectory to "hit a target at range x and altitude y when fired from (0,0) and with initial velocity v the required angle(s) of launch θ", which is what you want, right? To get your variables into the right form, set the rotation angle (in the x-y plane) so you're pointing in the right direction, that is atan(y/x), and from then on out, to match the usual terminology for 2D problem, rewrite your z to y, and the horizontal distance to the target (which is sqrt(xx + yy)) as x, and then you can directly use the formula in link.
Do the same as you'd do in 2D. You just have to convert your figures to an affine space by rotating the axis, so one of them becomes zero; then solve and undo the rotation.

Resources