Page 1 sur 1

[3D] Les jointures résistent !

Publié : lun. 15/juil./2013 16:20
par falsam
Dans le code ci-dessous, j'assemble 5 planches avec des jointures pour former un pont.
Le but recherché et que quand je presse la touche espace, les joints lâchent et trois des 5 planches tombent.

- Tant que les planches sont en mouvement pas de souci ça fonctionne.
- Attendez maintenant que les planches soit fixent et pressez la touche espace : Rien ne se passe.

Code : Tout sélectionner

Enumeration
  #Joint1
  #Joint2
  #Joint3
  #Joint4
EndEnumeration

InitEngine3D()
InitKeyboard()
InitSprite()

window = OpenWindow(#PB_Any,0,0,1024,768,"Les jointures font de la résistance")
OpenWindowedScreen(WindowID(window),0,0,1024,768)
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)


; Une forme
Cube_Mesh = CreateCube(#PB_Any, 1) 

; Materiel 
Texture1 = CreateTexture(#PB_Any,512,512)
StartDrawing(TextureOutput(Texture1))
Box(0,0,512,512,RGB(0, 0, 0))
Box(1,1,510,510,RGB(255, 216, 0))
StopDrawing()
Material1 = CreateMaterial(#PB_Any,TextureID(texture1))


Plank0 = CreateEntity(#PB_Any, MeshID(Cube_Mesh), MaterialID(Material1),  0, 0, 0)
ScaleEntity(Plank0, 4, 0.2, 2.5)
EntityPhysicBody(Plank0, #PB_Entity_StaticBody)

Plank1 = CreateEntity(#PB_Any, MeshID(Cube_Mesh), MaterialID(Material1), 0, 0, 8)
ScaleEntity(Plank1, 4, 0.2, 2.5)
EntityPhysicBody(Plank1, #PB_Entity_BoxBody, 0.1)

Plank2 = CreateEntity(#PB_Any, MeshID(Cube_Mesh), MaterialID(Material1), 0, 0, 8)
ScaleEntity(Plank2, 4, 0.2, 2.5)
EntityPhysicBody(Plank2, #PB_Entity_BoxBody, 0.1)

Plank3 = CreateEntity(#PB_Any, MeshID(Cube_Mesh), MaterialID(Material1), 0, 0, 8)
ScaleEntity(Plank3, 4, 0.2, 2.5)
EntityPhysicBody(Plank3, #PB_Entity_BoxBody, 0.1)

Plank4 = CreateEntity(#PB_Any, MeshID(Cube_Mesh), MaterialID(Material1), 0, 0, 13)
ScaleEntity(Plank4, 4, 0.2, 2.5)
EntityPhysicBody(Plank4, #PB_Entity_StaticBody)

;
;Jointures
HingeJoint(#Joint1, EntityID(Plank0), 0, 0.1, 1.7, 1, 0, 0, EntityID(Plank1), 0, 0.1, -1.7, 1, 0, 0)
HingeJoint(#Joint2, EntityID(Plank1), 0, 0.1, 1.7, 1, 0, 0, EntityID(Plank2), 0, 0.1, -1.7, 1, 0, 0)
HingeJoint(#Joint3, EntityID(Plank2), 0, 0.1, 1.7, 1, 0, 0, EntityID(Plank3), 0, 0.1, -1.7, 1, 0, 0)
HingeJoint(#Joint4, EntityID(Plank3), 0, 0.1, 1.7, 1, 0, 0, EntityID(Plank4), 0, 0.1, -1.7, 1, 0, 0)

;
;Lumiere et ombre
AmbientColor(RGB(127, 127, 127))
CreateLight(#PB_Any,RGB(151, 251, 151), -1.8, 10, 5)
WorldShadows(#PB_Shadow_Additive)

;
; Une camera 
Camera = CreateCamera(#PB_Any,0,0,100,100)

While #True
  
  Event = WindowEvent()
    
  If ExamineKeyboard()    
    If Event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
      Break
    EndIf  
    
    If KeyboardPushed(#PB_Key_Space)
      FreeJoint(#PB_All)
    EndIf
    
  EndIf
    
  MoveCamera(Camera, 8, 8 , 25, #PB_Absolute)
  
  CameraLookAt(camera,0,0,0)
  
  ; Affiche le rendu de la scène
  ClearScreen(RGB(0, 0, 0))
  RenderWorld()
  FlipBuffers()
Wend

Re: [3D] Les jointures résistent !

Publié : mar. 16/juil./2013 7:51
par kelebrindae
Je n'ai pas encore essayé le code, mais je pense que ton problème est dû au fait que les bodies physiques de tes entités sont passés en "sommeil" (Pour optimiser les calculs, Bullet passe en sommeil tous les corps immobiles ou presque dès lors qu'ils ne subissent aucune influence extérieure).

Essaye de faire un "DisableEntityBody(plank[n],#False)" sur chacune de tes entités au moment où tu casses le joint, ça devrait arranger les choses... :wink:

Re: [3D] Les jointures résistent !

Publié : mar. 16/juil./2013 8:52
par Fred
Si c'est le cas, il faudra modifier FreeJoint() pour que ca le fasse automatiquement.

Re: [3D] Les jointures résistent !

Publié : mar. 16/juil./2013 11:07
par falsam
Bonjour kelebrindae et merci pour cette suggestion. Il faut effectivement insérer DisableEntityBody(Entity, #False) juste avant freejoint() pour que le ou les entités qui sont passée(s) en sommeil (Planches immobiles dans mon cas)
Fred a écrit :Si c'est le cas, il faudra modifier FreeJoint() pour que ca le fasse automatiquement.
Bonjour Fred. Apparement c'est le cas.

je joint le bout de code corrigé avec cette suggestion.
J'ai volontairement commenter cette suggestion pour la planche du milieu.
Attender que toutes les planches soient immobilisées et appuyer sur la touche Espace pour détruire tous les joints (FreeJoint(#PB_All)). La planche du milieu ne chutera pas.

Code : Tout sélectionner

Enumeration
  #Joint1
  #Joint2
  #Joint3
  #Joint4
EndEnumeration

InitEngine3D()
InitKeyboard()
InitSprite()

window = OpenWindow(#PB_Any,0,0,1024,768,"Les jointures font de la résistance")
OpenWindowedScreen(WindowID(window),0,0,1024,768)
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)


; Une forme
Cube_Mesh = CreateCube(#PB_Any, 1) 

; Materiel 
Texture1 = CreateTexture(#PB_Any,512,512)
StartDrawing(TextureOutput(Texture1))
Box(0,0,512,512,RGB(0, 0, 0))
Box(1,1,510,510,RGB(255, 216, 0))
StopDrawing()
Material1 = CreateMaterial(#PB_Any,TextureID(texture1))


Plank0 = CreateEntity(#PB_Any, MeshID(Cube_Mesh), MaterialID(Material1),  0, 0, 0)
ScaleEntity(Plank0, 4, 0.2, 2.5)
EntityPhysicBody(Plank0, #PB_Entity_StaticBody)

Plank1 = CreateEntity(#PB_Any, MeshID(Cube_Mesh), MaterialID(Material1), 0, 0, 8)
ScaleEntity(Plank1, 4, 0.2, 2.5)
EntityPhysicBody(Plank1, #PB_Entity_BoxBody, 0.1)

Plank2 = CreateEntity(#PB_Any, MeshID(Cube_Mesh), MaterialID(Material1), 0, 0, 8)
ScaleEntity(Plank2, 4, 0.2, 2.5)
EntityPhysicBody(Plank2, #PB_Entity_BoxBody, 0.1)

Plank3 = CreateEntity(#PB_Any, MeshID(Cube_Mesh), MaterialID(Material1), 0, 0, 8)
ScaleEntity(Plank3, 4, 0.2, 2.5)
EntityPhysicBody(Plank3, #PB_Entity_BoxBody, 0.1)

Plank4 = CreateEntity(#PB_Any, MeshID(Cube_Mesh), MaterialID(Material1), 0, 0, 13)
ScaleEntity(Plank4, 4, 0.2, 2.5)
EntityPhysicBody(Plank4, #PB_Entity_StaticBody)

;
;Jointures
HingeJoint(#Joint1, EntityID(Plank0), 0, 0.1, 1.7, 1, 0, 0, EntityID(Plank1), 0, 0.1, -1.7, 1, 0, 0)
HingeJoint(#Joint2, EntityID(Plank1), 0, 0.1, 1.7, 1, 0, 0, EntityID(Plank2), 0, 0.1, -1.7, 1, 0, 0)
HingeJoint(#Joint3, EntityID(Plank2), 0, 0.1, 1.7, 1, 0, 0, EntityID(Plank3), 0, 0.1, -1.7, 1, 0, 0)
HingeJoint(#Joint4, EntityID(Plank3), 0, 0.1, 1.7, 1, 0, 0, EntityID(Plank4), 0, 0.1, -1.7, 1, 0, 0)

;
;Lumiere et ombre
AmbientColor(RGB(127, 127, 127))
CreateLight(#PB_Any,RGB(151, 251, 151), -1.8, 10, 5)
WorldShadows(#PB_Shadow_Additive)

;
; Une camera 
Camera = CreateCamera(#PB_Any,0,0,100,100)

While #True
  
  Event = WindowEvent()
    
  If ExamineKeyboard()    
    If Event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
      Break
    EndIf  
    
    If KeyboardPushed(#PB_Key_Space)
      DisableEntityBody(plank1, #False)
      ;DisableEntityBody(plank2, #False)
      DisableEntityBody(plank3, #False)
      FreeJoint(#PB_All)
    EndIf
    
  EndIf
    
  MoveCamera(Camera, 8, 8 , 25, #PB_Absolute)
  
  CameraLookAt(camera,0,0,0)
  
  ; Affiche le rendu de la scène
  ClearScreen(RGB(0, 0, 0))
  RenderWorld()
  FlipBuffers()
Wend