[4.60 beta3] pendules harmoniques

Généralités sur la programmation 3D
kelebrindae
Messages : 579
Inscription : ven. 11/mai/2007 15:21

[4.60 beta3] pendules harmoniques

Message par kelebrindae »

Bonjour !

Juste un petit test rapide du moteur physique de PB4.60, dans lequel je voulais reproduire l'expérience visible ici: http://www.youtube.com/watch?v=yVkdfJ9PkRQ
=> Une série de pendules (dont la longueur est soigneusement calculée) forme différents motifs avant de finalement de re-synchroniser.

Bon, ça n'a pas grand intérêt, mais c'est marrant à regarder et ça permet de vérifier que le moteur physique est réaliste :wink: ...

(F1 - F2 pour changer de caméra, ENTER pour afficher les FPS)

Code : Tout sélectionner

; Pendulum waves experiment
; See here for more infos: 
; http://books.google.fr/books?id=gTpMdcYienQC&pg=PA195&lpg=PA195&dq=Pendulum+Waves:+The+Physics+of+a+Set+of+Tuned+Pendulums&source=bl&ots=OAzAaa_QEr&sig=BhtGLaqaxiVTuy5DpW4l8af99s8&hl=fr&ei=VP_cTa36GdS58gPhwNwC&sa=X&oi=book_result&ct=result&resnum=3&ved=0CDMQ6AEwAg#v=onepage&q=Pendulum%20Waves%3A%20The%20Physics%20of%20a%20Set%20of%20Tuned%20Pendulums&f=false


; Window size
#SCREENWIDTH = 800
#SCREENHEIGHT = 500

;- initialization
InitSprite()
InitEngine3D()
InitKeyboard()

