Archived

This topic is now archived and is closed to further replies.

MechFanatic

3D Spiral

Recommended Posts

MechFanatic    122
I want to make an entity follow a spiral around its path. I have variables which keep its origin and velocity. I thought by taking the cross product of the global forward vector and the velocity and multiplying it by the cos(time - starttime) it would work. But it only works when facing certain angles. If anyone could think of anything I would be very appreciative, my head is spinning! Thanks, MF

Share this post


Link to post
Share on other sites
LilBudyWizer    491
Basically you would have to rotate a circle. The path would be the position of the center of the circle. The circle is in a plane perpendicular to the direction of travel. The easiest way I can think of is to calculate the tangent, normal and binormal of the path. Then you would calculate the (x,y) coordinates of the object as though it was following a circular path centered at the origin of the xy plane, i.e. x=r*cos(t), y=r*sin(t). That gives you an x and y. The position along the path is your actual origin, call it p, and call the normal and binormal n and b. So the position of the object, po, is po=p+x*n+y*b.

Since your normal and binormal are changing directions you will have angular acceleration/decelleration do to twisting of the path. So if you path is a spiral itself making a revolution per second and you object is making a revolution per second around the path then the object will make two revolutions per second. If you didn''t want that effect you wouldn''t use the normal and binormal because they they change direction with the twist of the path. I don''t think the benefit of getting around that is worth the effort it takes to still keep a realistic movement of the object. It would be easier just to change the object to not actually rotate to get one revolution per second.

If you don''t know calculus I can''t help you much with how to calculate the tangent, normal and binormal. If you know it, but it is a bit rusty then I can at least refresh your memory. If you have a vector function r(t) that gives the (x,y,z) coordinates of a position of an object in space as time t, then its derivative is a vector function giving a vector in the direction of travel at time t. The components of that vector is the change in x, y and z with respect to time. A clue if you haven''t made it to Calculus III yet. It''s magnitude is the change in the arclength with respect to time, i.e. how far do you travel along the curve per unit of time. To find the normal you have to normalize that vector, i.e. divide by its magnitude. That gives you a vector function, T(t), that returns a unit vector in the direction of travel at time t.

The derivative of of T(t) will point toward the center of curvature, i.e. with a circle it is towards the center of the circle. I''m not sure exactly what it''s magnitude is, but your normal vector is T''(t)/|T''(t)|, so you have to divide the derivative of T(t) by its magnitude to get a unit normal vector. The binormal is at a right angle to your direction of travel and the normal. So it is the cross product of the tangent and the normal. You can approximate these values. A vector from where you where to where you are normalized approximates the tangent vector. The cross product of the what your tangent vector was with what it is now approximates the binormal. The cross product of those two approximations is approximately your normal. How good the approximation is depends on how small a step you use to calculate it.

Share this post


Link to post
Share on other sites
TerranFury    142
Parametric equations:

x = sin(t) * (INITIAL_RADIUS - t)
y = INITIAL_HEIGHT - t
z = cos(t) * (INITIAL_RADIUS - t)

t = 0 -> INTIAL_RADIUS

Use whetever step size you like.

Share this post


Link to post
Share on other sites
LilBudyWizer    491
Hehe, yeah, I guess a straight line path would be the logical assumption unless stated otherwise. He may want to follow a path around a cylindar instead of a cone though. He might also need a bit of help on how to orbit an arbitrary vector instead of just the y-axis. Its too late and I''m too tired right now though.

Share this post


Link to post
Share on other sites
TerranFury    142
I reread your post, and now realize that the parametric equations I gave you aren't in and of themselves enough, since you need your object to always face the direction it's traveling in. Therefore, the easiest way to do this, I think, is to simulate the physics behind such movement.

To make an object move in a circle, you need to add the appropriate certripital acceleration to the object's velocity vector at each timestep. Centripital acceleration points towards the center of the circle. Its magnitude can be found with the following equation:

ac = v2r

Vector ac will always point towards a line down the middle of your spiral.

Edited by - TerranFury on December 17, 2001 4:17:26 PM

Share this post


Link to post
Share on other sites
MechFanatic    122
Ok, I''ve understoond a lot of what has been said here, but I am still having problems. The way entites are set up is they have 2 variables which are passed through physics code.

velocity and origin

Velocity is of course the velocity vector and origin is its current origin. So each step the origin changes.

How do you think I would need to code this?
Do I need to store the original velocity and origin?

Thanks,
MF

Share this post


