Need some help with RayCast().
Posted: Mon Dec 09, 2013 9:44 pm
I created a little example with a sphere that casts a ray below it. If I understood everything correctly the RayCast() command should return the handle
of the entity that the ray crosses. At least that's how it seemed to work in the examples, but I keep getting a value of -1.
According to the help -1 means that the ray didn't come in contact with any entity.
It would be great if someone could tell me what I'm doing wrong.
of the entity that the ray crosses. At least that's how it seemed to work in the examples, but I keep getting a value of -1.
According to the help -1 means that the ray didn't come in contact with any entity.
It would be great if someone could tell me what I'm doing wrong.
Code: Select all
;Press escape to exit.
Enumeration
#Window
#Camera
#Light
#TextureRed
#TextureGreen
#TextureBlue
#TextureYellow
#TexturePink
#MaterialRed
#MaterialGreen
#MaterialBlue
#MaterialYellow
#MaterialPink
#SphereMesh
#CubeMesh
#Sphere
#Cube1
#Cube2
#Cube3
#Cube4
EndEnumeration
#CameraSpeed =10
Define.f KeyX, KeyY, MouseX, MouseY
If InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
If OpenWindow(#Window, 0, 0, DesktopW, DesktopH, "RayCast Test")
If OpenWindowedScreen(WindowID(#Window), 0, 0, DesktopW, DesktopH, 0, 0, 0)
CreateLight(#Light ,RGB(255, 255, 150),20000,3000, 1000,#PB_Light_Directional)
SetLightColor(#Light, #PB_Light_SpecularColor, RGB(255, 255,255))
LightDirection(#Light, 0.55, -0.3, -0.75)
AmbientColor(RGB(90, 90, 90))
CreateTexture(#TextureRed,32,32)
StartDrawing(TextureOutput(#TextureRed))
Box(0,0,32,32,RGB(255, 0, 0))
StopDrawing()
CreateTexture(#TextureGreen,32,32)
StartDrawing(TextureOutput(#TextureGreen))
Box(0,0,32,32,RGB(0, 255, 0))
StopDrawing()
CreateTexture(#TextureBlue,32,32)
StartDrawing(TextureOutput(#TextureBlue))
Box(0,0,32,32,RGB(0, 0, 255))
StopDrawing()
CreateTexture(#TextureYellow,32,32)
StartDrawing(TextureOutput(#TextureYellow))
Box(0,0,32,32,RGB(255, 255, 0))
StopDrawing()
CreateTexture(#TexturePink,32,32)
StartDrawing(TextureOutput(#TexturePink))
Box(0,0,32,32,RGB(255, 155, 255))
StopDrawing()
CreateMaterial(#MaterialRed,TextureID(#TextureRed))
CreateMaterial(#MaterialGreen,TextureID(#TextureGreen))
CreateMaterial(#MaterialBlue,TextureID(#TextureBlue))
CreateMaterial(#MaterialYellow,TextureID(#TextureYellow))
CreateMaterial(#MaterialPink,TextureID(#TexturePink))
CreateSphere(#SphereMesh, 10)
CreateCube(#CubeMesh,800)
CreateEntity(#Sphere, MeshID(#SphereMesh), MaterialID(#MaterialYellow),0,500,0,0)
CreateEntity(#Cube1,MeshID(#CubeMesh),MaterialID(#MaterialRed),430,40,430,1)
CreateEntity(#Cube2,MeshID(#CubeMesh),MaterialID(#MaterialGreen),-430,40,430,1)
CreateEntity(#Cube3,MeshID(#CubeMesh),MaterialID(#MaterialBlue),430,40,-430,1)
CreateEntity(#Cube4,MeshID(#CubeMesh),MaterialID(#MaterialPink),-430,40,-430,1)
CreateCamera(#Camera, 0, 0, 100, 100)
CameraBackColor(#Camera,RGB(30,30,30))
MoveCamera(#Camera, 0, 0, 0, #PB_Absolute)
EndIf
EndIf
Repeat
Event=WaitWindowEvent()
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
If KeyboardReleased(#PB_Key_W)
CameraRenderMode(#Camera, #PB_Camera_Wireframe)
ElseIf KeyboardReleased(#PB_Key_E)
CameraRenderMode(#Camera, #PB_Camera_Textured)
EndIf
EndIf
If ExamineMouse()
MouseX = -(MouseDeltaX()/10)
MouseY = -(MouseDeltaY()/10)
MoveEntity(#Sphere, KeyX, 0, KeyY, #PB_Local)
Result = RayCast(EntityX(#Sphere), EntityY(#Sphere), EntityZ(#Sphere), EntityX(#Sphere), EntityY(#Sphere)-90, EntityZ(#Sphere),1)
CreateLine3D(10, EntityX(#Sphere), EntityY(#Sphere), EntityZ(#Sphere), RGB(255,255,255), EntityX(#Sphere), EntityY(#Sphere)-90, EntityZ(#Sphere),RGB(255,255,255))
Debug Result
If Result=#Cube1
Debug "The red cube is crossing the ray."
ElseIf Result=#Cube2
Debug "The green cube is crossing the ray."
ElseIf Result=#Cube3
Debug "The blue cube is crossing the ray."
ElseIf Result=#Cube4
Debug "The pink cube is crossing the ray."
EndIf
Yaw(EntityID(#Sphere), MouseX, #PB_World)
CameraFollow(#Camera, EntityID(#Sphere), 0, EntityY(#Sphere)+20, 300, 0.5, 0.5, #False)
CameraLookAt(#Camera, EntityX(#Sphere), EntityY(#Sphere), EntityZ(#Sphere))
EndIf
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
EndIf
End