raylib/ports/shapes/shapes_collision_area

Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license. Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
A screenshot of shapes_collision_area

Code

shapes_collision_area.odin ¶
128 linesSource

1/*******************************************************************************************
2*
3*   raylib [shapes] example - collision area
4*
5*   Example complexity rating: [★★☆☆] 2/4
6*
7*   Example originally created with raylib 2.5, last time updated with raylib 2.5
8*
9*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
10*   BSD-like license that allows static linking with closed source software
11*
12*   Copyright (c) 2013-2025 Ramon Santamaria (@raysan5)
13*
14********************************************************************************************/
15
16package raylib_examples
17
18import rl "vendor:raylib"
19
20//------------------------------------------------------------------------------------
21// Program main entry point
22//------------------------------------------------------------------------------------
23main :: proc() {
24	// Initialization
25	//---------------------------------------------------------
26	SCREEN_WIDTH :: 800
27	SCREEN_HEIGHT :: 450
28
29	rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [shapes] example - collision area")
30
31	// Box A: Moving box
32	box_a := rl.Rectangle {10, f32(rl.GetScreenHeight())/2 - 50, 200, 100}
33	box_a_speed_x: int = 4
34
35	// Box B: Mouse moved box
36	box_b := rl.Rectangle {f32(rl.GetScreenWidth())/2 - 30, f32(rl.GetScreenHeight())/2 - 30, 60, 60}
37
38	box_collision: rl.Rectangle // Collision rectangle
39
40	screen_upper_limit: i32 = 40      // Top menu limits
41
42	pause: bool             // Movement pause
43	collision: bool         // Collision detection
44
45	rl.SetTargetFPS(60)               // Set our game to run at 60 frames-per-second
46	//----------------------------------------------------------
47
48	// Main game loop
49	for !rl.WindowShouldClose() {    // Detect window close button or ESC key
50		// Update
51		//-----------------------------------------------------
52		// Move box if not paused
53		if !pause {
54			box_a.x += f32(box_a_speed_x)
55		}
56
57		// Bounce box on x screen limits
58		if (box_a.x + box_a.width) >= f32(rl.GetScreenWidth()) || box_a.x <= 0 {
59			box_a_speed_x *= -1
60		}
61
62		// Update player-controlled-box (box02)
63		box_b.x = f32(rl.GetMouseX()) - box_b.width/2
64		box_b.y = f32(rl.GetMouseY()) - box_b.height/2
65
66		// Make sure Box B does not go out of move area limits
67		if box_b.x + box_b.width >= f32(rl.GetScreenWidth()) {
68			box_b.x = f32(rl.GetScreenWidth()) - box_b.width
69		} else if box_b.x <= 0 {
70			box_b.x = 0
71		}
72
73		if box_b.y + box_b.height >= f32(rl.GetScreenHeight()) {
74			box_b.y = f32(rl.GetScreenHeight()) - box_b.height
75		} else if box_b.y <= f32(screen_upper_limit) {
76			box_b.y = f32(screen_upper_limit)
77		}
78
79		// Check boxes collision
80		collision = rl.CheckCollisionRecs(box_a, box_b)
81
82		// Get collision rectangle (only on collision)
83		if collision {
84			box_collision = rl.GetCollisionRec(box_a, box_b)
85		}
86
87		// Pause Box A movement
88		if rl.IsKeyPressed(.SPACE) {
89			pause = !pause
90		}
91		//-----------------------------------------------------
92
93		// Draw
94		//-----------------------------------------------------
95		rl.BeginDrawing()
96
97			rl.ClearBackground(rl.RAYWHITE)
98
99			rl.DrawRectangle(0, 0, SCREEN_WIDTH, screen_upper_limit, collision? rl.RED : rl.BLACK)
100
101			rl.DrawRectangleRec(box_a, rl.GOLD)
102			rl.DrawRectangleRec(box_b, rl.BLUE)
103
104			if collision {
105				// Draw collision area
106				rl.DrawRectangleRec(box_collision, rl.LIME)
107
108				// Draw collision message
109				rl.DrawText("COLLISION!", rl.GetScreenWidth()/2 - rl.MeasureText("COLLISION!", 20)/2, screen_upper_limit/2 - 10, 20, rl.BLACK)
110
111				// Draw collision area
112				rl.DrawText(rl.TextFormat("Collision Area: %i", i32(box_collision.width*box_collision.height)), rl.GetScreenWidth()/2 - 100, screen_upper_limit + 10, 20, rl.BLACK)
113			}
114
115			// Draw help instructions
116			rl.DrawText("Press SPACE to PAUSE/RESUME", 20, SCREEN_HEIGHT - 35, 20, rl.LIGHTGRAY)
117
118			rl.DrawFPS(10, 10)
119
120		rl.EndDrawing()
121		//-----------------------------------------------------
122	}
123
124	// De-Initialization
125	//---------------------------------------------------------
126	rl.CloseWindow()        // Close window and OpenGL context
127	//----------------------------------------------------------
128}

Declarations Used 28