Move an object in the direction of a bezier curve? - c++

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.

Related

How to move the scaled point and keep the shape of the curve?

For a curve, I wanna move the start and end point to new position, and then how to keep the shape of the curve ?
From the points which circulated on the yellow curve, move them to red curve position which circulated on the red curve, how to keep the shape of the curve on the red curve ?
When you define spline curve you need to define also tangents angles at endpoints, when you move you points to center, you are chenging the slopes of side lines, so if tangent angles are fixed wrt lines, they will make curve change. If you will see carefully corner angles are same on both images. To keep your curves shape you need to change these angles according side lines slope chenges.
So, let me summarize what you want to do here: given a Bezier curve or a B-spline curve and two new locations for the start point and end point, find a new Bezier/B-spline curve that more or less keeps the same shape as original curve.
One way to achieve this is to find a transformation that would transform the start/end points to their new locations, then you can apply the same transformation to all the other control points for the curve. Basically, this is the same as finding the transformation between a line defined by the curve's start point P and end point Q and another line defined by the new point locations P* and Q*. From P and P*, we can find a translation vector. From line PQ and line PQ, we can find a rotation angle. From length |PO| and length |PQ| we can find a scale factor. So, combining the translation vector, rotation angle and the scale factor, we will be able to find the transformation matrix. Apply the transformation matrix onto all the other control points, then you should be able to get the new curve that more or less maintain the same shape as original curve.
If what you really wan to do is to offset the curves, then this is a totally different story. Offset curves are achieved by a far-more complex algorithm (which I will not elaborate here) and we will specify offset distance (could be positive or negative to imply outwards/inwards offset) instead of new locations for the start/end points.

Rotating plane such that it has a certain normal vector

I've got the following problem:
In 3D there's a vector from fixed the center of a plane to the origin. This plane has arbitrary coordinates around this center thus its normal vector is not necessarily the mentioned vector. Therefore I have to rotate the plane around this fixed center such that the mentioned vector is the plane's normal vector.
My first idea was to compute the angle between the vector and the normal vector, but the problem then is how to rotate the plane.
Any ideas?
A plane is a mathematical entity which satisfies the following equation:
Where n is the normal, and a is any point on the plane (in this case the center point as above). It makes no sense to "rotate" this equation - if you want the plane to face a certain direction, just make the normal equal to that direction (i.e. the "mentioned" vector).
You later mentioned in the comments that the "plane" is an OpenGL quad, in which case you can use Quaternions to compute the rotation.
This Stackoverflow post tells you how to compute the rotation quaternion from your current normal vector to the "mentioned" vector. This site tells you how to convert a quaternion into a rotation matrix (whose dimensions are 3x3).
Let's suppose the center point is called q, and that the rotation matrix you obtain has the following form:
This can only rotate geometry about the origin. A rotation about a general point requires a 4x4 matrix (what OpenGL uses), which can be constructed as follows:

Avoid twisting artifacts in spline extrusion?

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.

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 ...

Making an object orbit a fixed point in directx?

I am trying to make a very simple object rotate around a fixed point in 3dspace.
Basically my object is created from a single D3DXVECTOR3, which indicates the current position of the object, relative to a single constant point. Lets just say 0,0,0.
I already calculate my angle based on the current in game time of the day.
But how can i apply that angle to the position, so it will rotate?
:(?
Sorry im pretty new to Directx.
So are you trying to plot the sun or the moon?
If so then one assumes your celestial object is something like a sphere that has (0,0,0) as its center point.
Probably the easiest way to rotate it into position is to do something like the following
D3DXMATRIX matRot;
D3DXMATRIX matTrans;
D3DXMatrixRotationX( &matRot, angle );
D3DXMatrixTranslation( &matTrans, 0.0f, 0.0f, orbitRadius );
D3DXMATRIX matFinal = matTrans * matRot;
Then Set that matrix as your world matrix.
What it does is it creates a rotation matrix to rotate the object by "angle" around the XAxis (ie in the Y-Z plane); It then creates a matrix that pushes it out to the appropriate place at the 0 angle (orbitRadius may be better off as the 3rd parameter in the translation call, depending on where your zero point is). The final line multiplies these 2 matrices together. Matrix multiplications are non commutative (ie M1 * M2 != M2 * M1). What the above does is move the object orbitRadius units along the Z-axis and then it rotates that around the point (0, 0, 0). You can think of rotating an object that is held in your hand. If orbitRadius is the distance from your elbow to your hand then any rotation around your elbow (at 0,0,0) is going to form an arc through the air.
I hope that helps, but I would really recommend doing some serious reading up on Linear Algebra. The more you know the easier questions like this will be to solve yourself :)