Avoid twisting artifacts in spline extrusion? - c++

I am trying to attach 2d shape profiles to a spline curve. At certain points in the spline I get the weird twisting artifacts in my geometry as shown in the image. How can I avoid this using the Frenet-Frame equations?
My current calculations for the normal, binormal and tangent:
forward_tangent_vector = glm::normalize(pointforward - pointmid);
backward_tangent_vector = glm::normalize(pointmid - pointback);
second_order_tangent = glm::normalize(forward_tangent_vector - backward_tangent_vector);
binormal = glm::normalize(glm::cross(forward_tangent_vector,second_order_tangent));
normal = glm::normalize(glm::cross(binormal, forward_tangent_vector));
//translation matrix
T = glm::translate(T, pointmid);
normal_axis = glm::vec3(0, 1, 0);
rotationAxis = glm::cross(normal_axis, forward_tangent_vector);
rotationAngle = glm::acos(glm::dot(normal_axis, forward_tangent_vector));
//rotation matrix
R = glm::rotate(R, glm::degrees(rotationAngle), rotationAxis);

You fell victim to the hairy ball theorem:
A common problem in computer graphics is to generate a non-zero vector in R3 that is orthogonal to a given non-zero one. There is no single continuous function that can do this for all non-zero vector inputs. This is a corollary of the hairy ball theorem. To see this, consider the given vector as the radius of a sphere and note that finding a non-zero vector orthogonal to the given one is equivalent to finding a non-zero vector that is tangent to the surface of that sphere where it touches the radius. However, the hairy ball theorem says there exists no continuous function that can do this for every point on the sphere (i.e. every given vector).
Also see this: http://blog.sigfpe.com/2006/10/oriented-fish-and-hairy-balls.html
The problem lies in these two lines:
normal_axis = glm::vec3(0, 1, 0);
rotationAxis = glm::cross(normal_axis, forward_tangent_vector);
When forward_tangent_vector is colinear with (0,1,0), rotationAxis becomes (0,0,0). That's why you get a jolt in your pipe.
What you need to do instead of hardcoding (0,1,0), is to take the first derivative of the spline (velocity/tangent vector), take the second derivative of the spline (acceleration/normal vector), and take their cross products (binormal). Normalize these three vectors and you get the so-called Frenet-frame, a set of 3 mutually perpendicular vectors around the spline.
Note that your spline has to be C2-continuous, otherwise you would get similar "twists" caused by the discontinuities in the second derivative (aka. acceleration/normal vector).
Once you have the Frenet-frame, it's a matter of a simple change of basis to work in that coordinate system. Don't mess around with glm::rotate, just put the x,y,z unit vectors into a matrix as rows (or columns? I'm not sure what convention GLM uses...) and that'll be your transformation matrix.

Related

calculate rotation matrix from 4 points

I would like to stick a moving car to a curvy terrarian. I can calculate the y coordinate (which is height in my case) for each wheel. These 4 points forms a plane. I don't know how to calculate the rotation matrix from these 4 pints so I can apply it to the car. So this is what I would like to achieve:
calculateRotationMatrix(p1,p2,p3,p4);
BTW I am using c++ and openGL.
Could anybody help me out here?
If you guarantee that all 4 points lie on one plane, then the problem is not that hard to solve: Let's call the points (A,B,C,D) and we define a up vector (UP = [0,1,0])
1) Calculate the plane normal (N)
N = normalize(cross(B-A, C-A));
2) Calculate the rotation axis (R)
R = normalize(cross(N,UP))
3) Calculate rotation angle (alpha)
alpha = dot(N, UP)
The resulting matrix is then the one that rotates around R by an angle of alpha. If your matrix library does not support creating rotation axis around arbitrary axis, you can find the form here.
Note, that there is a singularity when alpha is very small (N will then vanish), so you should only calculate the matrix if alpha is sufficiently large. It might also be that case that some of the vectors point to the opposite direction depending on the winding order in which the points are defined. In this case just switch the two parameters of the cross function.

Analytic method for calculate a mirror angle

