Road train

Everything related to 3D programming
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Road train

Post by Comtois »

Use cursor to move vehicle, and mouse to play with vehicle

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - CreateVehicle
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;
IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

#CameraSpeed = 2

Global.f KeyX, KeyY, MouseX, MouseY, ElapsedTime

Structure Vector3
  x.f
  y.f
  z.f
EndStructure

Macro VECTOR3(V, a, b, c)
  V\x = a
  V\y = b
  V\z = c
EndMacro  

Structure s_Vehicle
  Chassis.i 
  Wheels.i[6]
  
  WheelsEngine.i[4]
  WheelsEngineCount.i
  WheelsSteerable.i[4]
  WheelsSteerableCount.i
  
  EngineBrake.f
  EngineForce.f
  Steering.f
  
  SteeringLeft.i
  SteeringRight.i
EndStructure

Global MaxEngineForce.f = 13000.0
Global MaxEngineBrake.f = 200.0

Global SteeringIncrement.f = 0.5
Global SteeringClamp.f = 23        

Global WheelRadius.f = 0.5;
Global WheelWidth.f = 0.4 ;

Global SuspensionStiffness.f = 25.0
Global SuspensionDamping.f =  0.4 * 2.0 * Sqr(suspensionStiffness)
Global SuspensionCompression.f =  0.2 * 2.0 * Sqr(suspensionStiffness)
Global MaxSuspensionTravelCm.f = 400.0;
Global FrictionSlip.f = 1000     

Global RollInfluence.f = 0.0
Global SuspensionRestLength.f = 0.6;


Global VECTOR3(CameraStart.Vector3, 0, 25, 0)

Global VECTOR3(CarPosition.Vector3, 15, 3, -15)

Global Vehicle.s_Vehicle
Global Dim Remorque.s_Vehicle(10)

#CUBE_HALF_EXTENTS = 1

Declare BuildVehicle(*Vehicle.s_Vehicle)
Declare BuildRemorque(*Vehicle.s_Vehicle,x.f,y.f,z.f)
Declare HandleVehicle()
Declare frameStarted(elapsedTime.f)
Declare.f  Interpolation(x1.f, x2.f, percent.f)


