example GenericJoint - car control

Everything related to 3D programming
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 285
Joined: Thu Jul 09, 2015 9:07 am

example GenericJoint - car control

Post by pf shadoko »

a small example of car control via GenericJoint and ApplyEntityTorque

[EDIT] I added "WindowEvent()" in main loop

Code: Select all

; GenericJoint - exemple voiture - pf Shadoko - 2018

Procedure matiere(num,dx,dy,c1,c2, brillance=$888888)
  CreateTexture(num,dx,dy)
  StartDrawing(TextureOutput(num))
  DrawingMode(#PB_2DDrawing_AllChannels )
  Box(0,0,dx,dy,c2)
  Box(2,2,dx-4,dy-4,c1) 
  StopDrawing()
  CreateMaterial(num, TextureID(num))
  If brillance:SetMaterialColor(num, #PB_Material_SpecularColor, brillance):MaterialShininess(num, 20):EndIf
EndProcedure

EnableExplicit
Define.f angle,sens,dsens,h
Define i,ii,j,c,l,dis=0,vdis=2<<dis*1,ex,ey,ombre

InitEngine3D():InitSprite():InitKeyboard():InitMouse()
OpenWindow(0, 0, 0, 0,0, " Test GenericJoint  - curseur + F2(camera) + F3(ombres)",#PB_Window_Maximize)
ex=WindowWidth (0,#PB_Window_InnerCoordinate)
ey=WindowHeight(0,#PB_Window_InnerCoordinate)
OpenWindowedScreen(WindowID(0), 0, 0, ex, ey, 0, 0, 0)

CreateLight(0,$888888, 5000, 3000, 2000):SetLightColor(0, #PB_Light_SpecularColor, $ffffff)
AmbientColor($777777)

CreateCamera(0, 0, 0, 100, 100):MoveCamera(0,0,10,-20)
CameraBackColor(0,$888888) 

;WorldDebug(#PB_World_DebugBody)
WorldShadows(#PB_Shadow_Additive)

matiere(0,256,256,$444444,$111111)
matiere(1,256,256,$884444,$441111)
matiere(2,256,256,$ffff00,$444400)
matiere(3,256,256,$ffffff,$444444)
matiere(4,256,256,$4444ff,$4444ff)
matiere(5,256,256,$44ffff,$00aaaa)
matiere(10,256,256,$888888,$222222)

;---------------------piste
CreatePlane(0,200,200,1,1,200,200)
CreateEntity(100,MeshID(0),MaterialID(10),0,0,0):CreateEntityBody(100,#PB_Entity_StaticBody,1,0,1)
CreateCone(0,4,2,32,4)
For i=200 To 219:CreateEntity(i,MeshID(0),MaterialID(4),Random(100)-50,1,Random(100)-50):CreateEntityBody(i,#PB_Entity_StaticBody,1,0,1):Next
CreateCube(0,4)
For i=300 To 339:CreateEntity(i,MeshID(0),MaterialID(1),Random(100)-50,-1,Random(100)-50):RotateEntity(i,25,Random(360),0):CreateEntityBody(i,#PB_Entity_StaticBody,1,0,1):Next
CreateCube(0,0.1):TransformMesh(0,0,0.025,0,1,0.5,50,0,0,0):UpdateMeshBoundingBox(0)
For i=400 To 439:CreateEntity(i,MeshID(0),MaterialID(2),Random(100)-50,0,Random(100)-50):RotateEntity(i,0,Random(360),0):CreateEntityBody(i,#PB_Entity_StaticBody,1,0,1):Next

;--------------------- voiture
h=2
CreateCube(0,2):TransformMesh(0,0,0,0,0.4,0.2,0.8,0,0,0):UpdateMeshBoundingBox(0)
CreateEntity(0, MeshID(0), MaterialID(5),0,h,0):CreateEntityBody(0, #PB_Entity_BoxBody ,4, 0.0,0.3):SetEntityCollisionFilter(0, 1,2)
SetEntityAttribute(0,#PB_Entity_LinearSleeping,0)

;---------------------roues
CreateCylinder(2, 0.3, 0.3)
For j=-1 To 1 Step 2:For i=-1 To 1 Step 2:c+1
    CreateEntity(c, MeshID(2), MaterialID(0),0.6*i, -0.2+h, -0.8*j):CreateEntityBody(c, #PB_Entity_CylinderBody, 1, 0,0.7):SetEntityCollisionFilter(c, 1,2) 
    RotateEntity(c,0,0,90)
    GenericJoint(c, EntityID(0),0.6*i, -0.2, -0.8*j, EntityID(c), 0, 0, 0)
    SetJointAttribute(c, #PB_Joint_EnableSpring,1,1)
    SetJointAttribute(c, #PB_Joint_Damping,0.005,1) 
    SetJointAttribute(c, #PB_Joint_Stiffness,200,1) 
    SetJointAttribute(c, #PB_Joint_NoLimit, 0, 3) 
Next :Next

CreateNode(0,0,0.0,-1):AttachEntityObject(0,"",NodeID(0))
CreateNode(1,0,0.1,0) :AttachEntityObject(0,"",NodeID(1))

Repeat
  While WindowEvent():Wend 
  ExamineMouse()
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_F2):dis=(dis+1)%3:vdis=2<<dis*1:EndIf
  If KeyboardReleased(#PB_Key_F3):ombre=1-ombre:If ombre:WorldShadows(#PB_Shadow_Additive):Else:WorldShadows(#PB_Shadow_None):EndIf:EndIf
  
  sens=(GetEntityAttribute(1,#PB_Entity_AngularVelocity)+GetEntityAttribute(2,#PB_Entity_AngularVelocity))/2
  angle+(Bool(KeyboardPushed(#PB_Key_Left))-Bool(KeyboardPushed(#PB_Key_Right)))*-0.02:angle/(1+sens*0.002)
  dsens=(Bool(KeyboardPushed(#PB_Key_Up))-Bool(KeyboardPushed(#PB_Key_Down))) * 100
  SetJointAttribute(1,#PB_Joint_Position,angle,4)
  SetJointAttribute(2,#PB_Joint_Position,angle,4)
  For i=1 To 4:ApplyEntityTorque(i,0,dsens/50,0,#PB_Local):Next 
  
  CameraFollow(0, NodeID(1), -180,NodeY(0)+vdis, 2*vdis, 0.1, 0.1)
  
  RenderWorld()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Last edited by pf shadoko on Wed Mar 20, 2019 3:56 pm, edited 1 time in total.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: example GenericJoint - car control

Post by TI-994A »

Marvellous!
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
dige
Addict
Addict
Posts: 1247
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: example GenericJoint - car control

Post by dige »

I wonder, where's the code hidden?! ;-)
Great example! :-D
"Daddy, I'll run faster, then it is not so far..."
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: example GenericJoint - car control

Post by Fred »

Neat !
User avatar
C87
Enthusiast
Enthusiast
Posts: 176
Joined: Mon Jul 17, 2017 7:22 am
Location: Cotswolds England

Re: example GenericJoint - car control

Post by C87 »

I don't know if anyone can help but this type of code, the 3D stuff is a complete mystery to me. Maybe because I'm from the database systems brigade?

In the past I have looked through the demos that come with PureB and they just fall over. It didn't particularly bother me as from experience many demos and examples issued with a language fall over. (In fact it seems to be custom & practice with Microsoft!) So I just presumed they didn't work. Then with this example, which everyone else finds to be brilliant I have the same result. So it must be me! :cry: The demo programs I tried are: Carphysics, Character, PinBall and Tank.

They all end without doing anything and all end with the fatal error message box “The debugged executable quit unexpectedly” which isn't particularly helpful.
This example from pf shadoko opens a blank screen and crashes out in just the same way. Can anyone point me in the right direction and tell what simple step I haven't taken?
If it's falling over......just remember the computer is never wrong!
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: example GenericJoint - car control

Post by DK_PETER »

@C87
A couple of suggestions:
Install DirectX 9.0c
https://www.microsoft.com/en-us/downloa ... x?id=34429

You could also try to run it with 'OpenGL' as subsystem.

@pf_Shadoko
Yes..neat indeed.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
C87
Enthusiast
Enthusiast
Posts: 176
Joined: Mon Jul 17, 2017 7:22 am
Location: Cotswolds England

Re: example GenericJoint - car control

Post by C87 »

@DK_PETER
Thanks for that, I'll need to look into how they work. Maybe the OpenGL of which I've now seen in a few examples. The ActiveXv9 I'm not too keen on installing as MS have v11.3 and/or 12 for Win10. I may have ActiveX9.0 on a WinXP pc, so I can try it there 1st.

It's always the case, something that looks trivial ends up taking hours and hours :lol:

Cheers anyway, C87
If it's falling over......just remember the computer is never wrong!
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: example GenericJoint - car control

Post by Little John »

Very nice indeed!
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 285
Joined: Thu Jul 09, 2015 9:07 am

Re: example GenericJoint - car control

Post by pf shadoko »

for those who have a crash: try disabling shadows
comment line 31 (WorldShadows(#PB_Shadow_Additive))
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: example GenericJoint - car control

Post by davido »

@pf shadoko,
I'm using Windows 10 with PureBasic 5.70LTS

I just got a white blank screen which eventually crashed.
I tried 'commenting out' line 31: got a screen showing a vehicle on a terrain. This crashed when pressing a key.

I think something must be missing rather than a bug in your code, any ideas?
DE AA EB
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 285
Joined: Thu Jul 09, 2015 9:07 am

Re: example GenericJoint - car control

Post by pf shadoko »

try inserting :
WindowEvent()
(line 73, after 'repeat')
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: example GenericJoint - car control

Post by Fred »

Ho yeah, you need to flush the event in windowed mode, even if you don't use them:

Code: Select all

While WindowEvent() : Wend
In the main repeat loop. In fullscreen mode, it's done automatically.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: example GenericJoint - car control

Post by Little John »

davido wrote:@pf shadoko,
I'm using Windows 10 with PureBasic 5.70LTS

I just got a white blank screen which eventually crashed.
Same here on my notebook, even after inserting the code for flushing events, as Fred wrote. :-(

Yesterday, on a different computer (also on Windows 10), the code worked fine.

Is an advanced graphics card required, or any special setting on the PC regarding 3D processing?

//edit:
That fixed the problem here, thank you!
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: example GenericJoint - car control

Post by #NULL »

I could make it work on Window 10 by disabling ;WorldShadows(#PB_Shadow_Additive). It makes renderWorld() hang or crash. I did not try other arguments.
With opengl it's extremely slow/laggy. I'm not sure about my system. dxdiag says direct x 12 and I have read that Windows comes with opengl. I have nvidia drivers installed for the card.
Here is my debug code, printing most function call results. All non-zero.

Code: Select all

; GenericJoint - exemple voiture - pf Shadoko - 2018

OpenConsole()
Procedure deb(i.i)
  PrintN(Str(i))
EndProcedure
Procedure debs(s.s)
  PrintN(s)
EndProcedure

Procedure matiere(num,dx,dy,c1,c2, brillance=$888888)
  deb(CreateTexture(num,dx,dy))
  deb(StartDrawing(TextureOutput(num)))
  DrawingMode(#PB_2DDrawing_AllChannels )
  Box(0,0,dx,dy,c2)
  Box(2,2,dx-4,dy-4,c1)
  StopDrawing()
  CreateMaterial(num, TextureID(num))
  If brillance
    SetMaterialColor(num, #PB_Material_SpecularColor, brillance)
    MaterialShininess(num, 20)
  EndIf
EndProcedure

EnableExplicit
Define.f angle,sens,dsens,h
Define i,ii,j,c,l,dis=0,vdis=2<<dis*1,ex,ey,ombre

deb(InitEngine3D())
deb(InitSprite())
deb(InitKeyboard())
deb(InitMouse())
deb(OpenWindow(0, 0, 0, 0,0, " Test GenericJoint  - curseur + F2(camera) + F3(ombres)",#PB_Window_Maximize))
ex=WindowWidth (0,#PB_Window_InnerCoordinate)
ey=WindowHeight(0,#PB_Window_InnerCoordinate)
deb(OpenWindowedScreen(WindowID(0), 0, 0, ex, ey, 0, 0, 0))

deb(CreateLight(0,$888888, 5000, 3000, 2000))
SetLightColor(0, #PB_Light_SpecularColor, $ffffff)
AmbientColor($777777)

deb(CreateCamera(0, 0, 0, 100, 100))
MoveCamera(0,0,10,-20)
CameraBackColor(0,$888888)

;WorldDebug(#PB_World_DebugBody)
;WorldShadows(#PB_Shadow_Additive)

matiere(0,256,256,$444444,$111111)
matiere(1,256,256,$884444,$441111)
matiere(2,256,256,$ffff00,$444400)
matiere(3,256,256,$ffffff,$444444)
matiere(4,256,256,$4444ff,$4444ff)
matiere(5,256,256,$44ffff,$00aaaa)
matiere(10,256,256,$888888,$222222)

;---------------------piste
deb(CreatePlane(0,200,200,1,1,200,200))
deb(CreateEntity(100,MeshID(0),MaterialID(10),0,0,0))
CreateEntityBody(100,#PB_Entity_StaticBody,1,0,1)
deb(CreateCone(0,4,2,32,4))
For i=200 To 219
  deb(CreateEntity(i,MeshID(0),MaterialID(4),Random(100)-50,1,Random(100)-50))
  CreateEntityBody(i,#PB_Entity_StaticBody,1,0,1)
Next
deb(CreateCube(0,4))
For i=300 To 339
  deb(CreateEntity(i,MeshID(0),MaterialID(1),Random(100)-50,-1,Random(100)-50))
  RotateEntity(i,25,Random(360),0)
  CreateEntityBody(i,#PB_Entity_StaticBody,1,0,1)
Next
deb(CreateCube(0,0.1))
TransformMesh(0,0,0.025,0,1,0.5,50,0,0,0)
UpdateMeshBoundingBox(0)
For i=400 To 439
  deb(CreateEntity(i,MeshID(0),MaterialID(2),Random(100)-50,0,Random(100)-50))
  RotateEntity(i,0,Random(360),0)
  CreateEntityBody(i,#PB_Entity_StaticBody,1,0,1)
Next

;--------------------- voiture
h=2
deb(CreateCube(0,2))
TransformMesh(0,0,0,0,0.4,0.2,0.8,0,0,0)
UpdateMeshBoundingBox(0)
deb(CreateEntity(0, MeshID(0), MaterialID(5),0,h,0))
CreateEntityBody(0, #PB_Entity_BoxBody ,4, 0.0,0.3)
SetEntityCollisionFilter(0, 1,2)
SetEntityAttribute(0,#PB_Entity_LinearSleeping,0)

;---------------------roues
deb(CreateCylinder(2, 0.3, 0.3))
For j=-1 To 1 Step 2
  For i=-1 To 1 Step 2:c+1
    deb(CreateEntity(c, MeshID(2), MaterialID(0),0.6*i, -0.2+h, -0.8*j))
    CreateEntityBody(c, #PB_Entity_CylinderBody, 1, 0,0.7)
    SetEntityCollisionFilter(c, 1,2)
    RotateEntity(c,0,0,90)
    deb(GenericJoint(c, EntityID(0),0.6*i, -0.2, -0.8*j, EntityID(c), 0, 0, 0))
    SetJointAttribute(c, #PB_Joint_EnableSpring,1,1)
    SetJointAttribute(c, #PB_Joint_Damping,0.005,1)
    SetJointAttribute(c, #PB_Joint_Stiffness,200,1)
    SetJointAttribute(c, #PB_Joint_NoLimit, 0, 3)
  Next
Next

deb(CreateNode(0,0,0.0,-1))
AttachEntityObject(0,"",NodeID(0))
deb(CreateNode(1,0,0.1,0))
AttachEntityObject(0,"",NodeID(1))

;End

Repeat
  debs("loop")
  While WindowEvent() : Wend
  Delay(1)
  ExamineMouse()
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_F2):dis=(dis+1)%3:vdis=2<<dis*1:EndIf
  If KeyboardReleased(#PB_Key_F3):ombre=1-ombre:If ombre:WorldShadows(#PB_Shadow_Additive):Else:WorldShadows(#PB_Shadow_None):EndIf:EndIf
 
  debs("loop1")
  sens=(GetEntityAttribute(1,#PB_Entity_AngularVelocity)+GetEntityAttribute(2,#PB_Entity_AngularVelocity))/2
  debs("loop2")
  angle+(Bool(KeyboardPushed(#PB_Key_Left))-Bool(KeyboardPushed(#PB_Key_Right)))*-0.02:angle/(1+sens*0.002)
  debs("loop3")
  dsens=(Bool(KeyboardPushed(#PB_Key_Up))-Bool(KeyboardPushed(#PB_Key_Down))) * 100
  debs("loop4")
  SetJointAttribute(1,#PB_Joint_Position,angle,4)
  debs("loop5")
  SetJointAttribute(2,#PB_Joint_Position,angle,4)
  debs("loop6")
  For i=1 To 4
    ApplyEntityTorque(i,0,dsens/50,0,#PB_Local)
  Next
  debs("loop7")
 
  CameraFollow(0, NodeID(1), -180,NodeY(0)+vdis, 2*vdis, 0.1, 0.1)
  debs("loop8")
 
  RenderWorld()
  debs("loop9")
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: example GenericJoint - car control

Post by Little John »

#NULL wrote:I could make it work on Window 10 by disabling ;WorldShadows(#PB_Shadow_Additive).
Works here on Windows 10 with and without OpenGL.
There are rather wide grey margins at the right and at the bottom of the window. However, they were already there in the original version. :-)
Post Reply