;- Window
OpenWindow(0, 0, 0, #SCREENWIDTH, #SCREENHEIGHT, "Pendulum test", #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
OpenWindowedScreen(WindowID(0), 0, 0, #SCREENWIDTH,#SCREENHEIGHT, 0, 0, 0,#PB_Screen_SmartSynchronization)

;- 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),0,300,0) 
WorldShadows(#PB_Shadow_Additive)

;- Texture
Add3DArchive(".",#PB_3DArchive_FileSystem)
CreateImage(0,128, 128) 
StartDrawing(ImageOutput(0)) 
  Box(0, 0, 128, 128, $FFFFFF)
StopDrawing()
SaveImage(0,"temp.bmp")
LoadTexture(0,"temp.bmp")
DeleteFile("temp.bmp")
StartDrawing(ImageOutput(0)) 
  Box(0, 0, 64, 64, $BBBBBB)
  Box(64, 64, 64, 64, $BBBBBB)
StopDrawing()
SaveImage(0,"temp2.bmp")
FreeImage(0)
LoadTexture(1,"temp2.bmp")
DeleteFile("temp2.bmp")

;- Materials
CreateMaterial(1,TextureID(1))
MaterialAmbientColor(1,#PB_Material_AmbientColors)
CreateMaterial(2,TextureID(0))
MaterialAmbientColor(2,$D0B86B)
CreateMaterial(3,TextureID(0))
MaterialAmbientColor(3,$0077FF)

;- Entities
CreatePlane(1,2,2,20,20,15,15)
sol  = CreateEntity(#PB_Any, MeshID(1), MaterialID(1))
EntityPhysicBody(sol, #PB_Entity_StaticBody)    

CreateCube(2, 1)
support  = CreateEntity(#PB_Any, MeshID(2), MaterialID(3))
ScaleEntity(support,20,0.3,0.3)
EntityLocate(support,0,10,0)
EntityPhysicBody(support, #PB_Entity_StaticBody)    

support2  = CreateEntity(#PB_Any, MeshID(2), MaterialID(3))
ScaleEntity(support2,0.4,12,0.4)
RotateEntity(support2,30,0,0)
EntityLocate(support2,10,5,-3)
EntityPhysicBody(support2, #PB_Entity_StaticBody)    
support3  = CreateEntity(#PB_Any, MeshID(2), MaterialID(3))
ScaleEntity(support3,0.4,12,0.4)
RotateEntity(support3,-30,0,0)
EntityLocate(support3,10,5,3)
EntityPhysicBody(support3, #PB_Entity_StaticBody)    
support4  = CreateEntity(#PB_Any, MeshID(2), MaterialID(3))
ScaleEntity(support4,0.4,12,0.4)
RotateEntity(support4,30,0,0)
EntityLocate(support4,-10,5,-3)
EntityPhysicBody(support2, #PB_Entity_StaticBody)    
support5  = CreateEntity(#PB_Any, MeshID(2), MaterialID(3))
ScaleEntity(support5,0.4,12,0.4)
RotateEntity(support5,-30,0,0)
EntityLocate(support5,-10,5,3)
EntityPhysicBody(support5, #PB_Entity_StaticBody)    

; Pendulums
#NBPENDULUM = 16
Global Dim sph.i(#NBPENDULUM + 1)
Global position.f,stringLength.f
CreateSphere(3, 0.5)
For i=1 To #NBPENDULUM
  
  stringLength = 980.6 * Pow(15 / ( 2 * #PI * (24 + i) ),2) 
  position = - 9.3 + i*1.10
  
  ; Create a sphere
  sph(i)=CreateEntity(#PB_Any, MeshID(3), MaterialID(2))
  EntityLocate(sph(i), EntityX(support) + position,EntityY(support) - stringLength,EntityZ(support))
  EntityPhysicBody(sph(i), #PB_Entity_SphereBody)    

  ; Attach the support and the sphere
  PointJoint(support,position,0,0,sph(i),0,stringLength,0)
  CreateLine3D(100 + i,EntityX(support) + position,EntityY(support),EntityZ(support),$77FF00,EntityX(sph(i)),EntityY(sph(i)),EntityZ(sph(i)),$77FF00)

  ; Gently push the sphere
  ApplyEntityImpulse(sph(i),0,0,4)
Next i


;- Main loop
angle.f = 0
angle2.f = 0
Repeat
  Delay(1)
  While WindowEvent() : Wend
  
  ;- F1, F2 : Change view
  If ExamineKeyboard()
    If KeyboardReleased(#PB_Key_F1)
      CameraLocate(0,-15,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
    
    ;- Return : Display FPS
    If KeyboardReleased(#PB_Key_Return)
      MessageRequester("Infos","FPS = " + Str(Engine3DFrameRate(#PB_Engine3D_Average)))
    EndIf

  EndIf
  
  ; Redraw the line3D to  figure the prendulums' strings
  For i=1 To #NBPENDULUM
    position = - 9.3 + i*1.10
    FreeMesh(100 + i)
    CreateLine3D(100 + i,EntityX(support) + position,EntityY(support),EntityZ(support),$77FF00,EntityX(sph(i)),EntityY(sph(i)),EntityZ(sph(i)),$77FF00)
  Next i
  
  ; Render
  RenderWorld()
  FlipBuffers()
 
Until KeyboardPushed(#PB_Key_Escape)

End
Les idées sont le souvenir de choses qui ne se sont pas encore produites.
Avatar de l’utilisateur
graph100
Messages : 1318
Inscription : sam. 21/mai/2005 17:50

Re: [4.60 beta3] pendules harmoniques

Message par graph100 »

C'est magnifique :mrgreen: et puis ça permet d'avoir un beau code d'exemple sur le moteur physique et un peu de 3D !
_________________________________________________
Mon site : CeriseCode (Attention Chantier perpétuel ;))
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Re: [4.60 beta3] pendules harmoniques

Message par djes »

Ah, cool, justement j'avais vu cette vidéo qui m'avait fait penser à des vectorballs... J'avais d'ailleurs cherché assez longtemps une de ces vieilles intros avec un damier, les montagnes et les vectorballs :)
Merci!
Répondre