Topic: Rotating an object along a curve (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=29006" title="Pages that link to Topic: Rotating an object along a curve (Page 1 of 1)" rel="nofollow" >Topic: Rotating an object along a curve <span class="small">(Page 1 of 1)</span>\

 
Blaise
Paranoid (IV) Inmate

From: London
Insane since: Jun 2003

posted posted 02-28-2007 17:27

Hi there!

Wow I haven't posted a new topic in ages, so here goes, flame at will...

I've a question on Actionscript, yet it is truly a mathematical question that may well be answered without any AS knowledge.

I have a flash movie,
Inside my movie I have an Arrow,
My arrow follows the path of a sine wave

code:
WAVE_HEIGHT*Math.sin(n*(Math.PI*2)+WAVE_SPEED)

My arrow always faces 'east', I want my arrow to rotate to the tangent of the curve.

I currently have...

code:
arrow._rotation = WAVE_HEIGHT*Math.sin(n*(Math.PI*2))

But really I just made that up.

Does any brain here have the knowledge I seek to guide and educate?

Cheers,

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana
Insane since: Aug 2000

posted posted 02-28-2007 18:36

Let me restate the problem so I'm sure I understand what you said:

You want to display an arrow traveling along a sine curve and you want the arrow to point along the tangent of the curve as it travels.

If that is a correct restatement of your problem, the solution is relatively simple. It involves a little calculus, but you don't need to understand calculus to understand the solution.

The tangent of any function is defined as the first derivative of that function. Since your function is f(x) = sin(x), the first derivative is f'(x) = cos(x).

The tangent of the curve at any given point is the slope of the curve of that point. That is to say that a straight line with the same slope as the tangent to the curve will touch the curve at a single point. (This isn't quite true for curves such as sin because if you make the line long enough it may touch the curve at multiple point so what we are really talking about is a local tangent and that is what you are interested in here.)

Lets pick a point a on the x-axis where you want to draw your arrow. To draw your arrow moving along the curve, you will later solve the following for a range of a values and display the arrow at each of the chosen as, but for now we will work with only one a.

Since the first derivative of f(x) = sin(x) is f'(x) = cos(x) we can find the slope of the tangent to f(x) at a as f'(a) = cos(a).

This gives you the slope of a straight line that is tangent to sin(x) at a.

We know that the function for a straight line is y = m*x + b where m is the slope of the line and b is the value at which the line crosses the y-axis. So, we can substitute the slope of out tangent into this equation and get
y = cos(a)*x + b.

We also know that the straight line passes through the point (a, sin(a)) because that is the point where the line is tangent to f(x) = sin(x). Substituting this point into the equation we get
sin(a) = cos(a)*a + b.

This gives an equation for b
b = a * sin(a)/cos(a) = a * tan(a)

From this we substitute back into our original equation for a line (y = m*x + b) giving
y = cos(a)*x + a*tan(a).

Now you can pick two points on ether side of a and use those points to draw the arrow along the sine curve. Let's pick a point that is d to the left of a and another that is d to the right of a.

y1 = (a-d) * cos(a) + a*tan(a)
y2 = (a+d) * cos(a) + a*tan(a)

Now draw an arrow from (y1, a-d) to (y2, a+d) and you have drawn one arrow along the curve.

Put this into a loop

for a=min_a to max_a by a_step
{
y1 = (a-d) * cos(a) + a*tan(a)
y2 = (a+d) * cos(a) + a*tan(a)
Draw_Arrow((y1, a-d), (y2,a+d))
}

Your arrow will travel along the sine curve and point along the tangent of the curve.

Note that if you change f(x) to another function, you can find the first derivative of that function and use this same method to create an arrow that will travel along that curve as well.

.



-- not necessarily stoned... just beautiful.

Blaise
Paranoid (IV) Inmate

From: London
Insane since: Jun 2003

posted posted 02-28-2007 19:01

Hyperbole, that was great, thanks.

I read it all, understood only about half, and ended up using cos in my original calculation to be...

code:
arrow._rotation = WAVE_HEIGHT*Math.cos(n*(Math.PI*2));

and it works fantastically. I remember doing all this in Maths a long time ago, so reading this kinda made sense in my subconscious, but I'm not about to write a paper on it.

All done, thanks a bunch!



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu