align my ship along the spline

BlitzMax Forums/BlitzMax Beginners Area/align my ship along the spline

Hi ! How to align my ship along the spline ? Could you help me with some ideas ?
Thanks !




My code :
Strict


Graphics 800,600,0

Global ListeEnnemis:Tlist = CreateList()
Global ListeTrajectoires:Tlist = CreateList()


Type Ennemi

	Field Id
	Field DebutX 
	Field DebutY
	Field Trajectoire_en_cours
	Field Liste_trajectoires:Tlist = CreateList()
	Field Link_trajectoire:TLink = Liste_trajectoires.Firstlink()
	Field Index
	Field PosX
	Field PosY
	Field hImage:TImage


	' ----------------------------------------------------------------------------------------------
			
	Function  Creer:Ennemi (x,y, NomFichier$, t:trajectoire)

		Local n:Ennemi = New Ennemi
		n.DebutX = x
		n.DebutY = y
		n.hImage = LoadImage (NomFichier$)
		
		n.Trajectoire_en_cours = t.Id
		n.Index = 0
		n.PosX = x + t.x[0]
		n.PosY = x + t.y[0]
	
		ListAddLast n.Liste_trajectoires, String.FromInt(t.id)
		
		n.Link_trajectoire = n.Liste_trajectoires.Firstlink()

		ListAddLast ListeEnnemis, n

	
		Return n
	End Function

	' ----------------------------------------------------------------------------------------------
	
			
	Function Passer_a_autre_trajectoire (v:Ennemi)

		v.Link_trajectoire = v.Link_trajectoire.NextLink()
		If v.Link_trajectoire=Null v.Link_trajectoire=v.Liste_trajectoires.FirstLink()

		v.index = 0
		v.PosX = v.DebutX
		v.PosY = v.DebutY
		
		v.trajectoire_en_cours = String(v.Link_trajectoire.Value()).toint()
		
		
	End Function						
		
	' ----------------------------------------------------------------------------------------------
			
	Function Ajouter_une_trajectoire:Ennemi (v:Ennemi, t:trajectoire)
		ListAddLast (v.Liste_trajectoires, String.FromInt (t.Id))
	End Function

	' ----------------------------------------------------------------------------------------------
		
	Method Afficher(v:Ennemi)
			If v.Index < 100 Then
				v.Index = v.Index + 1
			Else
				v.Index = 0
				Passer_a_autre_trajectoire(v)
			End If
			
			v.PosX = v.DebutX + v.Donner_x (v.Trajectoire_en_cours, v.index)
			v.PosY = v.DebutY + v.Donner_y (v.Trajectoire_en_cours, v.index)

			DrawImage v.hImage, v.PosX, v.PosY
	End Method

	' ----------------------------------------------------------------------------------------------

	Function Donner_x(Id,Index)
		Local x
		For Local t:Trajectoire = EachIn ListeTrajectoires
			If t.Id = Id Then
				x = t.x[Index]
			Exit
			End If
		Next
		Return x
	End Function

	' ----------------------------------------------------------------------------------------------

	Function Donner_y(Id,Index)
		Local y
		For Local t:Trajectoire = EachIn ListeTrajectoires
			If t.Id = Id Then
				y = t.y[Index]
			Exit
			End If
		Next
		Return y
	End Function

		
	' ----------------------------------------------------------------------------------------------
			

End Type

Type Trajectoire

	Field x[102]
	Field y[102]
	Field Id
	
	
	Function Creer:Trajectoire (Id, x1,y1,vx1,vy1,x2,y2,vx2,vy2)

		Local Index
		Local t:Trajectoire = New Trajectoire
		
		'x1  - Starting Point X, y1  - Starting Point Y
		'vx1 - Curve modifier X, vy1 - Curve modifier Y
		'x2  - Ending Point X, y2  - Ending Point Y
		'vx2 - Curve modifier X, vy2 - Curve modifier Y
		
		t.Id = Id
		
		index = 0
		For Local a:Float=0 To 1 Step .01    
			t.x[Index] = x1*(1-a)^3 + 3*vx1*(1-a)^2*a + 3*vx2*(1-a)*a^2 + x2*a^3
			t.y[Index] = y1*(1-a)^3 + 3*vy1*(1-a)^2*a + 3*vy2*(1-a)*a^2 + y2*a^3
			Index = Index + 1
		Next
		ListAddLast (ListeTrajectoires, t)
		Return t
	
	End Function

