primitive car

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

primitive car

Post by applePi »

i have attached 4 spheres to the both sides of a block with a hinge joint, then powering the hinge in the front wheels with a motor using EnableHingeJointAngularMotor(...)
to move the car use UP key to provide a pulse (ApplyEntityImpulse(#carBody, 15, 0, 0, 0,0,0))
rotate right and Left by making a difference in the angularMotor between the front wheels, i found this more stable than RotateEntity(wheel...)
it is unstable car in high speed , and still needs much improvements.
also needs a camera view like that in the Tank.pb example
PS: note the friction in the front wheels (in the EntityPhysicBody for the 4 wheels) it is 1 for the front wheels, and for the back wheels it is 0.8 just an experimental choice, the friction make your car more stable. from the tests if the four wheels have the same friction (ie 1) the car will not turn right and Left
http://en.wikipedia.org/wiki/Friction#A ... f_friction

Code: Select all

Enumeration
   #LIGHT
   #camera
   #mainwin
   #plane
   #wheelL
   #wheelR
   #wheel_BR
   #wheel_BL
   #carBody
   #playBall
 EndEnumeration


ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "Z for speed , X to slow down the wheel, arrows to Camera rotate/zoom. W for wire/solid frame", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;Initialize environment
InitEngine3D()
InitSprite()
OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-50, 0, 0, 0)

InitKeyboard()
SetFrameRate(60)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)

