[4.60 beta] Démo Physique + joints
Publié : mar. 05/avr./2011 14:26
Plutôt élémentaire, mais ça permet de se faire une petite idée. Et personnellement, il le plaît bien, le nouveau moteur physique...
(Et si quelqu'un a trouvé comment déplacer une line3D ou comment l'attacher à d'autres objets, je suis preneur...)
[EDIT:] F1, F2, F3 pour changer de vue.

(Et si quelqu'un a trouvé comment déplacer une line3D ou comment l'attacher à d'autres objets, je suis preneur...)
[EDIT:] F1, F2, F3 pour changer de vue.
Code : Tout sélectionner
; Window size
#SCREENWIDTH = 800
#SCREENHEIGHT = 500
;- initialization
InitSprite()
InitEngine3D()
InitKeyboard()
;- Window
OpenWindow(0, 0, 0, #SCREENWIDTH, #SCREENHEIGHT, "Joint test", #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
OpenWindowedScreen(WindowID(0), 0, 0, #SCREENWIDTH,#SCREENHEIGHT, 0, 0, 0,#PB_Screen_SmartSynchronization)
;-Texture
Add3DArchive(".",#PB_3DArchive_FileSystem)
CreateImage(0,128, 128)
StartDrawing(ImageOutput(0))
Box(0, 0, 128, 128, $FFFFFF)
StopDrawing()
SaveImage(0,"temp.bmp")
FreeImage(0)
LoadTexture(0,"temp.bmp")
DeleteFile("temp.bmp")
;-Material
CreateMaterial(1,TextureID(0))
MaterialAmbientColor(1,#PB_Material_AmbientColors)
CreateMaterial(2,TextureID(0))
MaterialAmbientColor(2,$FFFF00)
CreateMaterial(3,TextureID(0))
MaterialAmbientColor(3,$0077FF)
CreateMaterial(4,TextureID(0))
MaterialAmbientColor(4,$FF00FF)
;- Entities
CreatePlane(4,2,2,20,20,1,1)
sol = CreateEntity(#PB_Any, MeshID(4), MaterialID(1))
EntityPhysicBody(sol, #PB_Entity_StaticBody)
CreateCube(4, 1)
socle=CreateEntity(#PB_Any, MeshID(4), MaterialID(1))
ScaleEntity(socle, 10,1,10)
EntityLocate(socle, 12,8,0)
EntityPhysicBody(socle, #PB_Entity_StaticBody)
; Mobile
CreateCube(1, 1.0)
cube=CreateEntity(#PB_Any, MeshID(1), MaterialID(2))
EntityLocate(cube,-5,9,0)
EntityPhysicBody(cube, #PB_Entity_BoxBody)
CreateSphere(2, 0.5)
sphere=CreateEntity(#PB_Any, MeshID(2), MaterialID(3))
EntityLocate(sphere, -5,6,0)
EntityPhysicBody(sphere, #PB_Entity_SphereBody, 2.0)
; Attache the cube and the sphere, and figure this link with a 3D line
PointJoint(cube,0,0,0,sphere,0,-3,0)
CreateLine3D(3,5,9,0,$FF0000,5,6,0,$00FF00)
; Targets
For i = 1 To 4
temp = CreateEntity(#PB_Any, MeshID(1), MaterialID(4))
EntityLocate(temp,0,0.5,(i*1.1)-2.8)
EntityPhysicBody(temp, #PB_Entity_BoxBody, 1)
Next i
For i = 1 To 3
temp = CreateEntity(#PB_Any, MeshID(1), MaterialID(4))
EntityLocate(temp,0,1.5,(i*1.1)-2.3)
EntityPhysicBody(temp, #PB_Entity_BoxBody, 1)
Next i
For i = 1 To 2
temp = CreateEntity(#PB_Any, MeshID(1), MaterialID(4))
EntityLocate(temp,0,2.5,(i*1.1)-1.8)
EntityPhysicBody(temp, #PB_Entity_BoxBody, 1)
Next i
;- Camera
CreateCamera(0, 0, 0, 100, 100)
CameraLocate(0,0,12,20)
CameraLookAt(0,0,5,0)
;-Light
AmbientColor(RGB(105,105,105))
CreateLight(0,RGB(160,160,255),-200,300,0)
;CreateLight(1,RGB(255,127,0),-200,-200,200)
WorldShadows(#PB_Shadow_Additive)
;- Main loop
angle.f = 0
angle2.f = 0
Repeat
Delay(1)
While WindowEvent() : Wend
;- F1, F2, F3 : Change view
If ExamineKeyboard()
If KeyboardReleased(#PB_Key_F1)
CameraLocate(0,-12,2.5,0)
CameraLookAt(0,0,3,0)
EndIf
If KeyboardReleased(#PB_Key_F2)
CameraLocate(0,0,12,20)
CameraLookAt(0,0,5,0)
EndIf
If KeyboardReleased(#PB_Key_F3)
CameraLocate(0,3,5,10)
CameraLookAt(0,0,3,0)
EndIf
;- Return : Display FPS
If KeyboardReleased(#PB_Key_Return)
MessageRequester("Infos","FPS = " + Str(Engine3DFrameRate(#PB_Engine3D_Average)))
EndIf
EndIf
; Move the cube
angle+0.02
angle2+0.005
EntityLocate(cube,-5*Cos(angle),9 + 5*Sin(angle),2*Cos(angle2))
; Redraw the line3D (I don't know how to do it! So I delete/recreate it in each frame...)
FreeEntity(3)
CreateLine3D(3,EntityX(cube),EntityY(cube),EntityZ(cube),$FF0000,EntityX(sphere),EntityY(sphere),EntityZ(sphere),$00FF00)
; Render
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
End