End Type

' ----------------------------------------------------------------------------------------------
'
' PROGRAMME PRINCIPAL
'
' ----------------------------------------------------------------------------------------------


For Local v:Ennemi = EachIn ListeEnnemis
	v.Afficher(v)
Next

Local t1:Trajectoire
Local t2:Trajectoire
Local t3:Trajectoire


t1 = Trajectoire.Creer (1, 0,0,77,580,587,252,448,574)
t2 = Trajectoire.Creer (2,587,253,518,3,177,240,126,-49)
t3 = Trajectoire.Creer (3,177,240,222,382,383,189,422,402)


Local v1:Ennemi


v1 = Ennemi.Creer(0,0,"ship.png",t1) 


Ennemi.Ajouter_une_trajectoire (v1,t2)
Ennemi.Ajouter_une_trajectoire (v1,t3)


Repeat

	'Cls
	
	For Local v:Ennemi = EachIn ListeEnnemis
		v.Afficher(v)
	Next	
			
	Flip


Until KeyHit (KEY_ESCAPE)



ship.png

This might help.
attack waves
<edit> Hmm, maybe not as it won't align the ship.

would you just need to get the position of the ship and then the next position, use atan2 (nxtx - curx, nxty - cury) and the set that as the drawing angle.

Thanks Diablo :

Strict


Graphics 800,600,0

Global ListeEnnemis:Tlist = CreateList()
Global ListeTrajectoires:Tlist = CreateList()


Type Ennemi

	Field Id
	Field DebutX 
	Field DebutY
	Field Trajectoire_en_cours
	Field Liste_trajectoires:Tlist = CreateList()
	Field Link_trajectoire:TLink = Liste_trajectoires.Firstlink()
	Field Index
	Field PosX
	Field PosY
	Field hImage:TImage


	' ----------------------------------------------------------------------------------------------
			
	Function  Creer:Ennemi (x,y, NomFichier$, t:trajectoire)

		Local n:Ennemi = New Ennemi
		n.DebutX = x
		n.DebutY = y
		n.hImage = LoadImage (NomFichier$)
		
		n.Trajectoire_en_cours = t.Id
		n.Index = 0
		n.PosX = x + t.x[0]
		n.PosY = x + t.y[0]
	
		ListAddLast n.Liste_trajectoires, String.FromInt(t.id)
		
		n.Link_trajectoire = n.Liste_trajectoires.Firstlink()

		ListAddLast ListeEnnemis, n

	
		Return n
	End Function

	' ----------------------------------------------------------------------------------------------
	
			
	Function Passer_a_autre_trajectoire (v:Ennemi)

		v.Link_trajectoire = v.Link_trajectoire.NextLink()
		If v.Link_trajectoire=Null v.Link_trajectoire=v.Liste_trajectoires.FirstLink()

		v.index = 0
		v.PosX = v.DebutX
		v.PosY = v.DebutY
		
		v.trajectoire_en_cours = String(v.Link_trajectoire.Value()).toint()
		
		
	End Function						
		
	' ----------------------------------------------------------------------------------------------
			
	Function Ajouter_une_trajectoire:Ennemi (v:Ennemi, t:trajectoire)
		ListAddLast (v.Liste_trajectoires, String.FromInt (t.Id))
	End Function

	' ----------------------------------------------------------------------------------------------
		
	Method Afficher(v:Ennemi)
			If v.Index < 100 Then
				v.Index = v.Index + 1
			Else
				v.Index = 0
				Passer_a_autre_trajectoire(v)
			End If
			
			v.PosX = v.DebutX + v.Donner_x (v.Trajectoire_en_cours, v.index)
			v.PosY = v.DebutY + v.Donner_y (v.Trajectoire_en_cours, v.index)

			SetRotation (Donner_rot(v.Trajectoire_en_cours, v.index))
			DrawImage v.hImage, v.PosX, v.PosY
			
	End Method

	' ----------------------------------------------------------------------------------------------

	Function Donner_x(Id,Index)
		Local x
		For Local t:Trajectoire = EachIn ListeTrajectoires
			If t.Id = Id Then
				x = t.x[Index]
			Exit
			End If
		Next
		Return x
	End Function

	' ----------------------------------------------------------------------------------------------

	Function Donner_y(Id,Index)
		Local y
		For Local t:Trajectoire = EachIn ListeTrajectoires
			If t.Id = Id Then
				y = t.y[Index]
			Exit
			End If
		Next
		Return y
	End Function

		
	' ----------------------------------------------------------------------------------------------

		Function Donner_rot(Id,Index)
		Local rot
		For Local t:Trajectoire = EachIn ListeTrajectoires
			If t.Id = Id Then
				rot = t.rot[Index]
			Exit
			End If
		Next
		Return rot
	End Function

		' ----------------------------------------------------------------------------------------------

