TSpline class


Here is script for spline created from 2 point and 2 control points:


                          
  1. SPLINE_XYZ <- 0
  2. SPLINE_XY <- 1
  3. SPLINE_YZ <- 2
  4. SPLINE_XZ <- 3
  5.  
  6. class TSpline
  7. {
  8. A = 0;
  9. B = 0;
  10. C = 0;
  11. D = 0;
  12. E = 0;
  13. F = 0;
  14. G = 0;
  15. H = 0;
  16. I = 0;
  17. J = 0;
  18. K = 0;
  19. L = 0;
  20.  
  21. mode = SPLINE_XYZ;
  22.  
  23. constructor()
  24. {
  25.  
  26.  
  27. }
  28.  
  29. function CreateSpline(p0, n0, p1, n1, mode, n0_mag , n1_mag )
  30. {
  31. local x0 = 0.0;
  32. local y0 = 0.0;
  33. local z0 = 0.0;
  34. local x1 = 0.0;
  35. local y1 = 0.0;
  36. local z1 = 0.0;
  37. local x2 = 0.0;
  38. local y2 = 0.0;
  39. local z2 = 0.0;
  40. local x3 = 0.0;
  41. local y3 = 0.0;
  42. local z3 = 0.0;
  43.  
  44.  
  45. x0 = p0.x
  46. y0 = p0.y
  47. z0 = p0.z
  48.  
  49.  
  50. x1 = n0.x * n0_mag + p0.x
  51. y1 = n0.y * n0_mag + p0.y
  52. z1 = n0.z * n0_mag + p0.z
  53.  
  54. x2 = n1.x * n1_mag + p1.x
  55. y2 = n1.y * n1_mag + p1.y
  56. z2 = n1.z * n1_mag + p1.z
  57.  
  58. x3=p1.x
  59. y3=p1.y
  60. z3=p1.z
  61.  
  62. //x coefficients...
  63. this.A = x3 - (3.0 * x2) + (3.0 * x1) - x0
  64. this.B = (3.0 * x2) - (6.0 * x1) + (3.0 * x0)
  65. this.C = (3.0 * x1) - (3.0 * x0)
  66. this.D = x0
  67.  
  68. //y coefficients...
  69. this.E = y3 - (3.0 * y2) + (3.0 * y1) - y0
  70. this.F = (3.0 * y2) - (6.0 * y1) + (3.0 * y0)
  71. this.G = (3.0 * y1) - (3.0 * y0)
  72. this.H = y0
  73.  
  74. //z coefficients...
  75. this.I = z3 - (3.0 * z2) + (3.0 * z1) - z0
  76. this.J = (3.0 * z2) - (6.0 * z1) + (3.0 * z0)
  77. this.K = (3.0 * z1) - (3.0 * z0)
  78. this.L = z0
  79. }
  80.  
  81. function Interpolate(t)
  82. {
  83. local v = Vector(0, 0, 0)
  84. v.x = ((this.A * (t * t * t)) + (this.B * (t * t)) + (this.C * t) + this.D)
  85. v.y = ((this.E * (t * t * t)) + (this.F * (t * t)) + (this.G * t) + this.H)
  86. v.z = ((this.I * (t * t * t)) + (this.J * (t * t)) + (this.K * t) + this.L)
  87.  
  88. if (this.mode == SPLINE_XY) { v.z = 0 }
  89. if (this.mode == SPLINE_YZ) { v.x = 0 }
  90. if (this.mode == SPLINE_XZ) { v.y = 0 }
  91.  
  92. return v
  93. }
  94. }

                        

and here is example of func_spline_mover class:


                          
  1. /*
  2. File: e:/_3D_Engines/GameStart/Projects/AGFX_Game_ShiftBomber/Scripts/func_spline_mover.nut
  3. Author: AndyGFX
  4. */
  5. Include("Scripts/lib/TSpline.nut")
  6. /*!
  7. @short func_spline_mover
  8. @author AndyGFX
  9. */
  10. class func_spline_mover
  11. {
  12.  
  13. /*<
  14. <Script =
  15. <Name = "player.nut">
  16. <Author = "AndyGFX">
  17. <Description = "Script for player control and moving obstacles logic.">
  18. >
  19. <Parameter =
  20. <spline_start_name = <Name = "Start point name"> <Description = "Set item name as start point"> <Type = "String"> <Default = "none">>
  21. <spline_start_cp_name = <Name = "Start control point name"> <Description = "Set item name as start control point"> <Type = "String"> <Default = "none">>
  22. <spline_start_mag = <Name = "Start control point magn."> <Description = "Set start control point magn."> <Type = "Float"> <Default = 1.0>>
  23. <spline_end_name = <Name = "End point name"> <Description = "Set item name as end point"> <Type = "String"> <Default = "none">>
  24. <spline_end_cp_name = <Name = "End control point name"> <Description = "Set item name as end control point"> <Type = "String"> <Default = "none">>
  25. <spline_end_mag = <Name = "End control point magn."> <Description = "Set end control point magn."> <Type = "Float"> <Default = 1.0>>
  26. <spline_increment = <Name = "Move speed"> <Description = "Set speed"> <Type = "Float"> <Default = 1.0>>
  27. >
  28. >*/
  29.  
  30. spline_start_name = "none"
  31. spline_start_mag = 1.0;
  32. spline_start_cp_name = "none"
  33.  
  34. spline_end_name = "none"
  35. spline_end_cp_name = "none"
  36. spline_end_mag = 1.0;
  37.  
  38. spline_increment = 1.0;
  39.  
  40.  
  41.  
  42. spline = TSpline()
  43.  
  44. time = 0;
  45. /*!
  46. @short OnUpdate
  47. Called during the scene update, each frame.
  48. */
  49. function OnUpdate(item)
  50. {
  51. this.time = this.time + this.spline_increment;
  52. if(time>100) {time=0.0}
  53.  
  54. local pos = this.spline.Interpolate(this.time/100.0)
  55. ItemSetPosition(item,pos)
  56. }
  57.  
  58. /*!
  59. @short OnSetup
  60. Called when the item is about to be setup.
  61. */
  62. function OnSetup(item)
  63. {
  64. local _i_sp = SceneFindItem(g_scene,this.spline_start_name)
  65. local _i_csp = SceneFindItem(g_scene,this.spline_start_cp_name)
  66. local _i_ep = SceneFindItem(g_scene,this.spline_end_name)
  67. local _i_cep = SceneFindItem(g_scene,this.spline_end_cp_name)
  68.  
  69. local cp0 = Vector(0,0,0)
  70. cp0 = _i_csp.position()-_i_sp.position()
  71. //cp0.Normalize()
  72.  
  73. local cp1 = Vector(0,0,0)
  74. cp1 = _i_cep.position()-_i_ep.position()
  75. //cp1.Normalize()
  76.  
  77. this.spline.CreateSpline(_i_sp.position(),cp0,_i_ep.position(),cp1,SPLINE_XYZ,this.spline_start_mag,this.spline_end_mag)
  78.  
  79. }
  80. }

                        

Assign this script to your item and set name of dummy items which are used as input to calc spline path.

Mr.Admin's picture

Nice! And thanks for sharing. I am moving this to the script sharing section as well.

AndyGFX's picture

Oops. Sorry, I forgot that exists. :)

C2D Q6600, 4GB RAM, NV460GTX 1GB GDDR5, Vista Ultimate x64
BlitzMax, Delphi, C/C++, LUA, Java, PLSQL, Director, Action Script, ASM, Perl
http://sites.google.com/site/andygfxproject/
http://andygfx.gfxartist.com/