the robots here folow the tangent to a curve on a specific position like in this post http://purebasic.fr/english/viewtopic.php?f=36&t=61469
we can also use the spline http://purebasic.fr/english/viewtopic.php?f=12&t=62251 to determine custom routs for the robots to follow , look example spline.pb in PB examples.
the flat models used here is: robots, house, the ground
Code: Select all
Declare DrawCurve()
#CameraSpeed = 0.4
Define.f KeyX, KeyY, MouseX, MouseY
;the range of the function under study is from xMin to xMax
Global xMin.f = -2*#PI
Global xMax.f = 2*#PI
Global stepx.f = (xMax - xMin) / 1000 ; the increment of x every loop
Enumeration
#tex
#light
#plane = 10
EndEnumeration
ExamineDesktops()
InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"2D models, .. press space key to start/stop Marching ----- mouse/arrow keys for the camera",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
KeyboardMode(#PB_Keyboard_AllowSystemKeys)
CreateMaterial(0, LoadTexture(0, "White.jpg"))
DisableMaterialLighting(0, #True)
CreateMaterial(1, LoadTexture(1, "snow_1024.jpg"))
;MaterialCullingMode(1, #PB_Material_NoCulling)
;CreatePlane(#plane, 210, 210, 50, 50, 10, 10)
CreateSphere(#plane, 100)
TransformMesh(#plane, 0,0,0, 1,0,1, 0,0,0)
CreateEntity(#plane,MeshID(#plane),MaterialID(1), 0,-0.5,0)
UpdateMeshBoundingBox(#plane)
;WorldShadows(#PB_Shadow_Modulative)
CreateLight(0,RGB(245,245,205),0, 100, -100)
;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0, -34.41, 13.56, 23.49,#PB_Absolute)
CameraLookAt(0, -10, 5, 0)
;CameraBackColor(0, RGB(227,239,242))
CameraBackColor(0, RGB(148,197,158))
AmbientColor(RGB(255,255,255))
CreateLine3D(#PB_Any, -20, 0, 0, #Red, 20, 0, 0, RGB(255, 0, 0)) ; Red
CreateLine3D(#PB_Any, 0,-20,0, #Green, 0, 20, 0, RGB(0, 255, 0)); Green
CreateLine3D(#PB_Any, 0,0,-20, #Blue, 0, 0, 20, RGB(0, 0, 255)) ;Blue
;-Mesh
DrawCurve() ; call the function drawing procedure
; Create flare
GetScriptMaterial(0, "Scene/burst")
CreateBillboardGroup(0, MaterialID(0), 16, 16)
AddBillboard(0, 0, 10, -100)
LoadMesh(4, "tudorhouse.mesh")
TransformMesh(4, 0,0,0, 1,1,0, 0,0,0)
UpdateMeshBoundingBox(4)
CreateEntity(4, MeshID(4), #PB_Material_None)
ScaleEntity(4, 0.015,0.015,0.015)
MoveEntity(4,0,9,-12, #PB_Absolute)
LoadMesh(5, "Barrel.mesh")
CreateEntity(5, MeshID(5), #PB_Material_None)
ScaleEntity(5, 0.4,0.4,0.4)
MoveEntity(5,-20,1,-6, #PB_Absolute)
;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
LoadMesh(1, "robot.mesh")
TransformMesh(1, 0,0,0, 0,1,1, 0,0,0)
UpdateMeshBoundingBox(1)
CreateEntity(1, MeshID(1), #PB_Material_None)
ScaleEntity(1, 0.06,0.06,0.06)
MoveEntity(1,-20,0,0)
;RotateEntity(1, 90,0,0)
StartEntityAnimation(1, "Walk")
CopyEntity(1, 2)
MoveEntity(2,-24,0,-3)
StartEntityAnimation(2, "Walk")
x.f = xMin
start = 0
Repeat
WindowEvent()
If ExamineMouse()
MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
EndIf
ShowCursor_(0)
; Use arrow keys and mouse to rotate and fly in/out
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
If KeyboardReleased(#PB_Key_Space)
start ! 1
EndIf
If KeyboardReleased(#PB_Key_C)
Debug CameraX(0): Debug CameraY(0):Debug CameraZ(0)
EndIf
If start
; for the first robot
x.f + stepx
y.f = 0
z.f= Sin(x)
;visit this site to find the derivative of any function: http://www.wolframalpha.com/examples/Derivatives.html
slope.f= Cos(x) ; the derivative of sin(x) is cos(x)
slope = Degree(ATan(slope))
MoveEntity(1,x*3,y, z*3, #PB_Absolute) ; we just amplify x,y by 3
RotateEntity(1, 0,-slope+ro,0)
;for the second robot
z.f= Sin(x-1)
slope.f= Cos(x-1) ; the derivative of sin(x) is cos(x)
slope = Degree(ATan(slope))
MoveEntity(2,(x-1)*3,y, z*3, #PB_Absolute) ; we just amplify x,y by 3
RotateEntity(2, 0,-slope+ro,0)
If x > xMax
ro = 180
x = xMax
stepx * -1
EndIf
If x < xMin
x = xMin
ro = 0
stepx * -1
EndIf
EndIf
RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
MoveCamera (0, KeyX, 0, KeyY)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
;function drawing
Procedure DrawCurve()
Protected.f x, y
CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static)
x.f = xMin: z.f = 0
While x <= 2*#PI
z.f = Sin(x)
MeshVertexPosition(x*3, 0, z*3)
MeshVertexColor(RGB(0,0,255))
x.f + stepx
Wend
FinishMesh(#True)
CreateEntity(0, MeshID(0), MaterialID(0) ,0,0,0)
EndProcedure



