summaryrefslogtreecommitdiffhomepage
path: root/lib/camera/camera.rb
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-05-05 06:45:59 -0400
committerrealtradam <[email protected]>2021-05-05 06:45:59 -0400
commit200d6ec73caf9c070375a972677c4c0af7c444ba (patch)
treec1740919c86904ddf35701dc922fb763b528073d /lib/camera/camera.rb
parenta62ac5e77570516c399f9bad114aaba7bf20ff67 (diff)
downloadtileset-map-editor-200d6ec73caf9c070375a972677c4c0af7c444ba.tar.gz
tileset-map-editor-200d6ec73caf9c070375a972677c4c0af7c444ba.zip
.
Diffstat (limited to 'lib/camera/camera.rb')
-rw-r--r--lib/camera/camera.rb25
1 files changed, 23 insertions, 2 deletions
diff --git a/lib/camera/camera.rb b/lib/camera/camera.rb
index 64e2095..e34d2d8 100644
--- a/lib/camera/camera.rb
+++ b/lib/camera/camera.rb
@@ -69,8 +69,8 @@ module Camera
if auto_purge
objects.each do |item|
if item.nil?
- puts "Warning: Nil Object detected in Camera"
- puts " Nil Object removed"
+ puts 'Warning: Nil Object detected in Camera'
+ puts ' Nil Object removed'
objects.delete(obj)
else
item.redraw
@@ -81,6 +81,27 @@ module Camera
end
end
+ # Convert screenspace coordinates into worldspace camera ones
+ def self.coordinate_to_worldspace(x, y)
+ angle = Camera.angle * (Math::PI / 180)
+ half_width = Window.width * 0.5
+ half_height = Window.height * 0.5
+
+ [(((x - half_width) / zoom) * Math.cos(-angle)) - (((y - half_height) / zoom) * Math.sin(-angle)) + self.x,
+ (((x - half_width) / zoom) * Math.sin(-angle)) + (((y - half_height) / zoom) * Math.cos(-angle)) + self.y]
+ end
+
+ # Convert worldspace camera coordinates into screenspace ones
+ def self.coordinate_to_screenspace(x, y)
+ angle = Camera.angle * (Math::PI / 180)
+ [(((x - Camera.x) * Math.cos(angle)) - ((y - Camera.y) * Math.sin(angle))) * Camera.zoom + (Window.width * 0.5),
+ (((x - Camera.x) * Math.sin(angle)) + ((y - Camera.y) * Math.cos(angle))) * Camera.zoom + (Window.height * 0.5)]
+ end
+
+ def self.mouse
+ coordinate_to_worldspace(Window.mouse_x, Window.mouse_y)
+ end
+
# Variables changing Camera properties
def self._x(x)
@x += x