summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-01-27 20:19:19 -0500
committerrealtradam <[email protected]>2021-01-27 20:19:19 -0500
commit7c87aec3da8f502bc5401264fc68dcefde25caf0 (patch)
treeae278f67807d4d682a4c8ec873f2855e37ab65ea
parente66aa3520badc5b2641088ff5f9e903d13e2c98e (diff)
downloadruby2d-camera-7c87aec3da8f502bc5401264fc68dcefde25caf0.tar.gz
ruby2d-camera-7c87aec3da8f502bc5401264fc68dcefde25caf0.zip
more rotation additions
-rw-r--r--quad_camera_tracker.rb87
-rw-r--r--run.rb8
2 files changed, 64 insertions, 31 deletions
diff --git a/quad_camera_tracker.rb b/quad_camera_tracker.rb
index 25c6071..de3e786 100644
--- a/quad_camera_tracker.rb
+++ b/quad_camera_tracker.rb
@@ -1,26 +1,51 @@
# frozen_string_literal: true
module QuadCameraTracker
- def update(zoom: 1, x: 0, y: 0)
- if x != 0 or y != 0
- @x1 -= x
- @x2 -= x
- @x3 -= x
- @x4 -= x
- @y1 -= y
- @y2 -= y
- @y3 -= y
- @y4 -= y
+ def update(options = {})
+ if options[:x] and options[:y]
+ @x1 -= options[:x]
+ @x2 -= options[:x]
+ @x3 -= options[:x]
+ @x4 -= options[:x]
+ @y1 -= options[:y]
+ @y2 -= options[:y]
+ @y3 -= options[:y]
+ @y4 -= options[:y]
end
- if zoom != 1
- @x1 *= zoom
- @x2 *= zoom
- @x3 *= zoom
- @x4 *= zoom
- @y1 *= zoom
- @y2 *= zoom
- @y3 *= zoom
- @y4 *= zoom
+ if options[:zoom]
+ @x1 *= options[:zoom]
+ @x2 *= options[:zoom]
+ @x3 *= options[:zoom]
+ @x4 *= options[:zoom]
+ @y1 *= options[:zoom]
+ @y2 *= options[:zoom]
+ @y3 *= options[:zoom]
+ @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_shifted1 = x1 - x_pivot
+ y_shifted1 = y1 - y_pivot
+ x_shifted2 = x2 - x_pivot
+ y_shifted2 = y2 - y_pivot
+ x_shifted3 = x3 - x_pivot
+ y_shifted3 = y3 - y_pivot
+ x_shifted4 = x4 - x_pivot
+ y_shifted4 = y4 - y_pivot
+
+ self.x1 = x_pivot + (x_shifted1 * Math.cos(calc_angle) - y_shifted1 * Math.sin(calc_angle))
+ self.y1 = y_pivot + (x_shifted1 * Math.sin(calc_angle) + y_shifted1 * Math.cos(calc_angle))
+
+ self.x2 = x_pivot + (x_shifted2 * Math.cos(calc_angle) - y_shifted2 * Math.sin(calc_angle))
+ self.y2 = y_pivot + (x_shifted2 * Math.sin(calc_angle) + y_shifted2 * Math.cos(calc_angle))
+
+ self.x3 = x_pivot + (x_shifted3 * Math.cos(calc_angle) - y_shifted3 * Math.sin(calc_angle))
+ self.y3 = y_pivot + (x_shifted3 * Math.sin(calc_angle) + y_shifted3 * Math.cos(calc_angle))
+
+ self.x4 = x_pivot + (x_shifted4 * Math.cos(calc_angle) - y_shifted4 * Math.sin(calc_angle))
+ self.y4 = y_pivot + (x_shifted4 * Math.sin(calc_angle) + y_shifted4 * Math.cos(calc_angle))
end
end
@@ -167,10 +192,10 @@ module QuadCameraTracker
@rotation_degrees ||= 0
end
- def rotate(x_pivot, y_pivot, angle)
+ def rotate_relative(x_pivot, y_pivot, angle)
self.rotation_degrees += angle
self.rotation_degrees %= 360
- @angle = (Math::PI * angle) / 180
+ calc_angle = (Math::PI * angle) / 180
x_shifted1 = self.x1 - x_pivot
y_shifted1 = self.y1 - y_pivot
x_shifted2 = self.x2 - x_pivot
@@ -184,20 +209,24 @@ module QuadCameraTracker
x1_old = self.x1
y1_old = self.y1
- self.x1 = x_pivot + (x_shifted1 * Math.cos(@angle) - y_shifted1 * Math.sin(@angle))
- self.y1 = y_pivot + (x_shifted1 * Math.sin(@angle) + y_shifted1 * Math.cos(@angle))
+ self.x1 = x_pivot + (x_shifted1 * Math.cos(calc_angle) - y_shifted1 * Math.sin(calc_angle))
+ self.y1 = y_pivot + (x_shifted1 * Math.sin(calc_angle) + y_shifted1 * Math.cos(calc_angle))
- self.x2 = x_pivot + (x_shifted2 * Math.cos(@angle) - y_shifted2 * Math.sin(@angle))
- self.y2 = y_pivot + (x_shifted2 * Math.sin(@angle) + y_shifted2 * Math.cos(@angle))
+ self.x2 = x_pivot + (x_shifted2 * Math.cos(calc_angle) - y_shifted2 * Math.sin(calc_angle))
+ self.y2 = y_pivot + (x_shifted2 * Math.sin(calc_angle) + y_shifted2 * Math.cos(calc_angle))
- self.x3 = x_pivot + (x_shifted3 * Math.cos(@angle) - y_shifted3 * Math.sin(@angle))
- self.y3 = y_pivot + (x_shifted3 * Math.sin(@angle) + y_shifted3 * Math.cos(@angle))
+ self.x3 = x_pivot + (x_shifted3 * Math.cos(calc_angle) - y_shifted3 * Math.sin(calc_angle))
+ self.y3 = y_pivot + (x_shifted3 * Math.sin(calc_angle) + y_shifted3 * Math.cos(calc_angle))
- self.x4 = x_pivot + (x_shifted4 * Math.cos(@angle) - y_shifted4 * Math.sin(@angle))
- self.y4 = y_pivot + (x_shifted4 * Math.sin(@angle) + y_shifted4 * Math.cos(@angle))
+ self.x4 = x_pivot + (x_shifted4 * Math.cos(calc_angle) - y_shifted4 * Math.sin(calc_angle))
+ self.y4 = y_pivot + (x_shifted4 * Math.sin(calc_angle) + y_shifted4 * Math.cos(calc_angle))
# Updates x and y to be on the origin correctly
@x += -x1_old + self.x1
@y += -y1_old + self.y1
end
+
+ def rotate(angle)
+ rotate_relative(0, 0, angle)
+ end
end
diff --git a/run.rb b/run.rb
index 8c82fb9..b21b699 100644
--- a/run.rb
+++ b/run.rb
@@ -165,7 +165,7 @@ update do
@squares.each do |square|
square.update(Camera.camera_position, Camera.zoom_level)
end
- @quad.rotate(50 - @quad.x,50 - @quad.y,30) if @frame.zero?
+ @quad.rotate_relative(50 - @quad.x,50 - @quad.y,1)
@quad.color = 'random' if @frame.zero?
puts @quad.rotation_degrees
# Alternating between follow and manual control
@@ -174,7 +174,10 @@ update do
else
Camera.move_by(@cam_x_move, @cam_y_move)
end
- @player.rotate(0, 0, 5)
+ 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!
@@ -184,6 +187,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)}"
end
show