Page 1 sur 1

[Résolu] RayPick : Détection d'une entité

Publié : sam. 18/nov./2017 13:43
par falsam
La fonction 3D Raypick lance un rayon entre deux points et vérifie si un objet traverse la trajectoire du rayon.

C'est ce que j'ai tenté de faire avec ce code.

- Un cube fait une chute libre.
- Durant la chute il est impossible de le faire bouger de gauche à droite tant que le rayon ne détecte pas d'entité.
- Un tracé rouge montre le segment du rayon.

:!: Souci
- Le rayon détecte le cube (entité 5) alors qu'il se trouve sous le cube.
- La plateforme est détectée quand on ne voit plus le rayon.

Code : Tout sélectionner

EnableExplicit

Define.i Event, Target
Define.f Speed, x1, y1, z1, x2, y2, z2 

InitEngine3D() : InitKeyboard() : InitSprite()

OpenWindow(0,0,0,1024,768,"RayPick vertical")
OpenWindowedScreen(WindowID(0),0,0,1024,768)

Add3DArchive(#PB_Compiler_Home + "Examples\3D\Data\Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/waterworld.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)

SkyBox("desert07.jpg")

; Caméra, Lumiere & Ombre
CreateCamera(0, 0, 0, 100, 100)
CameraBackColor(0, RGB(0, 0, 0))

; Light
CreateLight(#PB_Any, RGB(151, 251, 151), -200, 100, -500)
WorldShadows(#PB_Shadow_Additive)

; Material
CreateMaterial(0, TextureID(LoadTexture(-1, "Dirt.jpg")))
CreateMaterial(1, TextureID(LoadTexture(-1, "Floor02.jpg")))
ScaleMaterial(0, 0.5, 0.5)

; Platform
CreateCube(0, 10)

CreateEntity(1, MeshID(0), MaterialID(0), 0, 10, 0)
CreateEntityBody(1, #PB_Entity_StaticBody)

;Player 
CreateEntity(5, MeshID(CreateCube(-1, 1)), MaterialID(1), 0, 20, 0)
CreateEntityBody(5, #PB_Entity_BoxBody, 1, 0.5, 0.5) 
EntityAngularFactor(5, 0, 90, 0)

;Debug
;WorldDebug(#PB_World_DebugEntity | #PB_World_DebugBody)

;loop
While #True  
  Repeat
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until Event = 0    
  
  If ExamineKeyboard()  
    If KeyboardReleased(#PB_Key_Escape)
      Break
    EndIf    
    If KeyboardPushed(#PB_Key_Right)
      Speed = 2
    ElseIf KeyboardPushed(#PB_Key_Left)
      Speed = -2
    Else
      Speed = 0
    EndIf    
  EndIf
  
  CameraFollow(0, EntityID(5), 0, EntityY(5) + 1.5, 10, 0.1, 0.1)
  
  ;Raypick : Casts a ray between the first point and the second point, and checks if an object is crossing the ray.  
  x1 = EntityX(5)
  y1 = EntityY(5) - 0.4
  z1 = EntityZ(5)
  x2 = x1
  y2 = y1 - 0.5
  z2 = z1
  
  Target = RayCollide(x1, y1, z1, x2, y2, z2)
  CreateLine3D(5, x1, y1, z1, RGB(255,0,0), x2, y2, z2, RGB(255,0,0))
  
  If Target <> -1
    Debug "Entity " + Target + " is crossing the ray"
    MoveEntity(5, Speed, 0, 0, #PB_Absolute|#PB_Local)
  Else
    ;Debug "Nothing has been crossed."
  EndIf     
  
  RenderWorld()
  FlipBuffers()
Wend
Merci de votre aide.

Re: RayPick : Détection d'une entité

Publié : sam. 18/nov./2017 17:47
par G-Rom
RayCollide() a la place de RayPick() ;)

Re: RayPick : Détection d'une entité

Publié : sam. 18/nov./2017 18:07
par falsam
Maître G-Rom est passé sur le chat de PureBasic et m'a expliqué que la fonction RayPick() ne fonctionne pas avec des entités physiques. Il me semble pas que ce soit indiqué dans la documentation.

Le code du premier message est modifié.

Merci G-Rom.