Skip to content

Commit ba8f74a

Browse files
committed
Added core_3d_picking.py example.
1 parent b3dd8e4 commit ba8f74a

File tree

3 files changed

+349
-249
lines changed

3 files changed

+349
-249
lines changed

examples/core/core_3d_picking.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# core_3d_picking.py
2+
3+
from raylibpy import *
4+
5+
def main() -> int:
6+
7+
# Initialization
8+
# -------------------------------------------------------------------------------------
9+
screen_width: int = 800
10+
screen_height: int = 450
11+
12+
init_window(screen_width, screen_height, b"raylib [core] example - 3d picking")
13+
14+
#
15+
camera: Camera = Camera()
16+
camera.position = Vector3(10., 10., 10.)
17+
camera.target = Vector3(0., 0., 0.)
18+
camera.up = Vector3(0., 1., 0.)
19+
camera.fovy = 45.0
20+
camera.type = CAMERA_PERSPECTIVE
21+
22+
cube_position: Vector3 = Vector3(0., 1., 0.)
23+
cube_size: Vector3 = Vector3(2., 2., 2.)
24+
25+
ray: Ray = Ray(Vector3(0., 0., 0.))
26+
27+
collision: bool = False
28+
29+
set_camera_mode(camera, CAMERA_FREE)
30+
31+
set_target_fps(60)
32+
# --------------------------------------------------------------------------------------
33+
34+
# Main game loop
35+
while not window_should_close():
36+
# Update
37+
# ---------------------------------------------------------------------------------
38+
update_camera(byref(camera))
39+
40+
if (is_mouse_button_pressed(MOUSE_LEFT_BUTTON)):
41+
ray = get_mouse_ray(get_mouse_position(), camera)
42+
43+
#
44+
collision = check_collision_ray_box(
45+
ray,
46+
BoundingBox(
47+
Vector3(
48+
cube_position.x - cube_size.x / 2,
49+
cube_position.y - cube_size.y / 2,
50+
cube_position.z - cube_size.z / 2,
51+
),
52+
Vector3(
53+
cube_position.x + cube_size.x / 2,
54+
cube_position.y + cube_size.y / 2,
55+
cube_position.z + cube_size.z / 2,
56+
)
57+
)
58+
)
59+
# ----------------------------------------------------------------------------------
60+
61+
# Draw
62+
# ---------------------------------------------------------------------------------
63+
begin_drawing()
64+
65+
clear_background(RAYWHITE)
66+
67+
begin_mode3d(camera)
68+
69+
if collision:
70+
draw_cube(cube_position, cube_size.x, cube_size.y, cube_size.z, RED)
71+
draw_cube_wires(cube_position, cube_size.x, cube_size.y, cube_size.z, MAROON)
72+
73+
draw_cube_wires(cube_position, cube_size.x + .2, cube_size.y + .2, cube_size.z + .2, GREEN)
74+
else:
75+
draw_cube(cube_position, cube_size.x, cube_size.y, cube_size.z, GRAY)
76+
draw_cube_wires(cube_position, cube_size.x, cube_size.y, cube_size.z, DARKGRAY)
77+
78+
draw_ray(ray, MAROON)
79+
draw_grid(10, 1.0)
80+
81+
end_mode3d()
82+
83+
draw_text(b"Try Selecting the box with mouse!", 240, 10, 20, DARKGRAY)
84+
85+
if collision:
86+
draw_text(b"BOX SELECTED", (screen_width - measure_text(b"BOX SELECTED", 30)) // 2, int(screen_height * .1), 30, GREEN)
87+
88+
end_drawing()
89+
# ---------------------------------------------------------------------------------
90+
91+
# De-Initialization
92+
# -------------------------------------------------------------------------------------
93+
close_window()
94+
# -------------------------------------------------------------------------------------
95+
96+
return 0
97+
98+
99+
if __name__ == '__main__':
100+
main()

0 commit comments

Comments
 (0)