raylib/ports/shapes/shapes_following_eyes
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.
Code
shapes_following_eyes.odin ¶106 linesSource
1/*******************************************************************************************
2*
3* raylib [shapes] example - following eyes
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
20import "core:math" // Required for: math.atan2()
21
22//------------------------------------------------------------------------------------
23// Program main entry point
24//------------------------------------------------------------------------------------
25main :: proc() {
26 // Initialization
27 //--------------------------------------------------------------------------------------
28 SCREEN_WIDTH :: 800
29 SCREEN_HEIGHT :: 450
30
31 rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [shapes] example - following eyes")
32
33 sclera_left_position := rl.Vector2 {f32(rl.GetScreenWidth())/2 - 100, f32(rl.GetScreenHeight())/2}
34 sclera_right_position := rl.Vector2 {f32(rl.GetScreenWidth())/2 + 100, f32(rl.GetScreenHeight())/2}
35 sclera_radius: f32 = 80
36
37 iris_left_position := rl.Vector2 {f32(rl.GetScreenWidth())/2 - 100, f32(rl.GetScreenHeight())/2}
38 iris_right_position := rl.Vector2 {f32(rl.GetScreenWidth())/2 + 100, f32(rl.GetScreenHeight())/2}
39 iris_radius: f32 = 24
40
41 angle, dx, dy, dxx, dyy: f32 // Initialized to zero.
42
43 rl.SetTargetFPS(60) // Set our game to run at 60 frames-per-second
44 //--------------------------------------------------------------------------------------
45
46 // Main game loop
47 for !rl.WindowShouldClose() { // Detect window close button or ESC key
48 // Update
49 //----------------------------------------------------------------------------------
50 iris_left_position = rl.GetMousePosition()
51 iris_right_position = rl.GetMousePosition()
52
53 // Check not inside the left eye sclera
54 if !rl.CheckCollisionPointCircle(iris_left_position, sclera_left_position, sclera_radius - iris_radius) {
55 dx = iris_left_position.x - sclera_left_position.x
56 dy = iris_left_position.y - sclera_left_position.y
57
58 angle = math.atan2(dy, dx)
59
60 dxx = (sclera_radius - iris_radius)*math.cos(angle)
61 dyy = (sclera_radius - iris_radius)*math.sin(angle)
62
63 iris_left_position.x = sclera_left_position.x + dxx
64 iris_left_position.y = sclera_left_position.y + dyy
65 }
66
67 // Check not inside the right eye sclera
68 if !rl.CheckCollisionPointCircle(iris_right_position, sclera_right_position, sclera_radius - iris_radius) {
69 dx = iris_right_position.x - sclera_right_position.x
70 dy = iris_right_position.y - sclera_right_position.y
71
72 angle = math.atan2(dy, dx)
73
74 dxx = (sclera_radius - iris_radius)*math.cos(angle)
75 dyy = (sclera_radius - iris_radius)*math.sin(angle)
76
77 iris_right_position.x = sclera_right_position.x + dxx
78 iris_right_position.y = sclera_right_position.y + dyy
79 }
80 //----------------------------------------------------------------------------------
81
82 // Draw
83 //----------------------------------------------------------------------------------
84 rl.BeginDrawing()
85
86 rl.ClearBackground(rl.RAYWHITE)
87
88 rl.DrawCircleV(sclera_left_position, sclera_radius, rl.LIGHTGRAY)
89 rl.DrawCircleV(iris_left_position, iris_radius, rl.BROWN)
90 rl.DrawCircleV(iris_left_position, 10, rl.BLACK)
91
92 rl.DrawCircleV(sclera_right_position, sclera_radius, rl.LIGHTGRAY)
93 rl.DrawCircleV(iris_right_position, iris_radius, rl.DARKGREEN)
94 rl.DrawCircleV(iris_right_position, 10, rl.BLACK)
95
96 rl.DrawFPS(10, 10)
97
98 rl.EndDrawing()
99 //----------------------------------------------------------------------------------
100 }
101
102 // De-Initialization
103 //--------------------------------------------------------------------------------------
104 rl.CloseWindow() // Close window and OpenGL context
105 //--------------------------------------------------------------------------------------
106}
Declarations Used 22
- core:math atan2 · cos · sin
- vendor:raylib BeginDrawing · BLACK · BROWN · CheckCollisionPointCircle · ClearBackground · CloseWindow · DARKGREEN · DrawCircleV · DrawFPS · EndDrawing · GetMousePosition · GetScreenHeight · GetScreenWidth · InitWindow · LIGHTGRAY · RAYWHITE · SetTargetFPS · Vector2 · WindowShouldClose