Skip to content

Commit

Permalink
Add shapes following eyes example
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonsilva committed Oct 23, 2023
1 parent 3ea6740 commit 396da30
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Examples using raylib shapes drawing functionality.
| 32 | [shapes_bouncing_ball](shapes/shapes_bouncing_ball.rb) | <img src="shapes/shapes_bouncing_ball.png" alt="shapes_bouncing_ball" width="80"> | ⭐️☆☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| 33 | [shapes_colors_palette](shapes/shapes_colors_palette.rb) | <img src="shapes/shapes_colors_palette.png" alt="shapes_colors_palette" width="80"> | ⭐️⭐️☆☆ | 1.0 | 2.5 | [Ray](https://github.com/raysan5) |
| 38 | [shapes_collision_area](shapes/shapes_collision_area.rb) | <img src="shapes/shapes_collision_area.png" alt="shapes_collision_area" width="80"> | ⭐️⭐️☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| 39 | [shapes_following_eyes](shapes/shapes_following_eyes.rb) | <img src="shapes/shapes_following_eyes.png" alt="shapes_following_eyes" width="80"> | ⭐️⭐️☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| 40 | [shapes_easings_ball_anim](shapes/shapes_easings_ball_anim.rb) | <img src="shapes/shapes_easings_ball_anim.png" alt="shapes_easings_ball_anim" width="80"> | ⭐️⭐️☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| 41 | [shapes_easings_box_anim](shapes/shapes_easings_box_anim.rb) | <img src="shapes/shapes_easings_box_anim.png" alt="shapes_easings_box_anim" width="80"> | ⭐️⭐️☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| 42 | [shapes_easings_rectangle_array](shapes/shapes_easings_rectangle_array.rb) | <img src="shapes/shapes_easings_rectangle_array.png" alt="shapes_easings_rectangle_array" width="80"> | ⭐️⭐️⭐️☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
Binary file added examples/shapes/shapes_following_eyes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions examples/shapes/shapes_following_eyes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# ******************************************************************************************
#
# raylib [shapes] example - following eyes
#
# Example originally created with raylib 2.5, last time updated with raylib 2.5
#
# Example ported to Ruby by Wilson Silva (@wilsonsilva). Works with Raylib 4.5
#
# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
# BSD-like license that allows static linking with closed source software
#
# Copyright (c) 2013-2023 Ramon Santamaria (@raysan5)
#
# ******************************************************************************************

require 'bundler/setup'
require 'raylib'

# Initialization
# --------------------------------------------------------------------------------------
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 450

Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'raylib [shapes] example - following eyes')

sclera_left_position = Raylib::Vector2.create(Raylib.get_screen_width/2.0 - 100.0, Raylib.get_screen_height/2.0)
sclera_right_position = Raylib::Vector2.create(Raylib.get_screen_width/2.0 + 100.0, Raylib.get_screen_height/2.0)
sclera_radius = 80

iris_radius = 24

Raylib.set_target_fps(60)
# --------------------------------------------------------------------------------------

# Main game loop
until Raylib.window_should_close # Detect window close button or ESC key
# Update
# ----------------------------------------------------------------------------------
iris_left_position = Raylib.get_mouse_position
iris_right_position = Raylib.get_mouse_position

# Check not inside the left eye sclera
unless Raylib.check_collision_point_circle(iris_left_position, sclera_left_position, sclera_radius - 20)
dx = iris_left_position.x - sclera_left_position.x
dy = iris_left_position.y - sclera_left_position.y

angle = Math.atan2(dy, dx)

dxx = (sclera_radius - iris_radius)*Math.cos(angle)
dyy = (sclera_radius - iris_radius)*Math.sin(angle)

iris_left_position.x = sclera_left_position.x + dxx
iris_left_position.y = sclera_left_position.y + dyy
end

# Check not inside the right eye sclera
unless Raylib.check_collision_point_circle(iris_right_position, sclera_right_position, sclera_radius - 20)
dx = iris_right_position.x - sclera_right_position.x
dy = iris_right_position.y - sclera_right_position.y

angle = Math.atan2(dy, dx)

dxx = (sclera_radius - iris_radius)*Math.cos(angle)
dyy = (sclera_radius - iris_radius)*Math.sin(angle)

iris_right_position.x = sclera_right_position.x + dxx
iris_right_position.y = sclera_right_position.y + dyy
end
# ----------------------------------------------------------------------------------

# Draw
# ----------------------------------------------------------------------------------
Raylib.begin_drawing
Raylib.clear_background(Raylib::RAYWHITE)

Raylib.draw_circle_v(sclera_left_position, sclera_radius, Raylib::LIGHTGRAY)
Raylib.draw_circle_v(iris_left_position, iris_radius, Raylib::BROWN)
Raylib.draw_circle_v(iris_left_position, 10, Raylib::BLACK)

Raylib.draw_circle_v(sclera_right_position, sclera_radius, Raylib::LIGHTGRAY)
Raylib.draw_circle_v(iris_right_position, iris_radius, Raylib::DARKGREEN)
Raylib.draw_circle_v(iris_right_position, 10, Raylib::BLACK)

Raylib.draw_fps(10, 10)
Raylib.end_drawing
# ----------------------------------------------------------------------------------
end

# De-Initialization
# --------------------------------------------------------------------------------------
Raylib.close_window # Close window and OpenGL context
# --------------------------------------------------------------------------------------

0 comments on commit 396da30

Please sign in to comment.