Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

Get physical distance from content pixels
Started by leojharris Aug 03 2018 02:46 PM

3 replies to this topic
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 19 posts
  • Corona SDK

Hi all,


I am trying to get the physical distance for any screen density device given the content pixels using

event.yStart - event.y 

within the 'touch' in of the EventListener, such that if i move my finger across a device I want to obtain the physical distance moved using the content pixels of the event that I am working with.


My question is how can I obtain the physical distance?


It was pointed out to me that letterbox should scale everything relative but I wasn't finding the results to be entirely accurate regarding the physical distance using simply letterbox.


Also I did discover the androiddisplayydpi property i.e. https://docs.coronalabs.com/api/library/system/getInfo.html#androiddisplayydpi which according to the docs says "can be used to can be used to convert pixels to inches and vice versa" sounds like what I need so I tried to apply it:

local dpi =  system.getInfo("androidDisplayYDpi")

local distanceDpi = (event.yStart - event.y) / dpi

I tested the above with letterbox set in my config.lua but still doesn't seem to get me the correct physical distance.


I am testing by connecting two devices usb and using adb logcat and printing the distanceDpi value above and there appears a disproportion between the values for each device.


Anyone been able to get good results obtaining physical distance from content pixels from content pixels across all sorts of device? And how so?

[TOPIC: post.html]

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,154 posts
  • Corona SDK

You need one other thing. Your content area is your content area and you in theory don't need to ever know about pixels and dpi, but in your use, you need that.  There is display.pixelWidth which will get you the actual width of your screen in pixels. Then you can calculate how much your content area is being scaled.  Armed with this info you can do:

local scale = display.pixelWidth / display.actualContentWidth
local distanceDpi = (event.yStart - event.y) / dpi * scale

or something like that.



[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 19 posts
  • Corona SDK

Hi @Rob


I will try that out, I am doing this currently but could change to your implementation but I found this to work okay as well..

local heightInInches = system.getInfo("androidDisplayHeightInInches")

local yPhysicalDistance = (event.yStart- event.y) * - HeightInInches

local speedY = (yPhysicalDistance / flickDuration) 

Not entirely sure if the value of yPhysicalDistance is in fact in inches but swiping on two devices at start and end Y positions in the same given time as closely as I can gives pretty close results to the same distance.. thoughts on this? 

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Corona Geek

  • 1,276 posts
  • Enterprise

i'm pretty sure i already said it in your first post, but.. you need display.contentScaleY in there


your delta is in content pixels

display.contentScaleY gives you the ratio of content pixels to device pixels

getInfo("androidDisplayYDpi") gives you the ratio of device pixels to inches

(though devices may "lie" about their size and/or density)


so multiply (or divide, aka invert then multiply) by the ratios so that unwanted units cancel out, something like:

yDeltaContent = event.yStart - event.y
yDeltaDevice = yDeltaContent / display.contentScaleY
yDeltaInches = yDeltaDevice / system.getInfo("androidDisplayYDpi")