Skip to main content
gamedev.net
🔒 Locked

Spline based direction

Started by domin 2 replies 1k views
Original Post
domin
domin
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...
Agony
Agony
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
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
phaelax
phaelax
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.
domin
domin
Big thanks for great answers!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.