CreateLight(#LIGHT,RGB(22,222,0),0,-4,0,#PB_Light_Directional )
LightLookAt(#LIGHT, 0, 4, -5)

CreateCamera(#camera, 0, 0, 1000, 1000)
MoveCamera(#camera, -220, 42, 30)
CameraLookAt(#camera, 0, 0, 0)

EndIf

CreateMaterial(2, LoadTexture(2, "Geebee2.bmp"))
CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
CreateMaterial(0, LoadTexture(0, "terrain_texture.jpg"))
DisableMaterialLighting(0, #True)
MaterialCullingMode(0, #PB_Material_NoCulling)

CreatePlane(#plane , 700, 500, 1, 1, 1, 1)
CreateEntity(#plane,MeshID(#plane),MaterialID(1), 0, 2, -10) 
RotateEntity(#plane,0,0,180)
MaterialCullingMode(1, #PB_Material_NoCulling)
MaterialBlendingMode(1, #PB_Material_Add )
EntityPhysicBody(#plane, #PB_Entity_StaticBody  ,0,0,5)

CreateCube(#carBody, 1)
CreateEntity(#carBody, MeshID(#carBody), MaterialID(2), -150,  4, 0)
EntityPhysicBody(#carBody, #PB_Entity_BoxBody ,200)
ScaleEntity(#carBody,20,1,2)

CreateSphere(#wheelR, 1)
CreateEntity(#wheelR, MeshID(#wheelR), MaterialID(2), -150,  4, 6)
EntityPhysicBody(#wheelR, #PB_Entity_SphereBody ,100,0,1) ; note the friction are 3000 to stabilize it

CopyMesh(#wheelR, #wheelL)
CreateEntity(#wheelL, MeshID(#wheelL), MaterialID(0), -150,  4, -6) 
EntityPhysicBody(#wheelL, #PB_Entity_SphereBody ,100,0,1) ;; note the friction are 3000

CopyMesh(#wheelR, #wheel_BR)
CopyMesh(#wheelR, #wheel_BL)
CreateEntity(#wheel_BR, MeshID(#wheel_BR), MaterialID(2), -170,  4, 6) 
CreateEntity(#wheel_BL, MeshID(#wheel_BL), MaterialID(0), -170,  4, -6) 
EntityPhysicBody(#wheel_BR, #PB_Entity_SphereBody ,10,0,0.8) ; note the friction are just 1
EntityPhysicBody(#wheel_BL, #PB_Entity_SphereBody ,10,0,0.8) ; note the friction are just 1

HingeJoint(1,EntityID(#wheelR),0,0,-1.5, 0,0,1, EntityID(#carBody),8,0,3, 0,0,1)
HingeJoint(2,EntityID(#wheelL),0,0,3, 0,0,1, EntityID(#carBody),8,0,-1.5, 0,0,1)
HingeJoint(3,EntityID(#wheel_BR),0,0,-1.5, 0,0,1, EntityID(#carBody),-8,0,3, 0,0,1)
HingeJoint(4,EntityID(#wheel_BL),0,0,3, 0,0,1, EntityID(#carBody),-8,0,-1.5, 0,0,1)


;WorldGravity(-9.806) ; this is the default
CreateMesh(#plane,#PB_Mesh_TriangleList ,#PB_Mesh_Static)
SetMeshMaterial(#plane, MaterialID(1))

CreateSphere(#playBall, 7)
CreateEntity(#playBall,MeshID(#playBall), MaterialID(2), -50,  8, 0)
EntityPhysicBody(#playBall, #PB_Entity_SphereBody ,1,20,1)

;WorldDebug(#PB_World_DebugBody)
;WorldDebug(#PB_World_DebugEntity)

Repeat
  Event = WindowEvent()
       
   RenderWorld()
   FlipBuffers()

   ExamineKeyboard()
   
    If KeyboardPushed(#PB_Key_Up)
      ApplyEntityImpulse(#carBody,  10, 0, 0,  0,0,0)
     ;Angular motor for both front wheels
     EnableHingeJointAngularMotor(1, #True, -40, 80)
     EnableHingeJointAngularMotor(2, #True, -40, 80)
     
     
     ElseIf KeyboardPushed(#PB_Key_Down) ; reverse
     EnableHingeJointAngularMotor(1, #True, 5, 80)
     EnableHingeJointAngularMotor(2, #True, 5, 80)
     
   EndIf
   If KeyboardPushed(#PB_Key_Right)
     ;make a difference between wheelR and wheelL angular velocity, so it turns right
     EnableHingeJointAngularMotor(1, #True, 40, 80) 
     
          
     ElseIf KeyboardPushed(#PB_Key_Left)
       ;make a difference between wheelR and wheelL angular velocity, so it turns Left     
       EnableHingeJointAngularMotor(2, #True, 40, 80) 
       
   EndIf 
   MoveCamera(#camera, EntityX(#carBody), EntityY(#carBody)+70, EntityZ(#carBody)-90, #PB_Absolute )
   CameraLookAt(#camera, EntityX(#carBody), EntityY(#carBody), EntityZ(#carBody) )
            
Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: primitive car

Post by applePi »

version 2: it is a kind of a dump truck, it has a bed hinged at the rear, and has a motor to lift the bed to allow the contents to be deposited on the ground behind the truck at the site of delivery
features:
1- it has cylinder wheels instead of spheres, note that cyclinder Y is connected to carBody Z
2- the car body are elevated slightly by attaching it to a node (0, 4, 0)
3-the slope have two hinge joints : one as an axis for rotation, the other for catching it from random unofficial rotation.
4-the turn left right are by differential speed to the both front wheels, still there is a need to rotate the front wheels like ordinary cars.
5- the picture on the slope are made using the ts-soft utility Bin2Data

controls keys:
Z to initiate power, use it before Up key (sometimes)
arrow keys to move and to turn, use Down key periodically to slow down the car else it will overturn.
S stop the car completely.
D move the slope to dump the cargo and when the slope reach certain angle the cargo will slide down by gravity to the ground.
R rotate the slope to its original position

the future models will focus on stability of the car
Image

Code: Select all

Enumeration
   #LIGHT
   #camera
   #mainwin
   #plane
   #wheelL
   #wheelR
   #wheel_BR
   #wheel_BL
   #carBody
   #playBall
   #node
   #axis
   #slope
   #cargo
      
 EndEnumeration
   
 
UsePNGImageDecoder() ; necessary to load the picture in the DataSection

ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "Z/up accel+move, S stop the Car, D dump cargo, R rotate slope left, arrow L/R/D turn and reverse,", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;Initialize environment
InitEngine3D()
InitSprite()
OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-50, 0, 0, 0)

InitKeyboard()
SetFrameRate(60)

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/Sources\Data", #PB_3DArchive_FileSystem)

CreateLight(#LIGHT,RGB(22,222,0),0,-4,0,#PB_Light_Directional )
LightLookAt(#LIGHT, 0, 4, -5)

CreateCamera(#camera, 0, 0, 1000, 1000)
MoveCamera(#camera, -220, 42, 30)
CameraLookAt(#camera, 0, 0, 0)

EndIf

CreateMaterial(3, LoadTexture(3, "clouds.jpg"))
CreateMaterial(2, LoadTexture(2, "Geebee2.bmp"))
CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
CreateMaterial(0, LoadTexture(0, "terrain_texture.jpg"))

DisableMaterialLighting(0, #True)
;MaterialCullingMode(0, #PB_Material_NoCulling)

CatchImage(4, ?pb_PNG_start)  ; to texture the slope
CreateTexture(4, ImageWidth(4), ImageHeight(4))
If StartDrawing(TextureOutput(4))
  DrawImage(ImageID(4), 0, 0)
  StopDrawing()
EndIf
CreateMaterial(4, TextureID(4))


CreatePlane(#plane , 2000, 2000, 1, 1, 5, 5)
CreateEntity(#plane,MeshID(#plane),MaterialID(1), 0, 0, -10) 
;RotateEntity(#plane,0,0,180)
MaterialCullingMode(1, #PB_Material_NoCulling)
MaterialBlendingMode(1, #PB_Material_Add )
EntityPhysicBody(#plane, #PB_Entity_StaticBody  ,0,0,5)

CreateCube(#carBody, 1)
CreateEntity(#carBody, MeshID(#carBody), MaterialID(2), -150,  4, 0)
ScaleEntity(#carBody,20,1,4)
EntityPhysicBody(#carBody, #PB_Entity_BoxBody ,200)
;;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
;
CreateNode(#node, 0,  4, 0)
AttachNodeObject(#node, EntityID(#carBody))

;CreateSphere(#wheelR, 1)
CreateCylinder(#wheelR,1,1)
CreateEntity(#wheelR, MeshID(#wheelR), MaterialID(0), -150,  4, 6)
ScaleEntity(#wheelR, 2,1,2)
EntityPhysicBody(#wheelR, #PB_Entity_CylinderBody ,200,0,1) ; note the friction are 3000 to stabilize it

CopyMesh(#wheelR, #wheelL)
CreateEntity(#wheelL, MeshID(#wheelL), MaterialID(0), -150,  4, -6) 
ScaleEntity(#wheelL, 2,1,2)
EntityPhysicBody(#wheelL, #PB_Entity_CylinderBody ,200,0,1) ;; note the friction are 3000

CopyMesh(#wheelR, #wheel_BR)
CopyMesh(#wheelR, #wheel_BL)
CreateEntity(#wheel_BR, MeshID(#wheel_BR), MaterialID(0), -170,  4, 6) 
CreateEntity(#wheel_BL, MeshID(#wheel_BL), MaterialID(0), -170,  4, -6) 
ScaleEntity(#wheel_BR, 2,1,2)
ScaleEntity(#wheel_BL, 2,1,2)
EntityPhysicBody(#wheel_BR, #PB_Entity_CylinderBody ,10,0,1) ; note the friction are just 1
EntityPhysicBody(#wheel_BL, #PB_Entity_CylinderBody ,10,0,1) ; note the friction are just 1

CreateCube(#slope, 1) ; the slope
CreateEntity(#slope, MeshID(#slope), MaterialID(4), -150,  4.4, -6)
ScaleEntity(#slope,12,0.5,10)
EntityPhysicBody(#slope, #PB_Entity_BoxBody  ,1,0,1) ; note the friction are just 1

CreateCube(#cargo, 1)
CreateEntity(#cargo, MeshID(#cargo), MaterialID(3), -150,  4.4, -6 )
ScaleEntity(#cargo, 4,4,4)
EntityPhysicBody(#cargo, #PB_Entity_BoxBody ,1,0,2)

PointJoint(6,EntityID(#cargo),0,-1.5,0, EntityID(#slope),0,0,0)
HingeJoint(1,EntityID(#wheelR),0,0,0, 0,1,0, EntityID(#carBody),8,0,6, 0,0,1)
HingeJoint(2,EntityID(#wheelL),0,0,0, 0,1,0, EntityID(#carBody),8,0,-6, 0,0,1)
HingeJoint(3,EntityID(#wheel_BR),0,0,0, 0,1,0, EntityID(#carBody),-8,0,6, 0,0,1)
HingeJoint(4,EntityID(#wheel_BL),0,0,0, 0,1,0, EntityID(#carBody),-8,0,-6, 0,0,1)
HingeJoint(7,EntityID(#slope),0,0,0, 0,0,1, EntityID(#carBody),0,5,0, 0,0,1) ; joint for the left side of the slope
HingeJoint(5,EntityID(#slope),-6,0,0, 0,0,1, EntityID(#carBody),-8,5,0, 0,0,1) ; joint for the slope axis
;HingeJoint(7,EntityID(#slope),-6,0,0, 0,0,1, EntityID(#carBody),-8,5,0, 0,0,1)
EnableHingeJointAngularMotor(5, #True, 0, 50)

;WorldGravity(-9.806) ; this is the default

CreateSphere(#playBall, 7)
CreateEntity(#playBall,MeshID(#playBall), MaterialID(2), -90,  8, 0)
EntityPhysicBody(#playBall, #PB_Entity_SphereBody ,1,20,1)

;WorldDebug(#PB_World_DebugBody)
;WorldDebug(#PB_World_DebugEntity)

Repeat
  Event = WindowEvent()
       
   RenderWorld()
   FlipBuffers()

   ExamineKeyboard()
    If KeyboardPushed(#PB_Key_Z) ; to give initial push to the car
            ApplyEntityImpulse(#carBody,  4, 0, 0,  0,0,0)
    EndIf
          
     If KeyboardReleased(#PB_Key_S) ; to stop the car completely
         EnableHingeJointAngularMotor(1, #True, 0, 100) ; stop the joint for right wheel
         EnableHingeJointAngularMotor(2, #True, 0, 100)
     EndIf
   
    If KeyboardPushed(#PB_Key_Up)
      ;ApplyEntityImpulse(#carBody,  4, 0, 0,  0,0,0)
      ApplyEntityForce(#carBody, 4, 0, 0,  0,0,0)

     ;Angular motor for both front wheels
     EnableHingeJointAngularMotor(1, #True, -70, 100)
     EnableHingeJointAngularMotor(2, #True, -70, 100)
          
     ElseIf KeyboardPushed(#PB_Key_Down) ; reverse
     EnableHingeJointAngularMotor(1, #True, 5, 80)
     EnableHingeJointAngularMotor(2, #True, 5, 80)
     ElseIf KeyboardPushed(#PB_Key_Z) ; reverse
     ApplyEntityImpulse(#carBody,  4, 0, 0,  0,0,0)
     
   EndIf
   
   If KeyboardPushed(#PB_Key_Right)
     ;make a difference between wheelR and wheelL angular velocity, so it turns right
     EnableHingeJointAngularMotor(1, #True, -5, 100) 
     a.f = EntityRoll(#carBody)
     RotateCamera(#camera,0,a,0)
          
     ElseIf KeyboardPushed(#PB_Key_Left)
       ;make a difference between wheelR and wheelL angular velocity, so it turns Left     
       EnableHingeJointAngularMotor(2, #True, -5, 100) 
       
    EndIf 
     
     If KeyboardPushed(#PB_Key_D) ; to lift the bed to deposit the cargo (the blue cube)
       FreeJoint(7) ; free the joint which prevent the slope from auto rotation
       ApplyEntityImpulse(#slope,  0, 4, 0,  6,0,0) ; apply force up
       EnableHingeJointAngularMotor(5, #True, 0.1, 50)
       
     EndIf 

       xx.f = EntityX(#carBody): yy.f = EntityY(#carBody): zz.f = EntityZ(#carBody)
       xx2.f = EntityX(#slope): yy2.f = EntityY(#slope): zz2.f = EntityZ(#slope)
     
       ;re = RayCollide(...) ; also can be used
       If (yy2-yy) > 5 ; to stop the slope at cetain angle
         EnableHingeJointAngularMotor(5, #True, 0, 50)
         FreeJoint(6) ; free the cargo to slide down by gravity
       EndIf
       
       If KeyboardReleased(#PB_Key_R) ; to reposition the slope to its initial position
         ApplyEntityImpulse(#slope,  0, -4, 0,  0,0,0) ; to push the slope down
         EnableHingeJointAngularMotor(5, #True, -0.2, 50)
         
       EndIf
       If (yy2-yy) < 1 ; when the slope return after dumbing stop at horizontal level
         EnableHingeJointAngularMotor(5, #True, 0, 50)  
       EndIf
       
       
           
   MoveCamera(#camera, EntityX(#carBody), EntityY(#carBody)+70, EntityZ(#carBody)-90, #PB_Absolute )
   CameraLookAt(#camera, EntityX(#carBody), EntityY(#carBody), EntityZ(#carBody) )
   
            
 Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow
 
 
 DataSection  ;
  pb_png_start:
    ; size : 537 bytes ; using the great ts-soft utility Bin2Data
    Data.q $0A1A0A0D474E5089,$524448490D000000,$2700000054000000,$F8BF1E0000000208,$414449E0010000F9
    Data.q $C38E3158ED9C7854,$FB2BF223DCDC1020,$36EF4A404FC23C1D,$4A4DBAD200F3A852,$02B9A936D523BB4A
    Data.q $22739C7018D906DD,$590466187AF154C1,$9F3F15C818C67790,$84A2B9F09449F036,$73E128AE7C2515CF
    Data.q $115CF84A2B9F0945,$8600132561F3BF09,$3D75D665DAEBA861,$B2BCDDBB7688C30C,$189800003C8BFA73
    Data.q $99EB5295912BD7AE,$6B5A34D34002DB6D,$19A99A69BAD68C63,$54888B6DB40018C6,$A94AE2BCEBAEFD24
    Data.q $729A3D7E16A9E728,$D2077A5294E6D33D,$8164E293ABD294A3,$9149905C996328DB,$2F77A3D20774C5DC
    Data.q $AF2776F0D2F93A24,$8CF8499DF86F79D1,$ADE9330E4A236D45,$988CE8A7DFBF7AD6,$D1D47A6EDDD24E29
    Data.q $DFBF7EB767CF9FCA,$38FEDCE70AA7C36D,$1637BA2868FF8F1E,$318CC56DD330A46D,$5AD28E9BAEBBEDDB
    Data.q $FA73DBEA75455557,$8F6EEB6D10BFDF7D,$EEF94CAA04AAB488,$88108472CC1739F1,$8E0787C31BF29D48
    Data.q $A9874E9CE715368A,$693B33804ADA223E,$AC247FD3C38DB3BA,$7D6C3DD25518842F,$1C3870FF395F8FB7
    Data.q $E93775E9D3A76852,$10B9CE294A0002CB,$4E78BD3B7DA665E2,$818CEE8B4E1D78CD,$18C72B8651B77E81
    Data.q $A8257D330D7EFBEB,$5A276F7BDEF5D20B,$DC37C836E8885555,$2250A8B18C94A5ED,$A3F1CF75A8FC3D33
    Data.q $711BDE1BA49980B4,$7F867B878F1E5CB9,$392F942AFF875B10,$AE7C2515CF84A2B9,$1B67032FC573E128
    Data.q $00000044E0E3E318,$6042AE444E454900
    Data.b $82
    pb_png_end:
  
  EndDataSection
Last edited by applePi on Wed May 15, 2013 7:08 am, edited 2 times in total.
Kelebrindae
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 01, 2008 3:23 pm

Re: primitive car

Post by Kelebrindae »

That's a very interesting piece of code, applePi.
I've tried something similar in the past, with former versions of PB, but your results are far better; "EnableHingeJointAngularMotor" didn't exist then, and it really makes a big difference. Well done! :)

One thing I discovered that may prove useful for you:
Collision detection for static objects is done against the mesh's triangles. And when cylindre bodies are used, they sometimes "hit" a triangle's boundary, even if the mesh is perfectly flat. (sorry, it's rather hard to explain...)
=> In the above source, if you replace

Code: Select all

CreatePlane(#plane , 2000, 2000, 1, 1, 5, 5)
by

Code: Select all

CreatePlane(#plane , 2000, 2000, 100, 100, 5, 5)
at line 113 and activate the "WorldDebug(#PB_World_DebugBody)" at line 175, you'll see that the truck "bounces" slightly when it hits the border of the tiles.

I have the feeling that Capsule bodies reduce this effect, but it isn't perfect. Or use a box body with mass = 0 instead of the static one; but boxes won't always fit if you want to build a more complex environnement (like a race track).
Tricky problem... :?
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: primitive car

Post by applePi »

Hi Kelebrindae
you are right it appears as if the car are running in a road with many small stones and it seems a one case for the car turn over. i have tested widening the distance between opposite wheels and making the wheels bigger, (but not correcting the car shape yet ) i think the effect on the car are less but not canceled, i have tried also the torus made from circles from points to cook a wheel but it is also not good, i hope a torus primitive may be added to the Ogre engine.
in fact your good example the "swinging Hammer" have inspired me to try the physics engine.

*i will test this: making a slope with many triangles using milkshape, and a car body with many triangles, to see if the slope will sink inside the body car sometimes or not, testing collisions between the two.
also modeling different wheels shapes

PS: i have edited the friction 3000 to be 1 for the front wheels and 1 for the back wheels above, it seems 3000 does not have a meaning as
in http://en.wikipedia.org/wiki/Friction#A ... f_friction which gives the rubber friction 1 , the proposed heavy engine on the front (weight 200) can force more friction to the front wheels than the back wheels
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: primitive car

Post by applePi »

i have found this way to turn front wheels left and right:
attach wheels to shaft with hinge joint, then apply force to either of its ends to turn left/right , but we can't attach the car body to the shaft with a joint normally, but through nodes look the code lines 73-77.
there is a problem, it is we must change the force direction on the shaft if we change the driving vector from right to left, ie if you want to advance to the right press Up, but if you want to drive to the left press Down. this is not the case with the above car .
press space to display the x,y,z for the car
Image

Code: Select all

Enumeration
  
  #cube = 100
   #LIGHT
   #CAMERA
   #mainwin
   #Joint
   #Joint2
   #Joint3
   #Joint4
   #shaft
   #plane
   #wheelR
   #wheelL
   #wheel_BR
   #wheel_BL
   #carBody
   #node
   #node2
         
 EndEnumeration

ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press L/R arrows to turn front wheels right/Left, press Space to check the x,y,z car positions", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;Initialize environment
InitEngine3D()
InitSprite()
OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-50, 0, 0, 0)

InitKeyboard()
SetFrameRate(60)

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/Sources\Data", #PB_3DArchive_FileSystem)

CreateLight(#LIGHT,RGB(22,222,0),0,-4,0,#PB_Light_Directional )
LightLookAt(#LIGHT, 0, 4, -5)

CreateCamera(#camera, 0, 0, 1000, 1000)
MoveCamera(#camera, -220, 42, 30)
CameraLookAt(#camera, 0, 0, 0)

EndIf

CreateMaterial(3, LoadTexture(3, "clouds.jpg"))
CreateMaterial(2, LoadTexture(2, "Geebee2.bmp"))
CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
CreateMaterial(0, LoadTexture(0, "terrain_texture.jpg"))

DisableMaterialLighting(0, #True)
;MaterialCullingMode(0, #PB_Material_NoCulling)

CreatePlane(#plane , 4000, 4000, 1, 1, 5, 5)
CreateEntity(#plane,MeshID(#plane),MaterialID(1), 0, 0, -10) 
MaterialCullingMode(1, #PB_Material_NoCulling)
MaterialBlendingMode(1, #PB_Material_Add )
EntityPhysicBody(#plane, #PB_Entity_StaticBody  ,0,0,5)

CreateCube(#carBody, 1)
CreateEntity(#carBody, MeshID(#carBody), MaterialID(2), 0,  2, 0)
ScaleEntity(#carBody,40,2,8)
EntityPhysicBody(#carBody, #PB_Entity_BoxBody ,2)

; shaft
CreateCylinder(#shaft, 1, 1)
CreateEntity(#shaft, MeshID(#shaft), MaterialID(1), 0,  2, 0)
ScaleEntity(#shaft,1,30,1)
RotateEntity(#shaft,90,0,0)
EntityPhysicBody(#shaft, #PB_Entity_CylinderBody  )
;node
CreateNode(#node, 0,  1, 0)
CreateNode(#node2, 0,  2, 0)
AttachNodeObject(#node, EntityID(#shaft))
AttachNodeObject(#node, NodeID(#node2))
AttachNodeObject(#node2, EntityID(#carbody))

; #wheelR
CreateCylinder(#wheelR, 1, 1)
CreateEntity(#wheelR, MeshID(#wheelR), MaterialID(0), 0,  3, -23) 
ScaleEntity(#wheelR,3,2,3)
RotateEntity(#wheelR,90,0,0)
EntityPhysicBody(#wheelR, #PB_Entity_CylinderBody   , 20,0,2)
HingeJoint(#Joint,EntityID(#shaft),0,-1.5,0, 0,1,0, EntityID(#wheelR),0,20.5,0, 0,1,0)

;#wheelR
CopyMesh(#wheelR,#wheelL)
CreateEntity(#wheelL, MeshID(#wheelL), MaterialID(0), 0,  3, 23) 
ScaleEntity(#wheelL, 3,2,3)
RotateEntity(#wheelL,90,0,0)
EntityPhysicBody(#wheelL, #PB_Entity_CylinderBody   , 20,0,2)
HingeJoint(#Joint2,EntityID(#shaft),0,1.5,0, 0,1,0, EntityID(#wheelL),0,-20.5,0, 0,1,0)

; back wheels
CopyMesh(#wheelR, #wheel_BR)
CopyMesh(#wheelR, #wheel_BL)
CreateEntity(#wheel_BR, MeshID(#wheel_BR), MaterialID(0), 40,  3, 3) 
CreateEntity(#wheel_BL, MeshID(#wheel_BL), MaterialID(0), 40,  3, -3) 
ScaleEntity(#wheel_BR, 3,2,3)
ScaleEntity(#wheel_BL, 3,2,3)
EntityPhysicBody(#wheel_BR, #PB_Entity_CylinderBody ,20,0,1) ; note the friction are just 1
EntityPhysicBody(#wheel_BL, #PB_Entity_CylinderBody ,20,0,1) ; note the friction are just 1

HingeJoint(#Joint3,EntityID(#wheel_BR),0,0,0, 0,1,0, EntityID(#carBody),20,0,6, 0,0,1)
HingeJoint(#Joint4,EntityID(#wheel_BL),0,0,0, 0,1,0, EntityID(#carBody),20,0,-6, 0,0,1)

EnableHingeJointAngularMotor(#Joint, #True, 20, 50)
EnableHingeJointAngularMotor(#Joint2, #True, 20, 50)
;PointJoint(1,EntityID(#shaft),0,0,0, EntityID(#carBody),-20,-2,0)
ConeTwistJoint(1, EntityID(#shaft), 0, 0, 0, EntityID(#carBody), -20,-2,0)

;WorldDebug(#PB_World_DebugBody)
Repeat
  Event = WindowEvent()
       
   RenderWorld()
   FlipBuffers()
   ApplyEntityForce(#shaft,  -5, 0, 0,  0,0,0)
   ExamineKeyboard()
   If KeyboardPushed(#PB_Key_Up )
          
     ApplyEntityImpulse(#shaft,  -20, 0, 0,  0,0,-15)
     ApplyEntityImpulse(#shaft,  -20, 0, 0,  0,0,15)
          
   ElseIf KeyboardPushed(#PB_Key_Down)
          
     ;EnableHingeJointAngularMotor(#Joint, #True, 30, 100)
     ;EnableHingeJointAngularMotor(#Joint2, #True, 30, 100)
     ApplyEntityImpulse(#shaft,  10, 0, 0,  0,0,0)
              
   EndIf
   
       
   If KeyboardPushed(#PB_Key_Left )
     ApplyEntityImpulse(#shaft,  -20, 0, 0,  0,0,-15) ; apply force to the shaft right
     ElseIf KeyboardPushed(#PB_Key_Right )
     ApplyEntityImpulse(#shaft,  -20, 0, 0,  0,0,15)  ; apply force to the shaft left
       
     
   EndIf  
   If KeyboardReleased(#PB_Key_Space) ; to show car positions
     Debug "X = " + StrF(EntityX(#carBody))
     Debug "Y = " + StrF(EntityY(#carBody))
     Debug "Z = " +StrF(EntityZ(#carBody))
   EndIf
       
   MoveCamera(#camera, EntityX(#carBody)+100, EntityY(#carBody)+70, EntityZ(#carBody)-90, #PB_Absolute )
   CameraLookAt(#camera, EntityX(#carBody), EntityY(#carBody), EntityZ(#carBody) )
      
 Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow
  
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: primitive car

Post by applePi »

i should say that kelebrindae have posted yesterday a car demo which solves all the above problems, i posted this in case someone falling on this thread begins to think that there is no working car in purebasic, try the example here http://www.purebasic.fr/french/viewtopi ... 13&t=13655
it can be a base for other models and variations.
Post Reply