If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures/", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts" , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    WorldShadows(#PB_Shadow_Modulative, -1, RGB(105, 105, 105))
    
    ;- Material
    ;
    CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
    GetScriptMaterial(1, "SphereMap/SphereMappedRustySteel")
    CreateMaterial(2, LoadTexture(2, "Dirt.jpg"))
    ScaleMaterial(2,0.05,0.05)
    GetScriptMaterial(3, "Scene/GroundBlend")
    
    ;-Ground
    ;
    CreatePlane(0, 500, 500, 5, 5, 5, 5)
    CreateEntity(0,MeshID(0),MaterialID(2))
    EntityRenderMode(0, 0) 
    CreateEntityBody(0, #PB_Entity_PlaneBody, 0, 0, 1)
    
    ;-Walls
    ;
    CreateCube(1, 1)
    CreateEntity(1,MeshID(1),MaterialID(2),0,1, 250)
    ScaleEntity(1,500,2,0.5) 
    CreateEntityBody(1, #PB_Entity_PlaneBody, 0, 0, 1,-1,-1,-1, 0,0,-1)
    
    CreateEntity(2,MeshID(1),MaterialID(2),0,1, -250)
    ScaleEntity(2,500,2,0.5) 
    CreateEntityBody(2, #PB_Entity_PlaneBody, 0, 0, 1,-1,-1,-1, 0,0,1)
    
    CreateEntity(3,MeshID(1),MaterialID(2),250,1, 0)
    ScaleEntity(3,0.5,2,500) 
    CreateEntityBody(3, #PB_Entity_PlaneBody, 0, 0, 1,-1,-1,-1, -1,0,0)
    
    CreateEntity(4,MeshID(1),MaterialID(2),-250,1, 0)
    ScaleEntity(4,0.5,2,500) 
    CreateEntityBody(4, #PB_Entity_PlaneBody, 0, 0, 1,-1,-1,-1, 1,0,0)
    
    ;- Light 
    ;
    CreateLight(0 ,RGB(190, 190, 190), 400, 120, 100,#PB_Light_Directional)
    SetLightColor(0, #PB_Light_SpecularColor, RGB(255*0.4, 255*0.4,255*0.4)) 
    LightDirection(0 ,0.55, -0.3, -0.75) 
    AmbientColor(RGB(255*0.2, 255*0.2,255*0.2))
    
    ;- Camera 
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0,  800, 400, 80, #PB_Absolute)
    
    ; SkyBox
    ;
    SkyBox("desert07.jpg")
    
    ;-
    
    BuildVehicle(@Vehicle)
    ;-Remorque
    BuildRemorque(@Remorque(0), 0, 3, -5)
    GenericJoint(0, EntityID(Vehicle\Chassis),0.0,0,-2.5,EntityID(Remorque(0)\Chassis),0.0,0,2.5)
    For i=1 To 10
      BuildRemorque(@Remorque(i), 0, 3, -5-10*i)
      GenericJoint(i, EntityID(Remorque(i-1)\Chassis),0,0,-2.5,EntityID(Remorque(i)\Chassis),0,0,2.5)
    Next
    
    ;- GUI
    ;
    OpenWindow3D(0, 0, 0, 50 , 10 , "")
    HideWindow3D(0,1)
    ShowGUI(128, 1) ; Display the GUI, semi-transparent and display the mouse cursor
    
    ;-Main 
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.03
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
        InputEvent3D(MouseX(), MouseY(),0)
        BodyPick(CameraID(Camera), MouseButton(#PB_MouseButton_Left), MouseX(), MouseY(), 1)
        
      EndIf
      ExamineKeyboard()
      
      
      handleVehicle()
      frameStarted(ElapsedTime)
      CameraFollow(0, EntityID(Remorque(6)\Chassis),180, 7, 27, 0.08, 0.08)
      
      ElapsedTime = RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape)   
    
    End 
    
  EndIf 
  
Else
  MessageRequester("Error","Can't initialize engine3D")
EndIf 

Procedure Clamp(*var.float, min.f, max.f)
  If *var\f < min
    *var\f = min
  ElseIf *var\f > max
    *var\f = max
  EndIf
EndProcedure  

Procedure BuildVehicle(*Vehicle.s_Vehicle)
  Protected.VECTOR3 wheelDirectionCS0,wheelAxleCS,connectionPointCS0
  
  With *Vehicle  
    ;reset
    For i = 0 To 3
      
      \WheelsEngine[i] = 0
      \WheelsSteerable[i] = 0
    Next
    \WheelsEngineCount = 2
    \WheelsEngine[0] = 0
    \WheelsEngine[1] = 1
    
    \WheelsSteerableCount = 4
    \WheelsSteerable[0] = 0
    \WheelsSteerable[1] = 1
    \WheelsSteerable[2] = 2
    \WheelsSteerable[3] = 3
    
    
    \SteeringLeft = #False
    \SteeringRight = #False
    
    \EngineForce = 0
    \Steering = 0
    
    
    ;- >>> create vehicle  <<<<<
    
    connectionHeight.f = 0.7
    
    ChassisMesh = CreateCube(#PB_Any, 2)
    
    ChassisEntity = CreateEntity(#PB_Any, MeshID(chassisMesh), #PB_Material_None, 0, 1, 0)
    ScaleEntity(ChassisEntity, 0.8, 0.7, 2)
    \Chassis = CreateVehicle(#PB_Any)
    AddSubEntity(\Chassis, ChassisEntity, #PB_Entity_BoxBody, 0, 0, 0, 0.8, 0.7, 2) 
    
    EntityRenderMode(\Chassis, #PB_Entity_CastShadow)
    CreateVehicleBody(\Chassis, 700, 0.3, 0.8,suspensionStiffness, suspensionCompression, suspensionDamping, maxSuspensionTravelCm, frictionSlip)
    
    MoveEntity(\Chassis, 0, 3, 0,#PB_Absolute)
    DisableDebugger
    SetEntityAttribute(\Chassis, 27, 0.0)
    SetEntityAttribute(\Chassis, 28, 0.0)
    EnableDebugger
    wheelAxleCS\x       = -1
    wheelAxleCS\y       = 0
    wheelAxleCS\z       = 0
    
    
    Wheel = CreateSphere(#PB_Any, WheelRadius)
    For i = 0 To 5
      \Wheels[i] = CreateEntity(#PB_Any, MeshID(Wheel), #PB_Material_None)
      ScaleEntity(\Wheels[i], WheelWidth,1,1)
    Next
    
    isFrontWheel = #True
    
    ;-WheelSteerable and WheelsEngine
    VECTOR3(connectionPointCS0, #CUBE_HALF_EXTENTS-(0.3*WheelWidth), connectionHeight,2*#CUBE_HALF_EXTENTS-WheelRadius)
    AddVehicleWheel(\Chassis, \Wheels[0], 
                    connectionPointCS0\x, connectionPointCS0\y,connectionPointCS0\z,
                    wheelAxleCS\x, wheelAxleCS\y,wheelAxleCS\z,
                    SuspensionRestLength, 
                    WheelRadius, 
                    isFrontWheel, 
                    0.1)
    
    ;-WheelSteerable and WheelsEngine
    VECTOR3(connectionPointCS0, -#CUBE_HALF_EXTENTS+(0.3*WheelWidth), connectionHeight, 2*#CUBE_HALF_EXTENTS-WheelRadius)
    AddVehicleWheel(\Chassis, \Wheels[1], 
                    connectionPointCS0\x, connectionPointCS0\y,connectionPointCS0\z,
                    wheelAxleCS\x, wheelAxleCS\y,wheelAxleCS\z,
                    SuspensionRestLength, 
                    WheelRadius, 
                    isFrontWheel, 
                    0.1)
    
    isFrontWheel = #False
    
    VECTOR3(connectionPointCS0, -#CUBE_HALF_EXTENTS+(0.3*WheelWidth), connectionHeight, -2*#CUBE_HALF_EXTENTS+WheelRadius);
    AddVehicleWheel(\Chassis, \Wheels[2], 
                    connectionPointCS0\x, connectionPointCS0\y,connectionPointCS0\z,
                    wheelAxleCS\x, wheelAxleCS\y,wheelAxleCS\z,
                    SuspensionRestLength, 
                    WheelRadius, 
                    isFrontWheel, 
                    0.1)
    
    
    VECTOR3(connectionPointCS0, #CUBE_HALF_EXTENTS-(0.3*WheelWidth), connectionHeight, -2*#CUBE_HALF_EXTENTS+WheelRadius);
    AddVehicleWheel(\Chassis, \Wheels[3], 
                    connectionPointCS0\x, connectionPointCS0\y,connectionPointCS0\z,
                    wheelAxleCS\x, wheelAxleCS\y,wheelAxleCS\z,
                    SuspensionRestLength, 
                    WheelRadius, 
                    isFrontWheel, 
                    0.1)
    
    For i= 0 To 3
      SetVehicleAttribute(\Chassis, #PB_Vehicle_MaxSuspensionForce, 4000, i)
    Next	
    
  EndWith
EndProcedure

Procedure BuildRemorque(*Vehicle.s_Vehicle,x.f,y.f,z.f)
  Protected.VECTOR3 wheelDirectionCS0,wheelAxleCS,connectionPointCS0
  
  With *Vehicle  
    ;reset
    For i = 0 To 3
      
      \WheelsEngine[i] = 0
      \WheelsSteerable[i] = 0
    Next
    \WheelsEngineCount = 2
    \WheelsEngine[0] = 0
    \WheelsEngine[1] = 1
    
    \WheelsSteerableCount = 4
    \WheelsSteerable[0] = 0
    \WheelsSteerable[1] = 1
    \WheelsSteerable[2] = 2
    \WheelsSteerable[3] = 3
    
    
    \SteeringLeft = #False
    \SteeringRight = #False
    
    \EngineForce = 0
    \Steering = 0
    
    
    ;- >>> create vehicle  <<<<<
    
    connectionHeight.f = 0.7
    
    ChassisMesh = CreateCube(#PB_Any, 2)
    
    ChassisEntity = CreateEntity(#PB_Any, MeshID(chassisMesh), #PB_Material_None, 0, 1, 0)
    ScaleEntity(ChassisEntity, 0.8, 0.7, 2)
    \Chassis = CreateVehicle(#PB_Any)
    AddSubEntity(\Chassis, ChassisEntity, #PB_Entity_BoxBody, 0, 0, 0, 0.8, 0.7, 2) 
    
    EntityRenderMode(\Chassis, #PB_Entity_CastShadow)
    CreateVehicleBody(\Chassis, 170, 0.3, 0.8,suspensionStiffness, suspensionCompression, suspensionDamping, maxSuspensionTravelCm, frictionSlip)
    
    MoveEntity(\Chassis, x, 3, z,#PB_Absolute)
    DisableDebugger
    SetEntityAttribute(\Chassis, 27, 0.0)
    SetEntityAttribute(\Chassis, 28, 0.0)
    EnableDebugger
    wheelAxleCS\x       = -1
    wheelAxleCS\y       = 0
    wheelAxleCS\z       = 0
    
    
    Wheel = CreateSphere(#PB_Any, WheelRadius)
    For i = 0 To 5
      \Wheels[i] = CreateEntity(#PB_Any, MeshID(Wheel), #PB_Material_None)
      ScaleEntity(\Wheels[i], WheelWidth,1,1)
    Next
    
    isFrontWheel = #False
    
    ;-WheelSteerable and WheelsEngine
    VECTOR3(connectionPointCS0, #CUBE_HALF_EXTENTS-(0.3*WheelWidth), connectionHeight,2*#CUBE_HALF_EXTENTS-WheelRadius)
    AddVehicleWheel(\Chassis, \Wheels[0], 
                    connectionPointCS0\x, connectionPointCS0\y,connectionPointCS0\z,
                    wheelAxleCS\x, wheelAxleCS\y,wheelAxleCS\z,
                    SuspensionRestLength, 
                    WheelRadius, 
                    isFrontWheel, 
                    RollInfluence)
    
    ;-WheelSteerable and WheelsEngine
    VECTOR3(connectionPointCS0, -#CUBE_HALF_EXTENTS+(0.3*WheelWidth), connectionHeight, 2*#CUBE_HALF_EXTENTS-WheelRadius)
    AddVehicleWheel(\Chassis, \Wheels[1], 
                    connectionPointCS0\x, connectionPointCS0\y,connectionPointCS0\z,
                    wheelAxleCS\x, wheelAxleCS\y,wheelAxleCS\z,
                    SuspensionRestLength, 
                    WheelRadius, 
                    isFrontWheel, 
                    RollInfluence)
    
    isFrontWheel = #False
    
    VECTOR3(connectionPointCS0, -#CUBE_HALF_EXTENTS+(0.3*WheelWidth), connectionHeight, -2*#CUBE_HALF_EXTENTS+WheelRadius);
    AddVehicleWheel(\Chassis, \Wheels[2], 
                    connectionPointCS0\x, connectionPointCS0\y,connectionPointCS0\z,
                    wheelAxleCS\x, wheelAxleCS\y,wheelAxleCS\z,
                    SuspensionRestLength, 
                    WheelRadius, 
                    isFrontWheel, 
                    RollInfluence)
    
    
    VECTOR3(connectionPointCS0, #CUBE_HALF_EXTENTS-(0.3*WheelWidth), connectionHeight, -2*#CUBE_HALF_EXTENTS+WheelRadius);
    AddVehicleWheel(\Chassis, \Wheels[3], 
                    connectionPointCS0\x, connectionPointCS0\y,connectionPointCS0\z,
                    wheelAxleCS\x, wheelAxleCS\y,wheelAxleCS\z,
                    SuspensionRestLength, 
                    WheelRadius, 
                    isFrontWheel, 
                    RollInfluence)
    
    For i= 0 To 3
      SetVehicleAttribute(\Chassis, #PB_Vehicle_MaxSuspensionForce, 4000, i)
    Next	
    
  EndWith
EndProcedure

Procedure HandleVehicle()
  
  If KeyboardPushed(#PB_Key_Left)  
    Vehicle\SteeringLeft = #True
    Vehicle\SteeringRight = #False
    
  ElseIf KeyboardPushed(#PB_Key_Right)  
    Vehicle\SteeringRight = #True
    Vehicle\SteeringLeft = #False
  Else 
    Vehicle\SteeringRight = #False
    Vehicle\SteeringLeft = #False          
  EndIf
  
  If KeyboardPushed(#PB_Key_Down) 
    If GetEntityAttribute(Vehicle\Chassis, #PB_Entity_LinearVelocity)< 0.4
      Recul = #True
    EndIf  
    If Recul 
      Vehicle\EngineForce = -MaxEngineForce
      Vehicle\EngineBrake = 0  
    Else
      Vehicle\EngineForce = 0
      Vehicle\EngineBrake = MaxEngineBrake
    EndIf 
  ElseIf KeyboardPushed(#PB_Key_Up) 
    Vehicle\EngineForce = MaxEngineForce
    Vehicle\EngineBrake = 0
  Else
    Vehicle\EngineBrake = MaxEngineForce/ 100
    Vehicle\EngineForce = 0
    Recul = #False
  EndIf
  
  
EndProcedure
Procedure frameStarted(elapsedTime.f)
  With Vehicle
    
    ; apply engine Force on relevant wheels
    For i = 0 To 1
      ApplyVehicleBrake(\Chassis,\WheelsEngine[i],  \EngineBrake)  
      ApplyVehicleForce(\Chassis, \WheelsEngine[i], \EngineForce)
    Next
    
    
    If (\SteeringLeft)
      
      \Steering + SteeringIncrement*elapsedTime
      If (\Steering > SteeringClamp)
        \Steering = SteeringClamp
      EndIf  
      
    ElseIf (\SteeringRight)
      
      \Steering - SteeringIncrement*elapsedTime
      If (\Steering < -SteeringClamp)
        \Steering = -SteeringClamp
      EndIf  
    Else
      \Steering = Interpolation(\Steering, 0, 0.05)  
    EndIf
    
    ; apply Steering on relevant wheels
    
    For i = 0 To 1			
      ApplyVehicleSteering(\Chassis,  \WheelsSteerable[i], \Steering)
    Next
    ; 		For i = 2 To 3			
    ; 			PBO_SetVehicleSteering(\Chassis, -\Steering, \WheelsSteerable[i])
    ; 		Next		
  EndWith   
EndProcedure

Procedure.f Interpolation(x1.f, x2.f, percent.f)
  If percent<0
    percent=0
  EndIf
  If percent>1
    percent=1
  EndIf
  ProcedureReturn x1 + percent * (x2 - x1)
  
EndProcedure
Last edited by Comtois on Sat Jun 18, 2016 4:56 pm, edited 2 times in total.
Please correct my english
http://purebasic.developpez.com/
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Road train

Post by Comtois »

Added BodyPick()
Please correct my english
http://purebasic.developpez.com/
User avatar
Demivec
Addict
Addict
Posts: 4267
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Road train

Post by Demivec »

Thanks for posting this awesome demo! :D
Fred
Administrator
Administrator
Posts: 18210
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Road train

Post by Fred »

Very nice :)
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Road train

Post by Little John »

Hi,

the program crashes here (PB 5.50 beta 1 x64 on Windows 10).

The very first part runs fine, I can see the "3D requester" (Is that the proper name?).
But when I click at the "Launch" button, then the program crashes.
This happens when "Fullscreen" is selected, and also when "Windowed" is selected.
I have also tested with my anti virus program switched off, but that didn't change anything.
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Road train

Post by Joris »

It doesn't run here PB 5.42 under XP

CreateEntityBody ......
has to many parameters (also stated in the help of PB 5.42)
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Road train

Post by applePi »

thanks Comtois for this magnificent demo.

@Little John: On Windows, a recent version of DirectX 9 needs to be installed: look near the end of this page:
https://www.purebasic.com/documentation ... ine3d.html
the directx9 will not damage the currect windows 10 directx
i never tried the web intaller: https://www.microsoft.com/en-us/downloa ... 35&nowin10
uncheck everything in the left pan (so not to set MSN default homepage & Bing default search engine)

the full dx9 package here:(96MB): inflate it and run setup:
http://download.microsoft.com/download/ ... redist.exe

or first before downloading direct x9, write in the Compiler -> compiler options -> Library subsystem : opengl
and run the demo

@joris : the demo needs PB v5.50 beta 1
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Road train

Post by Kwai chang caine »

Splendid :shock:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply