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



