summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.mdown4
-rw-r--r--README.mdown5
-rw-r--r--lib/ruby2d/camera.rb17
-rw-r--r--lib/ruby2d/camera/version.rb2
4 files changed, 27 insertions, 1 deletions
diff --git a/CHANGELOG.mdown b/CHANGELOG.mdown
index b8436c7..a45db6b 100644
--- a/CHANGELOG.mdown
+++ b/CHANGELOG.mdown
@@ -6,6 +6,10 @@
![Deprecated](https://img.shields.io/badge/-Deprecated-orange)
![Removed](https://img.shields.io/badge/-Removed-red)
+## [1.1.0](https://github.com/realtradam/ruby2d-camera/releases/tag/1.1.0) - 2021-08-09
+![Added](https://img.shields.io/badge/-Added-brightgreen)
+- Added Camera functions `Camera.coordinate_to_screenspace` and `Camera.coordinate_to_worldspace`
+
## [1.0.0](https://github.com/realtradam/ruby2d-camera/releases/tag/1.0.0) - 2021-08-08
![Added](https://img.shields.io/badge/-Added-brightgreen)
- Initial release
diff --git a/README.mdown b/README.mdown
index 2a2e865..6152d07 100644
--- a/README.mdown
+++ b/README.mdown
@@ -19,6 +19,11 @@ Create your object as if you would create it in Ruby2D except you need to prefix
- `Camera.x` and `Camera.y` Default: 0. This is the position the camera is centered on in the "world"
- `Camera.angle` Default: 0. This is the angle of how much the camera is rotated(in degrees). It ranges from 0-360. Giving values outside of this range will automagically convert them to fit within the 0-360 range.
+### There are also 2 helpful functions:
+
+- `Camera.coordinate_to_worldspace(x,y)` You pass coordinates on the screen(for example where a player clicked in the window) and it will return the coordinates(in a 2d array) what the coordinates are in the world
+- `Camera.coordinate_to_screenspace(x,y)` You pass in a coordinate in the world(for example an enemy in your game) and it will give you the coordinates(in a 2d array) of where on the screen this character appears. Note this function may return values that are outside of the screen if the object is not in view of the camera
+
## How it works:
A single `Camera` module exists which keeps track of objects created with it. When you create an object with the camera it creates a special object that inherits the original object from Ruby2D and then adds additional functions. The Camera module then uses these functions to draw the various objects on the screen each frame, using the parameters you gave it.
diff --git a/lib/ruby2d/camera.rb b/lib/ruby2d/camera.rb
index ba0a280..8037b4d 100644
--- a/lib/ruby2d/camera.rb
+++ b/lib/ruby2d/camera.rb
@@ -94,6 +94,23 @@ module Ruby2D
angle %= 360
@angle = angle
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
end
end
diff --git a/lib/ruby2d/camera/version.rb b/lib/ruby2d/camera/version.rb
index 29ac7a8..2372efc 100644
--- a/lib/ruby2d/camera/version.rb
+++ b/lib/ruby2d/camera/version.rb
@@ -2,6 +2,6 @@
module Ruby2d
module Camera
- VERSION = '1.0.0'
+ VERSION = '1.1.0'
end
end