superpowers / superpowers-game

:video_game: Make 2D and 3D games in live collaboration with Superpowers
http://superpowers-html5.com/
Other
402 stars 93 forks source link

Camera's pixel per unit #150

Closed MathieuLoutre closed 8 years ago

MathieuLoutre commented 8 years ago

When working on a responsive game (no set width/height), I'd like some GUI elements to cover a particular percentage of the screen (for instance, always cover 50% of the screen whatever the resolution). Currently, as far as I'm aware, there's no way to do that easily.

I've tried something as detailed here: https://itch.io/t/38027/asset-scaling-to-match-camera-size#post-49255 and ended up monkey patching the game system to add:

getScreenWidth() { return this.canvas.clientWidth * this.viewport.width; }
getScreenHeight() { return this.canvas.clientHeight * this.viewport.height; }

which then allows me to do something like

let cameraPixelPerUnit = this.cameraActor.camera.getScreenHeight() / this.cameraActor.camera.getOrthographicScale();

I can go on to use that ratio to scale any assets regardless of their own pixel per unit ratio etc. It's incredibly useful. I was wondering if there's any interest in adding these methods or maybe a getPixelPerUnit() method on the camera. I could prepare a PR for it if so.

Let me know.

bilou84 commented 8 years ago

Can't you already use Sup.Input.getScreenSize() and camera.getViewport() to do this computation?

But maybe we can still add a method to make it easier. Not completly sure.

MathieuLoutre commented 8 years ago

Ah! I was expecting the "getScreenSize()" method to be on the Camera for some reason. It wasn't entirely intuitive. However, I'll give that a go and report if I can get the same results. No need for a dedicated method if the data is already available.

MathieuLoutre commented 8 years ago

Looks exactly like what I was after. I wish there was a better way to find out what the engine has in store. The API search isn't always the best way to find methods (if the keywords don't match). Something for the future maybe?

bilou84 commented 8 years ago

Yeah the search API can definitly be improved... Sorry for not being very convenient right now