End Type

Type Trajectoire

	Field x[102]
	Field y[102]
	Field rot[102]
	Field Id
	
	
	Function Creer:Trajectoire (Id, x1,y1,vx1,vy1,x2,y2,vx2,vy2)

		Local Index
		Local t:Trajectoire = New Trajectoire
		
		'x1  - Starting Point X, y1  - Starting Point Y
		'vx1 - Curve modifier X, vy1 - Curve modifier Y
		'x2  - Ending Point X, y2  - Ending Point Y
		'vx2 - Curve modifier X, vy2 - Curve modifier Y
		
		t.Id = Id
		
		index = 0
		
		
		For Local a:Float=0 To 1 Step .01    
			t.x[Index] = x1*(1-a)^3 + 3*vx1*(1-a)^2*a + 3*vx2*(1-a)*a^2 + x2*a^3
			t.y[Index] = y1*(1-a)^3 + 3*vy1*(1-a)^2*a + 3*vy2*(1-a)*a^2 + y2*a^3
			Index = Index + 1
		Next
		
		For Local a=0 To 99
			'ATan2 (nxtx - curx, nxty - cury)
			t.rot[a] = 360 - ATan2 (t.x[a+1] - t.x[a], t.y[a+1] - t.y[a])
		Next
		
		ListAddLast (ListeTrajectoires, t)
		Return t
	
	End Function

End Type

' ----------------------------------------------------------------------------------------------
'
' PROGRAMME PRINCIPAL
'
' ----------------------------------------------------------------------------------------------


For Local v:Ennemi = EachIn ListeEnnemis
	v.Afficher(v)
Next

Local t1:Trajectoire
Local t2:Trajectoire
Local t3:Trajectoire


t1 = Trajectoire.Creer (1, 0,0,77,580,587,252,448,574)
t2 = Trajectoire.Creer (2,587,253,518,3,177,240,126,-49)
t3 = Trajectoire.Creer (3,177,240,222,382,383,189,422,402)


Local v1:Ennemi


v1 = Ennemi.Creer(0,0,"ship2.png",t1) 


Ennemi.Ajouter_une_trajectoire (v1,t2)
Ennemi.Ajouter_une_trajectoire (v1,t3)


Repeat

	'Cls
	
	For Local v:Ennemi = EachIn ListeEnnemis
		v.Afficher(v)
	Next	
			
	Flip


Until KeyHit (KEY_ESCAPE)



ship2.png