Ce chemin que nous allons mettre en oeuvre est matérialisé par un certain nombre de points de passage que nous allons créer avec AddSplinePoint().
On utilisera ComputeSpline() afin de connaitre la position (x,y,z) du point de déplacement sur ce chemin.
Il suffira de placer une entité sur les coordonnées de ce point.
Dans cet exemple de code, le cube devra suivre un chemin triangulaire.
Code : Tout sélectionner
; Naissance d'un cube : Le cube suit un chemin
EnableExplicit
Enumeration
#Mainform
EndEnumeration
Define.l Event
Global WindowStyle.i=#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered
Global Camera, Texture.i, Material.i, Mesh.i, Entity.i, N.f, StepX.f
;Path Finding
Global SPLine, Time.f, x.f, y.f, z.f, StepValue.f, TimeSinceLastFrame.f
InitEngine3D()
InitKeyboard()
InitSprite()
InitMouse()
OpenWindow(#Mainform,0,0,1024,768, "Suivre un chemin", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Mainform),0,0,1024,768,0, 0, 0)
KeyboardMode(#PB_Keyboard_International)
;
;Lumiere et ombre
AmbientColor(RGB(127, 127, 127))
CreateLight(#PB_Any,RGB(151, 251, 151), -1.8, 10, 5)
WorldShadows(#PB_Shadow_Additive)
;
;Une camera
Camera = CreateCamera(#PB_Any,0,0,100,100)
CameraBackColor(Camera, RGB(145, 182, 201))
MoveCamera(Camera, 0, 5 , 10, #PB_Absolute)
CameraLookAt(Camera, 0, 0, 0)
;
;Texture & Material
Texture = CreateTexture(#PB_Any,256,256)
StartDrawing(TextureOutput(Texture))
Box(0,0,256,256,RGB(0, 0, 0))
Box(1,1,252,252,RGB(255, 216, 0))
StopDrawing()
Material = CreateMaterial(#PB_Any, TextureID(Texture))
;
;Creation du sol
Mesh = CreatePlane(#PB_Any, 50, 50, 1, 1, 6, 6)
Entity = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material))
CreateEntityBody(Entity, #PB_Entity_StaticBody, 0,0,1)
;
;Création du cube
Mesh = CreateCube(#PB_Any, 1)
Entity = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material), 0, 1, 0)
;
;Création de l'objet chemin
spline = CreateSpline(#PB_Any)
; On indique des points de passages de du chemin (Ici on trace un triangle)
AddSplinePoint(spline, 0, 1, 0) ;Premier point
AddSplinePoint(spline, 5, 1, -15) ;Second point
AddSplinePoint(spline, -5, 1, -15) ;Troisiéme point
;L'entité passera par ces points et reviendra au premier point
AddSplinePoint(spline, 0, 1, 0) ;Premier point
;Sa vitesse est déterminée par la valeur du pas (Valeur décimale)
StepValue = 2.00
;Nous voila dans la boucle evenementielle
Repeat
Repeat
Event = WindowEvent()
Select Event
Case #PB_Event_CloseWindow
End
EndSelect
Until Event = 0
ExamineKeyboard()
If KeyboardPushed (#PB_Key_Escape)
Break
EndIf
;Calcule la position du point de déplacement (offset) spécifié.
ComputeSpline(spline, time)
;Une fois que le point a été calculé, sa position est disponible
x = SplineX(spline)
y = SplineY(spline)
z = SplineZ(spline)
;L'entité regarde en direction de ces trois points
EntityLookAt(Entity, x, y, z, 1, 0, 0)
;Et on le positionne sur ces coordonnées
MoveEntity(Entity, x, y, z, #PB_Absolute)
;Ou en est ton dans notre déplacement ?
time + StepValue * TimeSinceLastFrame / 30
;La valeur de décalage varie de 0.0 à 1.0
If time > 1 ;fin de la spline
Time = 0 ;début de la spline
EndIf
;Quel est le temps écoulé depuis la dernière image en millisecondes.
TimeSinceLastFrame = RenderWorld() / 1000
; Affiche le rendu de la scène
ClearScreen(RGB(0, 0, 0))
FlipBuffers()
ForEver