I have in a 3D space a fixed light ray Lr and a mirror M that can rotate about the fixed point Mrot, this point is not on the same plane of the mirror, in other words the mirror plane is tangent to a sphere centered in Mrot with a fixed radius d. With that configuration I want to find an equation that receives point P as parameter and results with the rotation of the mirror in a 3D space.
We can consider that the mirror plane has no borders (infinite plane) and it's rotation have no limits. Also, the mirror reflects only on the opposite side of its rotation point.
In the picture are two cases with different input point P1and P2, with their respective solution angles alpha1 and alpha2. The pictures are in 2D to simplify the drawings, the real case is in 3D.
At the moment I am calculating the intersection with the mirror plane in a random rotation, then calculate the ray reflection and see how far is from the point (P) I want to reach. Finally iterate with some condition changing the rotation until it match.
Obviously it's an overkill, but I can't figure it out how to code it in an analytic way.
Any thoughts?
Note: I have noticed that if the mirror rotates about a point (Mrot) contained in it's plane and the ray light is reaching that point (Mrot) I can easily calculate the the mirror angle, but unfortunately is not my case.
First note that there is only one parameter here, namely the distance t along the ray at which it hits the mirror.
For any test value of t, compute in order
The point at which reflection occurs.
The vectors of the incident and reflected rays.
The normal vector of the mirror, which is found by taking the mean of the normalized incident and reflected vectors. Together with 1, you now know the plane of the mirror.
The distance d of the mirror to the rotation point.
The problem is now to choose t to make d take the desired value. This boils down to an octic polynomial in t, so there is no analytic formula[1] and the only solution is to iterate.[2]
Here's a code sample:
vec3 r; // Ray start position
vec3 v; // Ray direction
vec3 p; // Target point
vec3 m; // Mirror rotation point
double calc_d_from_t(double t)
{
vec3 reflection_point = r + t * v;
vec3 incident = normalize(-v);
vec3 reflected = normalize(p - reflection_point);
vec3 mirror_normal = normalize(incident + reflected);
return dot(reflection_point - m, mirror_normal);
}
Now pass calc_d_from_t(t) = d to your favourite root finder, ensuring to find the root with t > 0. Any half-decent root finder (e.g. Newton-Raphson) should be much faster than your current method.
[1] I.e. a formula involving arithmetic operations, nth roots and the coefficients.
[2] Unless the octic factorises identically, potentially reducing the problem to a quartic.
I would do it as 2 separate planar problems (one in xy plane and second in xz or yz plane). The first thing that hits my mind is this iterative process:
start
mirror is turning around Mrot in constant distance creating circle (sphere in 3D)
so compute first intersection of Lr and sphere
or find nearest point on sphere to Lr if no intersection found
compute n0 normal as half angle between Lr and red line from intersection to P
this is mirror start position
place mirror (aqua) to n0 angle
compute reflection of Lr
and compute half angle da0 this is step for new iteration
add da0 to n0 angle and place mirror to this new angle position
compute reflection of Lr
and compute half angle da1 this is step for new iteration
loop bullet 3 until
da(i) is small enough
max number of iteration is reached
[Notes]
This should converge into solution more quickly then random/linear probing
the more distant P from mirror (or smaller radius of rotation) the quicker convergence there is
Not sure if analytic solution to this problem even exists it looks like it would lead to transcendent system ...

Rotating object along all 3 axes to map to the player's camera LookAt vector

