core_world_screen.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [core] example - World to screen
  4. --
  5. -- This example has been created using raylib 1.6 (www.raylib.com)
  6. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. --
  8. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  9. --
  10. -------------------------------------------------------------------------------------------
  11. -- Initialization
  12. -------------------------------------------------------------------------------------------
  13. local screenWidth = 800
  14. local screenHeight = 450
  15. InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free")
  16. -- Define the camera to look into our 3d world
  17. local camera = Camera(Vector3(0.0, 10.0, 10.0), Vector3(0.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0), 45.0)
  18. local cubePosition = Vector3(0.0, 0.0, 0.0)
  19. local cubeScreenPosition = Vector2(0, 0)
  20. SetCameraMode(camera, CameraMode.FREE) -- Set a free camera mode
  21. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  22. ----------------------------------------------------------------------------------------
  23. -- Main game loop
  24. while not WindowShouldClose() do -- Detect window close button or ESC key
  25. -- Update
  26. ------------------------------------------------------------------------------------
  27. camera = UpdateCamera(camera) -- Update camera
  28. -- Calculate cube screen space position (with a little offset to be in top)
  29. cubeScreenPosition = GetWorldToScreen(Vector3(cubePosition.x, cubePosition.y + 2.5, cubePosition.z), camera)
  30. ------------------------------------------------------------------------------------
  31. -- Draw
  32. ------------------------------------------------------------------------------------
  33. BeginDrawing()
  34. ClearBackground(RAYWHITE)
  35. Begin3dMode(camera)
  36. DrawCube(cubePosition, 2.0, 2.0, 2.0, RED)
  37. DrawCubeWires(cubePosition, 2.0, 2.0, 2.0, MAROON)
  38. DrawGrid(10, 1.0)
  39. End3dMode()
  40. DrawText("Enemy: 100 / 100", cubeScreenPosition.x//1 - MeasureText("Enemy: 100 / 100", 20)//2, cubeScreenPosition.y//1, 20, BLACK)
  41. DrawText("Text is always on top of the cube", (screenWidth - MeasureText("Text is always on top of the cube", 20))//2, 25, 20, GRAY)
  42. EndDrawing()
  43. ------------------------------------------------------------------------------------
  44. end
  45. -- De-Initialization
  46. ----------------------------------------------------------------------------------------
  47. CloseWindow() -- Close window and OpenGL context
  48. ----------------------------------------------------------------------------------------