1 | /******************************************************************************************* |
2 | * |
3 | * raylib [shapes] example - following eyes |
4 | * |
5 | * This example has been created using raylib 2.5 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2013-2019 Ramon Santamaria (@raysan5) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | #include <math.h> // Required for: atan2f() |
15 | |
16 | int main(void) |
17 | { |
18 | // Initialization |
19 | //-------------------------------------------------------------------------------------- |
20 | const int screenWidth = 800; |
21 | const int screenHeight = 450; |
22 | |
23 | InitWindow(screenWidth, screenHeight, "raylib [shapes] example - following eyes" ); |
24 | |
25 | Vector2 scleraLeftPosition = { GetScreenWidth()/2 - 100, GetScreenHeight()/2 }; |
26 | Vector2 scleraRightPosition = { GetScreenWidth()/2 + 100, GetScreenHeight()/2 }; |
27 | float scleraRadius = 80; |
28 | |
29 | Vector2 irisLeftPosition = { GetScreenWidth()/2 - 100, GetScreenHeight()/2 }; |
30 | Vector2 irisRightPosition = { GetScreenWidth()/2 + 100, GetScreenHeight()/2}; |
31 | float irisRadius = 24; |
32 | |
33 | float angle = 0.0f; |
34 | float dx = 0.0f, dy = 0.0f, dxx = 0.0f, dyy = 0.0f; |
35 | |
36 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
37 | //-------------------------------------------------------------------------------------- |
38 | |
39 | // Main game loop |
40 | while (!WindowShouldClose()) // Detect window close button or ESC key |
41 | { |
42 | // Update |
43 | //---------------------------------------------------------------------------------- |
44 | irisLeftPosition = GetMousePosition(); |
45 | irisRightPosition = GetMousePosition(); |
46 | |
47 | // Check not inside the left eye sclera |
48 | if (!CheckCollisionPointCircle(irisLeftPosition, scleraLeftPosition, scleraRadius - 20)) |
49 | { |
50 | dx = irisLeftPosition.x - scleraLeftPosition.x; |
51 | dy = irisLeftPosition.y - scleraLeftPosition.y; |
52 | |
53 | angle = atan2f(dy, dx); |
54 | |
55 | dxx = (scleraRadius - irisRadius)*cosf(angle); |
56 | dyy = (scleraRadius - irisRadius)*sinf(angle); |
57 | |
58 | irisLeftPosition.x = scleraLeftPosition.x + dxx; |
59 | irisLeftPosition.y = scleraLeftPosition.y + dyy; |
60 | } |
61 | |
62 | // Check not inside the right eye sclera |
63 | if (!CheckCollisionPointCircle(irisRightPosition, scleraRightPosition, scleraRadius - 20)) |
64 | { |
65 | dx = irisRightPosition.x - scleraRightPosition.x; |
66 | dy = irisRightPosition.y - scleraRightPosition.y; |
67 | |
68 | angle = atan2f(dy, dx); |
69 | |
70 | dxx = (scleraRadius - irisRadius)*cosf(angle); |
71 | dyy = (scleraRadius - irisRadius)*sinf(angle); |
72 | |
73 | irisRightPosition.x = scleraRightPosition.x + dxx; |
74 | irisRightPosition.y = scleraRightPosition.y + dyy; |
75 | } |
76 | //---------------------------------------------------------------------------------- |
77 | |
78 | // Draw |
79 | //---------------------------------------------------------------------------------- |
80 | BeginDrawing(); |
81 | |
82 | ClearBackground(RAYWHITE); |
83 | |
84 | DrawCircleV(scleraLeftPosition, scleraRadius, LIGHTGRAY); |
85 | DrawCircleV(irisLeftPosition, irisRadius, BROWN); |
86 | DrawCircleV(irisLeftPosition, 10, BLACK); |
87 | |
88 | DrawCircleV(scleraRightPosition, scleraRadius, LIGHTGRAY); |
89 | DrawCircleV(irisRightPosition, irisRadius, DARKGREEN); |
90 | DrawCircleV(irisRightPosition, 10, BLACK); |
91 | |
92 | DrawFPS(10, 10); |
93 | |
94 | EndDrawing(); |
95 | //---------------------------------------------------------------------------------- |
96 | } |
97 | |
98 | // De-Initialization |
99 | //-------------------------------------------------------------------------------------- |
100 | CloseWindow(); // Close window and OpenGL context |
101 | //-------------------------------------------------------------------------------------- |
102 | |
103 | return 0; |
104 | } |