Interpolation of quaternion orientations and vector positions - math

I have a camera in 3d space that is defined by quaternion and position vector (q1 and p1)
I want to move camera to another viewpoint defined by another pair of quaternion and vector (q2 and p2). To achieve smooth animation I interpolate quaternions using spherical linear interpolation and position vectors using linear interpolation. For small camera movements it works ok, but if camera should orbit around model on 180 degrees it looks ugly, because it doesn't orbit model, but goes through it.
So the question is how to interpolate camera position taking into account slerp interpolation of camera orientation?

I found solution for my problem:
Firstly I calculate difference between quaternions q1 and q2 and calculate axis-angle representation from this difference. Then I calculate rotation centre from line p1-p2, rotation axis and angle value:
center = (p1 + p2) * 0.5 + norm(axis X (p2 - p1)) * (0.5 * |p2 - p1| / sin(angle * 0.5) )
and then I just rotate point p1 around center to interpolate camera position

Related

How to get a right rotation(x,y) by move vector(x,y)

I am developing a pool game , use box2d.
I have know the move vector(vtX,vtY) of ball, how to compute the rotation(x,y) when ball is moving.
The ball is 3D sphere, i need the rotations to rotate the ball when it is moving.
ps: in my case, i cant use the 3d physics engine.
You can express the resulting rotation as a combination of a rotation axis and an angle. I highly advise against using Euler angles as this will result in very complicated expressions if done correctly.
If your ball moves by distance (dx, dy), the rotation axis is normalize(cross(up, (dy, dy))), where up is the up-direction. Assuming that this is the z-axis, you get
axis = normalize(-dy, dx, 0)
And the angle is
angle = length(dx, dy) / r,
where r is the ball's radius and length(dx, dy) = sqrt(dx * dx + dy * dy). The angle will be in radians.

Calculate Point collision between a point of a given vector and the edge of a Circle

Lets say I have a point within a circle(not necessarily the origin) moving at a given vector how would I calculate the x and y coordinate of the point where it hits the edge of the circle.
Shift all coordinates by -cx, -cy. Now circle is centered at origin and has equation
x^2+y^2=R^2
Point coordinate (px, py), unit direction vector is (dx,dy). Equation of ray:
x = px + t * dx
y = py + t * dy
Substitute these variables into the circle equation, solve equation, find parameter t>0, then find intersection point (x,y), shift it back by (cx, cy).

Get Camera 2d position by 3 image points (1D)

i have an image and 3 points with following datas for each point:
x and y 2d-world coordinates
x image coordinate
how can i calculate the camera orientation (only left/right) and the 2d-world position?
thanks.
edit: the image is a normal photography (so perspective projection). The world coordinate is a top view of a map, so Orthographic projection).
Given a point in world space, the projection can be expressed as
(x - cx) * cos(phi) - (y - cy) * sin(phi)
proj(x, y) = -----------------------------------------
(x - cx) * sin(phi) + (y - cy) * cos(phi)
cx and cy are the camera position and phi is the camera rotation. The projection will result in a value in camera coordinates (not image coordinates). To transform image coordinates to camera coordinates, usw
cameraX(imageX) = (2 * imageX / W - 1) * tan(fovy / 2) * ratio
W is the pixel width of the image, fovy is the vertical field of view, ratio is the image's aspect ratio.
Then you want to solve the system of equations formed by the three given points. There is an analytic solution, but it is quite complex. So you're left with numerical (probably least-squares) solvers. Pick one, plug in the formula and get your result. Since you optimize for both a position and an angle, you may want to normalize the values so that they have a similar range. I got quite good results with levmar for similar problems if you're unsure what optimizer to use.
This all assumes that the camera does not distort the image.

How do I calculate x, y, z velocity given two rotation angles and a speed?

Another way of saying this question: How do I find the length, width and height of a cuboid given it's diagonal length and 2 rotational angles.
This is for a 3d game where the user can change up/down rotation (UP and DOWN arrow keys), left/right rotation (LEFT and RIGHT arrow keys) and the object can accelerated and reverse (Q and w). Each frame, the objects x, y, z gets updated according to it's current speed and up/down and left/right rotation.
If alpha is the left/right angle and beta is the up/down angle, then
v.x = speed * sin (alpha) * cos(beta)
v.y = speed * sin (beta)
v.z = speed * cos (alpha) * cos(beta)
Assuming, that no rotation will return the direction (0, 0, 1)
I'm assuming that this cuboid is measured using a static frame of reference, where the diagonal starts at the origin and extends to some other point. If not, this question has no definitive answer, as a diagonal length alone can not determine the width, height and length of some arbitrary cuboid, as there are an infinite number of cuboids that could have the same diagonal.
It sounds like what you're using is a spherical coordinate system: http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates
From the article:
x = r sin θ cos φ
y = r sin θ sin φ
z = r cos θ
r is your diagonal length. You'll have to determine θ and φ based on your rotation angles; they may not be proper inclination and azimuth angles. See the article for details on how these angles are defined in spherical coordinates.

Given start point, angles in each rotational axis and a direction, calculate end point

I have a start point in 3D coordinates, e.g. (0,0,0).
I have the direction I am pointing, represented by three angles - one for each angle of rotation (rotation in X, rotation in Y, rotation in Z) (for the sake of the example let's assume I'm one of those old logo turtles with a pen) and the distance I will travel in the direction I am pointing.
How would I go about calculating the end point coordinates?
I know for a 2D system it would be simple:
new_x = old_x + cos(angle) * distance
new_y = old_y + sin(angle) * distance
but I can't work out how to apply this to 3 dimensions
I suppose another way of thinking about this would be trying to find a point on the surface of a sphere, knowing the direction you're pointing and the sphere's radius.
First of all, for positioning a point in 3D you only need two angles (just like you only needed one in 2D)
Secondly, for various reasons (slow cos&sin, gimbal lock, ...) you might want to store the direction as a vector in the first place and avoid angles alltogether.
Anyway, Assuming direction is initially z aligned, then rotated around x axis followed by rotation around y axis.
x=x0 + distance * cos (angleZ) * sin (angleY)
Y=y0 + distance * sin (Anglez)
Z=z0 + distance * cos (angleZ) * cos (angleY)
Based in the three angles you have to construct the 3x3 rotation matrix. Then each column of the matrix represents the local x, y and z directions. If you have a local direction you want to move by, then multiply the 3x3 rotation with the direction vector to get the result in global coordinates.
I made a little intro to 3D coordinate transformations that I think will answer your question.
3D Coordinates
First, it is strange to have three angles to represent the direction -- two would be enough. Second, the result depends on the order in which you turn about the respective axes. Rotations about different axes do not commute.
Possibly you are simply looking for the conversion between spherical and Cartesian coordinates.

Resources