# Spline based direction

This topic is 5397 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi and sorry for my terrible english at first... I have a big problem with one of my node animators in spaceship sim. I'm moving a node (spaceship) along a continous spline build using Hermites. Movement is performed perfectly, but now I want to rotate a ship to match the orientation of the spline. And here problems begins... 1. How to calculate a direction that ship is pointing to? I try two follownig methods: - In each Hermite I have start and end tangent, so I try to interpolate them according to position of ship on this Hermite - I also try to store last position and substract it from current position, so I have delta movement vector But both methods fails when angle of a spline is changing drastically (the ship makes a 'quick flip' in this situation)... 2. How to translate this direction to angle measured in radians?. For one axis I have done it using trigonometry but for all three axes? Should one axis (like UP) always be defined? Please, help...

##### Share on other sites
So you have 3 functions, fx(t), fy(t), and fz(t), right? Like so:

fx(t) = axt2 + bxt + cx
fy(t) = ayt2 + byt + cy
fz(t) = azt2 + bzt + cz

To find the slope/tangent of an equation, you find the derivative of the equation. It's a calculus thing, which I'm assuming you have not yet taken, but you can look it up on the internet if you wanna know more. For this specific case, though, you can get the tangent vector of your spline at time t by using the following three functions (the single apostrophe after "f" indicates that it is the first derivative, and thus the rate of change of the position):

fx'(t) = 2axt + bx
fy'(t) = 2ayt + by
fz'(t) = 2azt + bz

(The very basic explanation of how I arrived at these equations is that I multiplied each term by the exponent that was on t, and then I reduced that exponent by 1. axt2 becomes 2axt2-1, or 2axt

##### Share on other sites
Use your current location, and the location of where the ship will be after the next iteration of the movement. Then use atan2() whatever it is that returns an angel between two points

Some pseudo code:

for time_index = 0 to 0.9 step 0.1    currentPos = hermite(time_index)    nextPos = hermite(time_index+0.1)    x = nextPos.x - currentPos.x    y = nextPos.y - currentPos.y    angle = atan2(x,y)    rotateShip currentAngle + anglenext time_index

I'm just guessing here, but it sounds like it would work.

##### Share on other sites
Big thanks for great answers!

• 21
• 16
• 9
• 17
• 13