Page 1 of 2

example GenericJoint - car control

Posted: Wed Mar 06, 2019 10:18 am
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)

Re: example GenericJoint - car control

Posted: Wed Mar 06, 2019 11:54 am
by TI-994A
Marvellous!

Re: example GenericJoint - car control

Posted: Mon Mar 11, 2019 8:45 am
by dige
I wonder, where's the code hidden?! ;-)
Great example! :-D

Re: example GenericJoint - car control

Posted: Mon Mar 18, 2019 9:49 pm
by Fred
Neat !

Re: example GenericJoint - car control

Posted: Tue Mar 19, 2019 10:07 am
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?

Re: example GenericJoint - car control

Posted: Tue Mar 19, 2019 11:52 am
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.

Re: example GenericJoint - car control

Posted: Tue Mar 19, 2019 2:40 pm
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

Re: example GenericJoint - car control

Posted: Tue Mar 19, 2019 4:52 pm
by Little John
Very nice indeed!

Re: example GenericJoint - car control

Posted: Tue Mar 19, 2019 5:09 pm
by pf shadoko
for those who have a crash: try disabling shadows
comment line 31 (WorldShadows(#PB_Shadow_Additive))

Re: example GenericJoint - car control

Posted: Tue Mar 19, 2019 5:29 pm
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?

Re: example GenericJoint - car control

Posted: Tue Mar 19, 2019 7:44 pm
by pf shadoko
try inserting :
WindowEvent()
(line 73, after 'repeat')

Re: example GenericJoint - car control

Posted: Wed Mar 20, 2019 12:08 pm
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.

Re: example GenericJoint - car control

Posted: Wed Mar 20, 2019 2:00 pm
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!

Re: example GenericJoint - car control

Posted: Wed Mar 20, 2019 3:01 pm
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)

Re: example GenericJoint - car control

Posted: Wed Mar 20, 2019 3:59 pm
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. :-)