🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Cubic splines + Lack of math knowledge = ?

Started by
4 comments, last by Almar 23 years, 6 months ago
Hi, I''ve asked things on this board before, and you''ve all helped me very much. Now, I''ve got a problem which I''m just unable to solve. I just can''t understand Cubic Splines, and how to get them to work. It''s probably just my lack of Math... I''m programming in Visual Basic6, but that shouldn''t matter. I can understand C code pretty well. Back to the problem: My game is played in a 2D Top-Down style, a''la Subspace or Netrek. I used to have a simple linear dead reckoning, but it still resulted in ''weird'' movement, with the turning of ships. The ships also moved in ''straight'' lines. My ships have,concerning the position and velocity, the following data: Public type ExampleShip vecVelocity As D3DVECTOR2 ''The velocity of this sprite. vecLocation As D3DVECTOR2 ''The location of this sprite. sngAngle As Single End Type FYI: A D3DVector2 is just [X,Y] Now I was thinking that vecVelocity should be an array of four entries. 1 Start point, 2 center points, and 1 endpoint. (I''ve read the GameDev doc about this, so this should be all right) The Position, and velocity is simply calculated using Cos & Sin: ''Velocity: With ExampleShip .vecVelocity.X = vecVelocity.X - TrigMath.TrigMathSin(.sngAngle) .vecVelocity.Y = vecVelocity.Y - -TrigMath.TrigMathCos(.sngAngle) End With ''Position: With ExampleShip .vecLocation.X = .vecLocation.X + .vecVelocity.X .vecLocation.Y = .vecLocation.Y + .vecVelocity.Y End With But further? Are those equations in the GameDev doc right for all types of Cubic SPlines? Please help and any help appreciated, Almar Joling ajoling@quadrantwars.com
Advertisement
How much you know about matrix math? Time to learn...

r(t) = 0.166·j·t³ + 0.5·a·t² + v·t + r°·t°
r: position (r° should be r sub-0, but there''s no ascii for that)
j: jerk
a: acceleration
v: velocity
r°: initial position
t: time

Now thats the ideal physics, its often desirable to twiddle the factor on the j & a to make them have less effect than they "should".

You''ll need a set of coef for the x & for the y components. Lucky you can treat them independantly and use the same math & functions to calculate both.

So the plan is, everytime you get a position update, you toss it in a short stack of the last 4 known positions, along with the time. So you''ll need something like this:
D3DVECTOR2 r(4)
int t(4) ''maybe a float, er single?

You may want to use a D3DMatrix3D, cause that''s what you''ll eventually need.

to figure out the coef ya need to do this:
[t(0)^3, t(0)^2, t(0), 1]
[t(1)^3, t(1)^2, t(1), 1]
[t(2)^3, t(2)^2, t(2), 1]
[t(3)^3, t(3)^2, t(3), 1]

invert it

and multiply by
[r(0).x]
[r(1).x]
[r(2).x]
[r(3).x]

the resultant vector will be
[j, a, v, r°] for x

repeat for y

...
There''s another way, using krammer''s rule, but I think it requires more math.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Okay, you''ve totally confused me with that message, but since it''s the most important part of my game, I''ll try very hard to understand what you mean.
I''ll copy and paste some stuff, to make it more readable :o)


r(t) = 0.166·j·t³ + 0.5·a·t² + v·t + r°·t°
r: position (r° should be r sub-0, but there''s no ascii for that)
j: jerk
a: acceleration
v: velocity
r°: initial position
t: time


What exactly is "jerk"? I thought this had to do about the "misplacement" over time. But how should I able to calculate it anyway? And also, how do you get those fixed values like 0.166 and 0.5? Is there any standard for this?

You''re right about D3DVector2. I was already planning to make it an array of 4 (Don''t know how it''s called in C)

I''ve searched the SDK for D3DMatrix3D, but it seems the VB version doesn''t have it. I can only find D3DMatrix, which only has data named "m11" till "m44"

Hmm... I''ve checked out the GameDev article (again, as it''s one of the best sources about the cubic spline), and should I contain velocity and position in every packet? The GameDev article says not (If I read it right) which I find very strange.

Thanks again,
Almar


@#!%%^!$#%@
I hate that, I hit esc on accident and lost my whole friggin msg.
argh...

The method I gave actually needs a 4D matrix, so D3dMatrix will work for it. I think there''s a D3DXMatrix3D & 4D thats part of the utilities that come with the Dx7 examples.

The coef. are from the calculous that is used to derive the equation; its physics based.

Jerk is the rate of change of acceleration (like velocity is the rate of change of position)
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Okay, I''m going to try it here first, see if it''s correct. It will be VB code, but that should be ok. Btw, I will probably make some stupid mistakes using a matrix. This is my first time :o)

I''ll be doing only X...

''Dim our variables
Dim lngT(4) as Long
Dim VecLocation(4) as D3DVector2
Dim matMatrix as D3DMatrix
Dim matReturn as D3DMatrix ''Returned by inverse
Dim sngDeterminant as single ''Also returned by inverse...

''Fill the matrix, doing this right?
With matMatrix
.m11 = lngTime(0)^3
.m12 = lngTime(0)^2
.m13 = lngTime(0)
.m14 = 1
.m21 = lngTime(1)^3
.m22 = lngTime(1)^2
.m23 = lngTime(1)
.m24 = 1
.m31 = lngTime(2)^3
.m32 = lngTime(2)^2
.m33 = lngTime(2)
.m34 = 1
.m41 = lngTime(3)^3
.m42 = lngTime(3)^2
.m43 = lngTime(3)
.m44 = 1
End With

''Invert it:
D3DXMatrixInverse(MatReturn,sngDeterminant, matMatrix)

With MatReturn
.m11 = VecLocation(0).x
.m12 = VecLocation(0).x
.m13 = VecLocation(0).x
.m14 = VecLocation(0).x
.m21 = VecLocation(1).x
.m22 = VecLocation(1).x
.m23 = VecLocation(1).x
.m24 = VecLocation(1).x
.m31 = VecLocation(2).x
.m32 = VecLocation(2).x
.m33 = VecLocation(2).x
.m34 = VecLocation(2).x
.m41 = VecLocation(3).x
.m42 = VecLocation(3).x
.m43 = VecLocation(3).x
.m44 = VecLocation(3).x
End With

And now, fill it in the formula or something?
r(t) = 0.166·j·t³ + 0.5·a·t² + v·t + r°·t°

Thanks in advance,
Almar
No!

You don''t replace those elements of the matreturn. You need to make another matrix with those values in the right place.

[r(0).x, r(1).x, r(3).x, r(4).x]
[0,0,0,0]
[0,0,0,0]
[0,0,0,0]

i think that will work...

...
hum, splines are complicated.
This method will give you one equation that goes through all the points you give it. Its not cubic spline interpolation, btw.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement