Get the DPI of the current display

To build touch-enabled games I need to be able to define a “hot” region around some game objects.

If a touch falls within a “hot” region then future touch events might manipulate that object.

Right now I am building hot regions by using Camera:GetWorldToScreenPoint() and testing if the touch event is within X pixels of my object.

I have no way to scale X based on the screen resolution, which limits my ability to make my game work well on all devices.

6 Likes

You can get the screen resolution by using ScreenGui.AbsoluteSize.

You can also get it with Camera.ViewportSize. But this doesn’t solve Shedletsky’s problem.
We need some way to get either the DPI or (preferrably) the physical size of the display to allow better scaling for different devices in our games, or else we get problems like OP where gameplay might even suffer.

1 Like

That gives the size in pixels.

An iPhone 6 can be 401 ppi and a 27 inch 1080p screen can be 81 ppi.

So if I size my regions correctly for the phone, they are going to be way too big on the monitor.

More importantly, if I size my regions for the iPhone 6, they will also be too big on some android crap phone with 100 ppi that I can’t test on. Or they will be way too small on an android super phone. It seems like they go to ~600 ppi.

3 Likes

Couldn’t you scale it based on the width to height ratio of the screen, rather than the real-world scale of the display?

The height of the screen scales the viewport afaik. You might be able to figure out something from that.

local cam = workspace.CurrentCamera
local vp = cam.ViewportSize
local aspectRatio = vp.X/vp.Y

local hFactor = math.tan(math.rad(cam.FieldOfView/2))
local wFactor = hFactor * aspectRatio

I’m basically trying to figure out how many pixels wide your finger is.

The only way I can think to get anywhere close to the answer is to ask the user to trace a figure on the screen to calibrate

We ran into this issue when trying to display emulated devices in Studio. It turns out there’s no universal way to determine this so your best bet is to prompt the user, probably by giving them a slider and adjusting it until something on screen matches a real-world object.

4 Likes

You could get pretty creative with this, I bet. I think I might go write a module that does this in a user-friendly way…

1 Like

Does that not suggest it should be a feature of the device?

Not exactly sure what you mean, but there’s no major OS that directly exposes physical display density. There’s no real way for desktop OSes to know, Android only gives you a general idea, and iOS tries to refer to everything in “points” that loosely correspond to actual density. So iOS is the only one that gets anywhere close.

3 Likes

Wow he’s right. Learn something new every day.

1 Like