In A-Frame. I need to output the roll, pitch and yaw as I move the view of <a-videosphere>.
You can do document.querySelector('a-videosphere').getAttribute('rotation'). The result will be a {x, y, z} object in degrees.
If you want the direct THREE.Vector3 object in radians, you can do document.querySelector('a-videosphere').object3D.rotation.
Related
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;
}
Is there a way to convert that data:
Object position which is a 3D point (X, Y, Z),
Camera position which is a 3D point (X, Y, Z),
Camera yaw, pitch, roll (-180:180, -90:90, 0)
Field of view (-45°:45°)
Screen width & height
into the 2D point on the screen (X, Y)?
I'm looking for proper math calculations according to this exact set of data.
It's difficult, but it's possible to do it for yourself.
There are lots of libraries that do this for you, but it is more satisfying if you do it yourself:
This problem is possible and I have written my own 3D engine to do this for objects in javascript using the HTML5 Canvas. You can see my code here and solve a 3D maze game I wrote here to try and understand what I will talk about below...
The basic idea is to work in steps. To start, you have to forget about camera angle (yaw, pitch and roll) as these come later and just imagine you are looking down the y axis. Then the basic idea is to calculate, using trig, the pitch angle and yaw to your object coordinate. By this I mean imagining that you are looking through a letterbox, the yaw angle would be the angle in degrees left and right to your coordinate (so both positive and negative) from the center/ mid line and the yaw up and down from it. Taking these angles, you can map them to the x and y 2D coordinate system.
The calculations for the angles are:
pitch = atan((coord.x - cam.x) / (coord.y - cam.y))
yaw = atan((coord.z - cam.z) / (coord.y - cam.y))
with coord.x, coord.y and coord.z being the coordinates of the object and the same for the cam (cam.x, cam.y and cam.z). These calculations also assume that you are using a Cartesian coordinate system with the different axis being: z up, y forward and x right.
From here, the next step is to map this angle in the 3D world to a coordinate which you can use in a 2D graphical representation.
To map these angles into your screen, you need to scale them up as distances from the mid line. This means multiplying them by your screen width / fov. Finally, these distances will now be positive or negative (as it is an angle from the mid line) so to actually draw it on a canvas, you need to add it to half of the screen width.
So this would mean your canvas coordinate would be:
x = width / 2 + (pitch * (width / fov)
y = height / 2 + (yaw * (height / fov)
where width and height are the dimensions of you screen, fov is the camera's fov and yaw and pitch are the respective angles of the object from the camera.
You have now achieved the first big step which is mapping a 3D coordinate down to 2D. If you have managed to get this all working, I would suggest trying multiple points and connecting them to form shapes. Also try moving your cameras position to see how the perspective changes as you will soon see how realistic it already looks.
In addition, if this worked fine for you, you can move on to having the camera be able to not only change its position in the 3D world but also change its perspective as in yaw, pitch and roll angles. I will not go into this entirely now, but the basic idea is to use 3D world transformation matrices. You can read up about them here but they do get quite complicated, however I can give you the calculations if you get this far.
It might help to read (old style) OpenGL specs:
https://www.khronos.org/registry/OpenGL/specs/gl/glspec14.pdf
See section 2.10
Also:
https://www.khronos.org/opengl/wiki/Vertex_Transformation
Might help with more concrete examples.
Also, for "proper math" look up 4x4 matrices, projections, and homogeneous coordinates.
https://en.wikipedia.org/wiki/Homogeneous_coordinates
I'm not quite sure if my question has been asked before in general terms. Whenever it has been asked, it is in terms of a specific language.
Let's say I have an objects rotation represented as a quaternion q = {w, x, y, z}.
I want to rotate this quaternion A radians around the y axis.
Since quaternions are already a measure of rotation, should I just add (or multiply) another quaternion representing the desired rotation to q? How would one go about this?
Thanks for your help.
You are right
Since quaternions are already a measure of rotation, should I just add (or multiply) another quaternion representing the desired rotation to q?
You should multiply current rotation quaternion with desired rotation quaternion. Depending on, local frame "Y" or global frame "Y" you should multiply from left to right or right to left.
I have the coordinates of a centre point . I also have an array called the asteroid normal which I assume is the relative rotation of the axis (its 3 numbers between zero and one).
How can I make an object revolve around this object? I haven't been able to find any formula that does this.
Try this:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslate(-x,-y,-z);
glRotate(angle,nx,ny,nz);
glTranslate(x,y,z);
Use the rotation matrix for an axis and angle. The new position p' of a point p on the object is
p' = center + R(angle, axis) * (p - center)
where R(angle, axis) is the matrix that rotates by angle about axis, and center is a point that the axis passes through. Tal Darom's answer is the same, only in OpenGL notation.
Hey I can't figure out what the equation to find the new angle of travel of an object is after reflecting off of a wall.... The angle of travel is also based off the unit circle so 0degrees would be traveling right, 180 traveling left, 270 down, etc.
Just making the angle negative doesn't work either, any tips?
I think this is what you're looking for. I added in the angle of the wall, even if you didn't need it.
reflectionAngle = wallAngle + ((wallAngle + 180) - (incidenceAngle + 180))
If the wall is just vertical, its angle would be 90 degrees.
I hope this helps, and good luck!
EDIT: As a more simplified method, posted by Casey below:
reflectionAngle = 2*wallAngle - incidenceAngle
You have to change the angle relative to the wall coordinate system (t, n) and then transform back to (x, y) coordinates. The wall coordinate n is perpendicular to the wall; the direction t is created by taking the cross-product of the t-vector into the z-direction.
The algorithm would say that the incoming (v_t, v_n) velocity are changed as follows:
perpendicular component v_n changes sign.
tangential component v_t is unchanged, assuming no friction.
Once you have those, transform back to (x, y) coordinates.
It's easy if you think in terms of 2D vectors.
It would be
outAngle = 360 - inAngle