I have a simple 3D LookAt vector, and I wish to rotate the player model (a simple cube) to show where the player/cube is looking at.
For sideways camera movement I've managed to figure it out and do the following:
glTranslatef(position.x, position.y, position.z);
glRotatef(atan2(lookAt.z, lookAt.x) * 180 / PI, 0, 1, 0);
Now I know that to get up-down camera movement to map to the rendered cube model, I need to rotate the cube around it's x and z axes as well, but I can't seem to figure out what formula to use for those two.
OpenGL will rotate the whole coordinate system (whole space, not only a cube) so after first rotation you just need to rotate only around z axis.
// first rotation
glRotatef(-atan2(lookAt.z, lookAt.x) * 180 / PI, 0, 1, 0);
// second rotation
float d = sqrt(pow(lookAt.x,2) + pow(lookAt.z,2));
float pitch = atan2(lookAt.y, d);
glRotatef(pitch * 180 / PI, 0, 0, 1);
First and second rotation:
I assume your model is looking along x axis (red arrow). I also assume lookAt is given relative to the position of the model.
If you're familiar with matrix math, matrices are an easier way to think about it. If you're not familiar with matrices, this series explains how to use them to solve common game development problems: https://www.youtube.com/playlist?list=PLW3Zl3wyJwWNQjMz941uyOIq3Nw6bcDYC Getting good with matrices is a good idea if you want to be a 3D game programmer.
For your problem, you want to make a translation/rotation matrix which will transform the box to the proper place for you. You can make a translation matrix and a rotation matrix individually, and then at the end take the product of the two. I'll try to break that down.
The translation matrix is simple, if your position is then your matrix will be
To construct a rotation matrix, you need to rotate the standard basis vectors the way you want. Then when you create a matrix from those rotated basis vectors, the matrix will rotate other vectors in the same way. As an example of that, take the standard basis vectors:
Now I'm going to rotate and around by 90 degrees clockwise:
Now put them into a matrix:
and you have R is a matrix that rotates things around by 90 degrees.
In your case you want to rotate stuff such that it faces a vector that you provide. That makes things easy, we can calculate our basis vectors from that vector. If your vector is then and we can solve for the other two basis vectors using cross products. You know that the character won't ever roll their view (right?) so we can use the global up vector as well. I'll call the global up vector . In your case you're using y as the "up" dimension so the global up vector will be
Then:
In the first line you do a cross product between the view vector and the up vector to get a vector orthogonal to both - this will serve as the third basis vector after it is normalized, which is the second line. In the third line another cross product generates the second basis vector. These three vectors represent what happens when the standard basis vectors are rotated the way you want them to be. Use them as the columns in a matrix like so:
Now the last step in the math is to make a final matrix that will do both translation and rotation, and this step is easy:
Then load that matrix into OpenGL with glLoadMatrix:
glLoadMatrixf(&M);
All of this gets explained in the video series I linked as well :)

Rounding a 3D point relative to a plane

I have a Plane class that holds n for normal and q for a point on the plane. I also have another point p that also lies on that plane. How do I go about rounding p to the nearest unit on that plane. Like snapping a cursor to a 3D grid but the grid can be rotating plane.
Image to explain:
Red is the current point. Green is the rounded point that I'm trying to get.
Probably the easiest way to achieve this is by taking the plane to define a rotated and shifted coordinate system. This allows you to construct the matrices for transforming a point in global coordinates into plane coordinates and back. Once you have this, you can simply transform the point into plane coordinates, perform the rounding/projection in a trivial manner, and convert back to world coordinates.
Of course, the problem is underspecified the way you pose the question: the transformation you need has six degrees of freedom, your plane equation only yields three constraints. So you need to add some more information: the location of the origin within the plane, and the rotation of your grid around the plane normal.
Personally, I would start by deriving a plane description in parametric form:
xVec = alpha*direction1 + beta*direction2 + x0
Of course, such a description contains nine variables (three vectors), but you can normalize the two direction vectors, and you can constrain the two direction vectors to be orthogonal, which reduces the amount of freedoms back to six.
The two normalized direction vectors, together with the normalized normal, are the base vectors of the rotated coordinate system, so you can simply construct the rotation matrix by putting these three vectors together. To get the inverse rotation, simply transpose the resulting matrix. Add the translation / inverse translation on the appropriate side of the rotation, and you are done.

Move an object in the direction of a bezier curve?

