diff options
| author | realtradam <[email protected]> | 2021-01-29 00:31:00 -0500 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-01-29 00:31:00 -0500 |
| commit | bee6e8aa782895acefc6b40b2559f4d618a9466d (patch) | |
| tree | 8d842ce28317bc23bd3bd56a0df07bd27a2e54ff | |
| parent | 7c87aec3da8f502bc5401264fc68dcefde25caf0 (diff) | |
| download | ruby2d-camera-bee6e8aa782895acefc6b40b2559f4d618a9466d.tar.gz ruby2d-camera-bee6e8aa782895acefc6b40b2559f4d618a9466d.zip | |
small progress
| -rw-r--r-- | camera.rb | 34 | ||||
| -rw-r--r-- | quad_camera_tracker.rb | 18 | ||||
| -rw-r--r-- | run.rb | 89 |
3 files changed, 106 insertions, 35 deletions
@@ -41,12 +41,15 @@ class Camera @rotation_degrees ||= 0 end - def self.rotate_degrees_by(degrees) + def self.rotate_by(degrees) # offset rotation to world - end - - def self.rotate_degree_to(degrees) - # set rotation + objects.each do |object| + unless object.is_a?(Image) or object.is_a?(AnimatedSquare) + object.update(rotate: degrees) + end + end + self.rotation_degrees += degrees + self.rotation_degrees %= 360 end def self.zoom_by(zoom) @@ -69,8 +72,10 @@ class Camera end def self.follow(item) - move_to(((item.true_center[0] + item.width / 2) - (Window.width / 2)) / elasticity, - ((item.true_center[1] + item.height / 2) - (Window.height / 2)) / elasticity) + move_by(((item.x - camera_position_x - (Window.width / 2) / zoom_level) / + (elasticity / (zoom_level * zoom_level))), + ((item.y - camera_position_y - (Window.height / 2) / zoom_level) / + (elasticity / (zoom_level * zoom_level)))) end def self.objects @@ -102,8 +107,20 @@ class Camera self.camera_position_x += x / zoom_level self.camera_position_y += y / zoom_level end - +# SOMETHING MESSED UP + # with the camera position thing def self.move_to(x, y) + objects.each do |object| + if object.is_a?(Image) or object.is_a?(AnimatedSquare) + object.x -= object.x + x + object.y -= object.y + y + else + object.update(x: -Camera.camera_position_x + x, y: -Camera.camera_position_y + y) + end + end + self.camera_position_x = x / zoom_level + self.camera_position_y = y / zoom_level +=begin self.camera_position_x = x / zoom_level + camera_position[0] self.camera_position_y = y / zoom_level + camera_position[1] objects.each do |object| @@ -114,5 +131,6 @@ class Camera object.update(x: x, y: y) end end +=end end end diff --git a/quad_camera_tracker.rb b/quad_camera_tracker.rb index de3e786..81c7561 100644 --- a/quad_camera_tracker.rb +++ b/quad_camera_tracker.rb @@ -23,9 +23,9 @@ module QuadCameraTracker @y4 *= options[:zoom] end if options[:rotate] - x_pivot = Camera.camera_position_x - x - y_pivot = Camera.camera_position_y - y - calc_angle = (Math::PI * angle) / 180 + x_pivot = -x + y_pivot = -y + calc_angle = (Math::PI * options[:rotate]) / 180 x_shifted1 = x1 - x_pivot y_shifted1 = y1 - y_pivot x_shifted2 = x2 - x_pivot @@ -49,8 +49,12 @@ module QuadCameraTracker end end - def true_center - [[@x1, @x2, @x3, @x4].min, [@y1, @y2, @y3, @y4].min] + # Uses a 'fast' method of getting the center + # perfectly accurate for squares and rectangles + # may be inaccurate for other quadrilaterals + def lazy_center + [([@x1, @x2, @x3, @x4].min + [@x1, @x2, @x3, @x4].max) / 2, + ([@y1, @y2, @y3, @y4].min + [@y1, @y2, @y3, @y4].max) / 2] end def width @@ -96,6 +100,10 @@ module QuadCameraTracker # offset rotation to shape end + def x1_debug + @x1 + end + def x1 @x1 / Camera.zoom_level + Camera.camera_position[0] - x # undo rotation/translation/zoom @@ -7,9 +7,9 @@ require_relative 'animator' @background = Image.new('assets/background.png') #@player = Image.new('assets/player.png') @player = Quad.new(x1: 0, y1: 0, - x2: 20, y2: 0, - x3: 20, y3: 20, - x4: 0, y4: 20) + x2: 20, y2: 0, + x3: 20, y3: 20, + x4: 0, y4: 20) @squares = [] # Use this to add a few extra methods to an Image @@ -39,12 +39,27 @@ Camera.elasticity = 10 # If you want to use a camera, you need all elements in the world to be known to it # Exeptions would be things such as UI elements where you want them statically placed on the screen 25.times do - @squares << AnimatedSquare.new(x: (0..1920).to_a.sample, - y: (0..1080).to_a.sample, - size: (10..50).to_a.sample, - color: 'random') - Camera << @squares.last + tempx = (0..1920).to_a.sample + tempy = (0..1080).to_a.sample + tempsize = (25..100).to_a.sample + Camera << Quad.new(x1: tempx, + y1: tempy, + x2: tempx, + y2: tempy + tempsize, + x3: tempx + tempsize, + y3: tempy + tempsize, + x4: tempx + tempsize, + y4: tempy, + size: (10..50).to_a.sample, + color: 'random') end +#25.times do +# @squares << AnimatedSquare.new(x: (0..1920).to_a.sample, +# y: (0..1080).to_a.sample, +# size: (10..50).to_a.sample, +# color: 'random') +# Camera << @squares.last +#end # An example of static elements on the screen that # do not follow the camera movement @@ -99,16 +114,20 @@ Rectangle.new( on :key do |event| if event.key == 'a' - @x_move -= @speed + @y_move += Math.sin(Math::PI * (180 + @player.rotation_degrees) / 180) * @speed + @x_move += Math.cos(Math::PI * (180 + @player.rotation_degrees) / 180) * @speed end if event.key == 'd' - @x_move += @speed + @y_move += Math.sin(Math::PI * @player.rotation_degrees / 180) * @speed + @x_move += Math.cos(Math::PI * @player.rotation_degrees / 180) * @speed end if event.key == 'w' - @y_move -= @speed + @y_move += Math.sin(Math::PI * (-90 + @player.rotation_degrees) / 180) * @speed + @x_move += Math.cos(Math::PI * (-90 + @player.rotation_degrees) / 180) * @speed end if event.key == 's' - @y_move += @speed + @y_move += Math.sin(Math::PI * (90 + @player.rotation_degrees) / 180) * @speed + @x_move += Math.cos(Math::PI * (90 + @player.rotation_degrees) / 180) * @speed end if event.key == 'j' @cam_x_move -= @speed @@ -132,14 +151,36 @@ on :key do |event| end on :key do |event| + if event.key == 'z' + @zoom_by = 1.015 + end + if event.key == 'x' + @zoom_by = 0.985 + end + if event.key == 'c' + Camera.zoom_to 1 + end + if event.key == 'm' + Camera.move_to(-10,-10) + end + if event.key == 'q' - @zoom_by = 1.05 + temp_cam = Camera.camera_position + Camera.rotate_by(-1) + Camera.move_to(@player.lazy_center[0] - (Window.width / 2), + @player.lazy_center[1] - (Window.height / 2)) end if event.key == 'e' - @zoom_by = 0.95 + Camera.rotate_by(1) + Camera.move_to(@player.lazy_center[0] - (Window.width / 2), + @player.lazy_center[1] - (Window.height / 2)) end if event.key == 'r' - Camera.zoom_to 1 + unless Camera.rotation_degrees.zero? + Camera.rotate_by(-Camera.rotation_degrees) + Camera.move_to(@player.lazy_center[0] - (Window.width / 2), + @player.lazy_center[1] - (Window.height / 2)) + end end end @quad = Quad.new(x1: 0, y1: 0, @@ -148,6 +189,9 @@ end x4: 0, y4: 20) Camera << @quad @frame = 0 + @player.rotate_relative([email protected], + -12) update do @frame += 1 @frame %= 60 @@ -167,18 +211,19 @@ update do end @quad.rotate_relative(50 - @quad.x,50 - @quad.y,1) @quad.color = 'random' if @frame.zero? - puts @quad.rotation_degrees + #puts @quad.rotation_degrees # Alternating between follow and manual control if @is_follow Camera.follow @player else Camera.move_by(@cam_x_move, @cam_y_move) end - puts Math.cos(Math::PI * @player.rotation_degrees.to_f / 180) - @player.rotate_relative(Math.cos(Math::PI * (@player.rotation_degrees + 1 * 180 / 4) / 180) * (10 * 1.41421356237), - Math.cos(Math::PI * (@player.rotation_degrees - 1 * 180 / 4) / 180) * (10 * 1.41421356237), - 2) - + puts @player.x1_debug + #Camera.rotate_by(2) + #puts Math.cos(Math::PI * @player.rotation_degrees.to_f / 180) + #@player.rotate_relative(Math.cos(Math::PI * (@player.rotation_degrees + 1 * 180 / 4) / 180) * (10 * 1.41421356237), + # Math.cos(Math::PI * (@player.rotation_degrees - 1 * 180 / 4) / 180) * (10 * 1.41421356237), + # 2) # This function will teleport the camera directory to those coordinates # It is used by Camera.follow but you can use it yourself too! #Camera.move_to(50,50) @@ -187,7 +232,7 @@ update do @ui_pos.text = "Camera Position: #{Camera.camera_position[0].round(1)}, #{Camera.camera_position[1].round(1)}" @ui_zoom.text = "Zoom: #{Camera.zoom_level.round(3)}" @ui_fps.text = "FPS: #{Window.fps.round(2)}" - @ui_rotation.text = "Angle: #{@player.rotation_degrees.round(-1)}" + @ui_rotation.text = "Angle: #{Camera.rotation_degrees}" end show |