Link to post
Share on other sites
MechFanatic    122
Another important question, I have read up on how to calculate the normal, binormal and tangent, but due to the fact that I did not pay attention as much as I should have in Calc 3 I am having trouble decyphering what the equations require to be solved.

At this page: http://www.rit.edu/~mkbsma/calculus/calculus305RAITN/lectures/tannorm.html

I found all the equations, but am lost.
It states that r(t) would be the vector which would mean the normal could be solved by:

T(t) = r''(t) / | r''(t) |
where
r''(t) = (-asint)i + (acost)j + (c)k

I assume i, j, and k are the x, y and z components respectively?
But what are the a and the c values?

| r''(t) | = sqrt( a^2 + c^2 )

Without understanding where the a and c values come from I have trouble solving for this equation.

Any help would be greatly appreciated. I am sorry I didn''t pay more attention in class, I lived in the false pretense that I was at school solely to get a Comp Sci degree and now I am just begining to realize the importance of all other information which was presented to me.

Thanks,
MF

Share this post


Link to post
Share on other sites
LilBudyWizer    491
It is easier to explain them in terms of r(t). r(t) for that r''(t) would be r(t)=(a*cos(t)+l, a*sin(t)+m, c*t+n). That is a spiral about the line (l, m, n) + (0, 0, c)*t, i.e. moving parallel to the z axis. The center of the circle moves down that line and how fast it moves down that line is determined by c. a is the radius of the circle. You should also have a b so that you can control the rate of revolution, i.e. r(t)=(a*cos(b*t*2*pi)+l, a*cos(b*t*2*pi)+m, c*t+n). If you wanted to do one revolution per second, assuming t is in seconds, then b is just 1, if you want to do 2 revolutions per second then b is 2.

The problem with the explaination I gave is that it was assuming the path you were orbiting was not a straight line. If you are following a straight line at a fixed velocity the r''(t) is just your velocity vector and T(t) is that vector normalized. T''(t) is the zero vector, i.e. (0,0,0). That is because any direction orthongal to the path of travel is a valid normal vector. That makes it a bit simplier though. Any orthogonal vector is fine so if your vector is (x,y,z) then an orthogonal vector is (-y,x,0) assuming x and y are not zero. That also makes your binormal (-z*x, -z*y, x^2+y^2).

I''m not sure of Terrian Fury''s centripical acceleration. It isn''t that I think it is wrong, but rather that I get confused when running in circles

Share this post


Link to post
Share on other sites
Timkin    864
Actually T(t) - as given above being equal to r''(t)/|r''(t)| - is the unit tangent vector to the space curve that is defined by the vector function r(t).

So, r(t) is a vector from the origin to a point on the curve at time t. As the point moves along the curve - lets say from point t to point t+dt - then r changes from r(t) to r(t+dt). The tangent vector at point t can be approximated as T(t) ~ (r(t+dt)-r(t))/dt. Taking the limit as dt->0 gives the tangent vector at point t and it can clearly be seen that this is the derivative of r with respect to t (from the method of first principles).

So, to solve your problem you need to know the velocity of the object, given by dS/dt and the direction it is facing, given by T(t). dS/dt is the speed along the space curve and is given by dS/dt = |r''(t)|. The direction the object is facing is given by T(t). So, basically all the information you need is contained in the vector function describing the evolution of a point on the curve, being r(t).

For your spiral problem in 3-d space, the following will suffice for r(t).

r(t) = f(t)*cos(g(t))i + f(t)*sin(g(t))j + h(t)k

Now you are asking what f(t), g(t) and h(t) are. h(t) is just the velocityof the object in the k-unit vector direction. g(t) governs the rate of revolution about the origin. For a linear time revolution setting g(t) = 2*pi*w*t will revolve the point through w revolutions in 1 timestep t. f(t) is the rate of change of the radius about the origin. It is NOT necessarily the same as |r''(t)|. This would only be the case if h(t)=0. I''ll leave it to you to derive an equation suitable to you for the rate of decay of the orbit of the object. (Hey, I''m not going to have ALL the fun!)

I hope this has helped present the solution a little more clearly. If you have any further questions, just holler.

Cheers,

Timkin

Share this post


Link to post
Share on other sites
bschwagg    122
I believe there is a less computationally expensive way to produce this result. If you use a system of differential equations, dx/dt = a*y & dy/dt = b*x. To make an exact circle, a and b would be the same positive constants. To make a slight elliptical shape vary a and b''s value.
so..example:

x = 2.0;
y = 0.0;

...

dx = 2.0*y;
dy = 2.0*x;
x += dx;
y += dy;

...

I think this should work. Thats confidence, eh?

Share this post


Link to post
Share on other sites