I have an object with which I would like to make follow a bezier curve and am a little lost right now as to how to make it do that based on time rather than the points that make up the curve.
.::Current System::.
Each object in my scene graph is made from position, rotation and scale vectors. These vectors are used to form their corresponding matrices: scale, rotation and translation. Which are then multiplied in that order to form the local transform matrix.
A world transform (Usually the identity matrix) is then multiplied against the local matrix transform.
class CObject
{
public:
// Local transform functions
Matrix4f GetLocalTransform() const;
void SetPosition(const Vector3f& pos);
void SetRotation(const Vector3f& rot);
void SetScale(const Vector3f& scale);
// Local transform
Matrix4f m_local;
Vector3f m_localPostion;
Vector3f m_localRotation; // rotation in degrees (xrot, yrot, zrot)
Vector3f m_localScale;
}
Matrix4f CObject::GetLocalTransform()
{
Matrix4f out(Matrix4f::IDENTITY);
Matrix4f scale(), rotation(), translation();
scale.SetScale(m_localScale);
rotation.SetRotationDegrees(m_localRotation);
translation.SetTranslation(m_localTranslation);
out = scale * rotation * translation;
}
The big question I have are
1) How do I orientate my object to face the tangent of the Bezier curve?
2) How do I move that object along the curve without just setting objects position to that of a point on the bezier cuve?
Heres an overview of the function thus far
void CNodeControllerPieceWise::AnimateNode(CObject* pSpatial, double deltaTime)
{
// Get object latest pos.
Vector3f posDelta = pSpatial->GetWorldTransform().GetTranslation();
// Get postion on curve
Vector3f pos = curve.GetPosition(m_t);
// Get tangent of curve
Vector3f tangent = curve.GetFirstDerivative(m_t);
}
Edit: sorry its not very clear. I've been working on this for ages and its making my brain turn to mush.
I want the object to be attached to the curve and face the direction of the curve.
As for movement, I want to object to follow the curve based on the time this way it creates smooth movement throughout the curve.
You should have a curve in parametric form and use derivative vector to evaluate the rotation of your object (rotation angle = derivative angle) as #etarion said.
To move the object on a curve with a desired velocity ( i think it is the think you want) each simulation step you shoud estimate the distance the point should move on this step.
The simplest estimation is dist = derivative.length()*TIMER_STEP. When you know the dist should be traversed on the current step and t0 - the current parameter of the curve you can simply increment t0 by some little value epsilon and check the traversed distance is still smaller then estimated. Repeat this until the traversed distance (while increasing t0) is >= estimated. This will be the new current parameter t0 for the next step
EDIT:
Didn't notice first you are in 3d. In 3d space you can't unambiguously define the position of an object on a curve even if you know the initial position. Just imagine your curve is a line - the object still can rotate around the line. This angle is not defined by a curve.
I would do something like this. Let's bind a vector to the object so that in the beginning of the movement (curve parameter t = 0 for example) the object vector direction coincide with the derivative vector. Then during the movement this vector should still coincide with the derivative in each point of a curve. So you will know this object vector and will be able to setup your object according to this vector. But you will still have one degree of freedom.
For example you can say that object does not rotate around this vector.
Knowing the object vector and the angle of rotation around it you can restore the object orientation in 3d world.
PS: such object vector and a rotation angle around it is called quaternion - so you can use quaternion math (simple copy the required formula) to calculate the object rotation matrix!
here are the formulas http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm
You need a parametric formulation of your curve.
The big question I have are
1) How do I orientate my object to
face the tangent of the Bezier curve?
If you have the curve in parametric form, the tangential direction is the derivative of the position wrt. t.
2) How do I move that object along the
curve without just setting objects
position to that of a point on the
bezier cuve?
I'm not sure I get your question - you'd increase t in your parametric form in a small step and update position and direction. You still have a degree of freedom there, btw - the "up" direction is not determined from the curve, so you'd need to take care of that too.
I'm assuming you have all points you need to define the bezier curve.
Then you can compute every point on that curve. Calculate a suitable point taking into account the speed that the object should travel at, and the frame timing and you should have consistent movement.
The vector formed by the points from last and current frame can be used as a rough estimate of the tangent in most cases; e.g. when the curve does not bend too sharp.
Edit:
also have a look here on how to calculate the length of a bezier curve. You will need to transform that, so you can calculate a point on your curve (or rather the t) for a given length. Then just move evenly distances in relation to time and you should be fine.
One approach is to calculate a pyramid of points. The bottom layer is your transformed control points. Now for a given t, and for each each pair of adjacent points, create a new point that is on this line segment weighted by t. This new set of points forms the next layer in the pyramid. Repeat until the current layer has only 1 point. This point is your position. Note that the second layer from the top has 2 points, and determines the tangent line.