From ad374079291e332c8c877f7f81e8a66989ed2a53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CTimfon=E2=80=9D?= <“timothy_fong@brown.edu”> Date: Tue, 25 Jul 2023 21:38:27 +0700 Subject: [PATCH 1/2] added core example smooth pixel perfect camera --- .../core/core_smooth pixel-perfect camera.py | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 examples/core/core_smooth pixel-perfect camera.py diff --git a/examples/core/core_smooth pixel-perfect camera.py b/examples/core/core_smooth pixel-perfect camera.py new file mode 100644 index 0000000..aec4e91 --- /dev/null +++ b/examples/core/core_smooth pixel-perfect camera.py @@ -0,0 +1,90 @@ +from pyray import * +import math +#Initialization + +screenWidth = 800 +screenHeight = 450 + +virtualScreenWidth = 160 +virtualScreenHeight = 90 + +virtualRatio = screenWidth/ virtualScreenWidth + +init_window(screenWidth, screenHeight, "raylib [core] example - smooth pixel-perfect camera") + +worldSpaceCamera = Camera2D([0]) # Game world camera +worldSpaceCamera.zoom = 1.0 + +screenSpaceCamera = Camera2D([0]) # Smoothing camera +screenSpaceCamera.zoom = 1.0 + +target = load_render_texture(virtualScreenWidth, virtualScreenHeight); # This is where we'll draw all our objects. + +rec01 = Rectangle(70.0, 35.0, 20.0, 20.0) +rec02 = Rectangle(90.0, 55.0, 30.0, 10.0) +rec03 = Rectangle(80.0, 65.0, 15.0, 25.0) + +# The target's height is flipped (in the source Rectangle), due to OpenGL reasons +sourceRec = Rectangle(0.0, 0.0, target.texture.width, -target.texture.height) +destRec = Rectangle(-virtualRatio, -virtualRatio, screenWidth + (virtualRatio*2), screenHeight + (virtualRatio*2)) + +origin = Vector2(0.0, 0.0) + +rotation = 0.0 + +cameraX = 0.0 +cameraY = 0.0 + +set_target_fps(60) + +# Main game loop +while not window_should_close(): # Detect window close button or ESC key + + # Update + + rotation += 60.0 *get_frame_time(); # Rotate the rectangles, 60 degrees per second + + # Make the camera move to demonstrate the effect + cameraX = (math.sin(get_time())*50.0) - 10.0 + cameraY = math.cos(get_time())*30.0 + + # Set the camera's target to the values computed above + screenSpaceCamera.target = Vector2(cameraX, cameraY) + + # Round worldSpace coordinates, keep decimals into screenSpace coordinates + worldSpaceCamera.target.x = screenSpaceCamera.target.x + screenSpaceCamera.target.x -= worldSpaceCamera.target.x + screenSpaceCamera.target.x *= virtualRatio + + worldSpaceCamera.target.y = screenSpaceCamera.target.y + screenSpaceCamera.target.y -= worldSpaceCamera.target.y + screenSpaceCamera.target.y *= virtualRatio + + begin_texture_mode(target) + clear_background(RAYWHITE) + + begin_mode_2d(worldSpaceCamera) + draw_rectangle_pro(rec01, origin, rotation, BLACK) + draw_rectangle_pro(rec02, origin, -rotation, RED) + draw_rectangle_pro(rec03, origin, rotation + 45.0, BLUE) + end_mode_2d() + end_texture_mode() + + begin_drawing() + clear_background(RED) + + begin_mode_2d(screenSpaceCamera) + draw_texture_pro(target.texture, sourceRec, destRec, origin, 0.0, WHITE) + end_mode_2d() + + draw_text(text_format("Screen resolution: %ix%i", screenWidth, screenHeight), 10, 10, 20, DARKBLUE) + draw_text(text_format("World resolution: %ix%i", virtualScreenWidth, virtualScreenHeight), 10, 40, 20, DARKGREEN) + draw_fps(get_screen_width() - 95, 10) + end_drawing() + + +# De-Initialization + +unload_render_texture(target) # Unload render texture + +close_window(); # Close window and OpenGL context \ No newline at end of file From 4893cfb9057ddfac95334e6fee16f53a8092ecb9 Mon Sep 17 00:00:00 2001 From: Timothy Fong Date: Wed, 26 Jul 2023 15:17:30 -0400 Subject: [PATCH 2/2] added core 3d camera free (#108) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * added core_split_screen example * added core_vr_simulator example * Delete launch.json * added core 3d camera free * added core_smooth pixel-perfect camera --------- Co-authored-by: “Timfon” <“timothy_fong@brown.edu”> Co-authored-by: Richard Smith --- examples/core/core_3d_camera_free.py | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 examples/core/core_3d_camera_free.py diff --git a/examples/core/core_3d_camera_free.py b/examples/core/core_3d_camera_free.py new file mode 100644 index 0000000..f549d72 --- /dev/null +++ b/examples/core/core_3d_camera_free.py @@ -0,0 +1,60 @@ +from raylib import Fade +import pyray +from pyray import * + +# Initialization +screenWidth = 800 +screenHeight = 450 + +init_window(screenWidth, screenHeight, "raylib [core] example - 3d camera free") + +# Define the camera to look into our 3d world +camera = Camera3D() +camera.position = Vector3(10.0, 10.0, 10.0) # Camera position +camera.target = Vector3(0.0, 0.0, 0.0) # Camera looking at point +camera.up = Vector3(0.0, 1.0, 0.0) # Camera up vector (rotation towards target) +camera.fovy = 45.0 # Camera field-of-view Y +camera.projection = pyray.CAMERA_PERSPECTIVE # Camera projection type + +cubePosition = Vector3(0.0, 0.0, 0.0) + +disable_cursor() # Limit cursor to relative movement inside the window + +set_target_fps(60) # Set our game to run at 60 frames-per-second + +# Main game loop +while not window_should_close(): # Detect window close button or ESC key + # Update + update_camera(camera, pyray.CAMERA_FREE) + + if is_key_down(pyray.KEY_Z): + camera.target = Vector3(0.0, 0.0, 0.0) + + # Draw + begin_drawing() + + clear_background(RAYWHITE) + + begin_mode_3d(camera) + + draw_cube(cubePosition, 2.0, 2.0, 2.0, RED) + draw_cube_wires(cubePosition, 2.0, 2.0, 2.0, MAROON) + + draw_grid(10, 1.0) + + end_mode_3d() + + draw_rectangle(10, 10, 320, 133, Fade(SKYBLUE, 0.5)) + draw_rectangle_lines(10, 10, 320, 133, BLUE) + + draw_text("Free camera default controls:", 20, 20, 10, BLACK) + draw_text("- Mouse Wheel to Zoom in-out", 40, 40, 10, DARKGRAY) + draw_text("- Mouse Wheel Pressed to Pan", 40, 60, 10, DARKGRAY) + draw_text("- Alt + Mouse Wheel Pressed to Rotate", 40, 80, 10, DARKGRAY) + draw_text("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 40, 100, 10, DARKGRAY) + draw_text("- Z to zoom to (0, 0, 0)", 40, 120, 10, DARKGRAY) + + end_drawing() + +# De-Initialization +close_window() # Close window and OpenGL context