TSpline class
Submitted by AndyGFX on Tue, 01/18/2011 - 23:35
Here is script for spline created from 2 point and 2 control points:
-
SPLINE_XYZ <- 0
-
SPLINE_XY <- 1
-
SPLINE_YZ <- 2
-
SPLINE_XZ <- 3
-
-
class TSpline
-
{
-
A = 0;
-
B = 0;
-
C = 0;
-
D = 0;
-
E = 0;
-
F = 0;
-
G = 0;
-
H = 0;
-
I = 0;
-
J = 0;
-
K = 0;
-
L = 0;
-
-
mode = SPLINE_XYZ;
-
-
constructor()
-
{
-
-
-
}
-
-
function CreateSpline(p0, n0, p1, n1, mode, n0_mag , n1_mag )
-
{
-
local x0 = 0.0;
-
local y0 = 0.0;
-
local z0 = 0.0;
-
local x1 = 0.0;
-
local y1 = 0.0;
-
local z1 = 0.0;
-
local x2 = 0.0;
-
local y2 = 0.0;
-
local z2 = 0.0;
-
local x3 = 0.0;
-
local y3 = 0.0;
-
local z3 = 0.0;
-
-
-
x0 = p0.x
-
y0 = p0.y
-
z0 = p0.z
-
-
-
x1 = n0.x * n0_mag + p0.x
-
y1 = n0.y * n0_mag + p0.y
-
z1 = n0.z * n0_mag + p0.z
-
-
x2 = n1.x * n1_mag + p1.x
-
y2 = n1.y * n1_mag + p1.y
-
z2 = n1.z * n1_mag + p1.z
-
-
x3=p1.x
-
y3=p1.y
-
z3=p1.z
-
-
//x coefficients...
-
this.A = x3 - (3.0 * x2) + (3.0 * x1) - x0
-
this.B = (3.0 * x2) - (6.0 * x1) + (3.0 * x0)
-
this.C = (3.0 * x1) - (3.0 * x0)
-
this.D = x0
-
-
//y coefficients...
-
this.E = y3 - (3.0 * y2) + (3.0 * y1) - y0
-
this.F = (3.0 * y2) - (6.0 * y1) + (3.0 * y0)
-
this.G = (3.0 * y1) - (3.0 * y0)
-
this.H = y0
-
-
//z coefficients...
-
this.I = z3 - (3.0 * z2) + (3.0 * z1) - z0
-
this.J = (3.0 * z2) - (6.0 * z1) + (3.0 * z0)
-
this.K = (3.0 * z1) - (3.0 * z0)
-
this.L = z0
-
}
-
-
function Interpolate(t)
-
{
-
local v = Vector(0, 0, 0)
-
v.x = ((this.A * (t * t * t)) + (this.B * (t * t)) + (this.C * t) + this.D)
-
v.y = ((this.E * (t * t * t)) + (this.F * (t * t)) + (this.G * t) + this.H)
-
v.z = ((this.I * (t * t * t)) + (this.J * (t * t)) + (this.K * t) + this.L)
-
-
if (this.mode == SPLINE_XY) { v.z = 0 }
-
if (this.mode == SPLINE_YZ) { v.x = 0 }
-
if (this.mode == SPLINE_XZ) { v.y = 0 }
-
-
return v
-
}
-
}
and here is example of func_spline_mover class:
-
/*
-
File: e:/_3D_Engines/GameStart/Projects/AGFX_Game_ShiftBomber/Scripts/func_spline_mover.nut
-
Author: AndyGFX
-
*/
-
Include("Scripts/lib/TSpline.nut")
-
/*!
-
@short func_spline_mover
-
@author AndyGFX
-
*/
-
class func_spline_mover
-
{
-
-
/*<
-
<Script =
-
<Name = "player.nut">
-
<Author = "AndyGFX">
-
<Description = "Script for player control and moving obstacles logic.">
-
>
-
<Parameter =
-
<spline_start_name = <Name = "Start point name"> <Description = "Set item name as start point"> <Type = "String"> <Default = "none">>
-
<spline_start_cp_name = <Name = "Start control point name"> <Description = "Set item name as start control point"> <Type = "String"> <Default = "none">>
-
<spline_start_mag = <Name = "Start control point magn."> <Description = "Set start control point magn."> <Type = "Float"> <Default = 1.0>>
-
<spline_end_name = <Name = "End point name"> <Description = "Set item name as end point"> <Type = "String"> <Default = "none">>
-
<spline_end_cp_name = <Name = "End control point name"> <Description = "Set item name as end control point"> <Type = "String"> <Default = "none">>
-
<spline_end_mag = <Name = "End control point magn."> <Description = "Set end control point magn."> <Type = "Float"> <Default = 1.0>>
-
<spline_increment = <Name = "Move speed"> <Description = "Set speed"> <Type = "Float"> <Default = 1.0>>
-
>
-
>*/
-
-
spline_start_name = "none"
-
spline_start_mag = 1.0;
-
spline_start_cp_name = "none"
-
-
spline_end_name = "none"
-
spline_end_cp_name = "none"
-
spline_end_mag = 1.0;
-
-
spline_increment = 1.0;
-
-
-
-
spline = TSpline()
-
-
time = 0;
-
/*!
-
@short OnUpdate
-
Called during the scene update, each frame.
-
*/
-
function OnUpdate(item)
-
{
-
this.time = this.time + this.spline_increment;
-
if(time>100) {time=0.0}
-
-
local pos = this.spline.Interpolate(this.time/100.0)
-
ItemSetPosition(item,pos)
-
}
-
-
/*!
-
@short OnSetup
-
Called when the item is about to be setup.
-
*/
-
function OnSetup(item)
-
{
-
local _i_sp = SceneFindItem(g_scene,this.spline_start_name)
-
local _i_csp = SceneFindItem(g_scene,this.spline_start_cp_name)
-
local _i_ep = SceneFindItem(g_scene,this.spline_end_name)
-
local _i_cep = SceneFindItem(g_scene,this.spline_end_cp_name)
-
-
local cp0 = Vector(0,0,0)
-
cp0 = _i_csp.position()-_i_sp.position()
-
//cp0.Normalize()
-
-
local cp1 = Vector(0,0,0)
-
cp1 = _i_cep.position()-_i_ep.position()
-
//cp1.Normalize()
-
-
this.spline.CreateSpline(_i_sp.position(),cp0,_i_ep.position(),cp1,SPLINE_XYZ,this.spline_start_mag,this.spline_end_mag)
-
-
}
-
}
Assign this script to your item and set name of dummy items which are used as input to calc spline path.
Mr.Admin
Wed, 01/19/2011 - 00:39
Permalink
Nice! And thanks for sharing. I am moving this to the script sharing section as well.
AndyGFX
Wed, 01/19/2011 - 01:07
Permalink
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/