Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

How to calculate the distance between two screen touches?
Started by lcriptocoin Aug 26 2018 05:04 AM

2 replies to this topic
zoom multitouch scale
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 1 posts
  • Corona SDK

Hello! Please tell me how to calculate the distance between two touches of the screen fingers? This is necessary in order to scale the image, if the distance between the fingers increases , the picture increases, and Vice versa.

[TOPIC: post.html]

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 948 posts
  • Corona SDK

You should read https://docs.coronalabs.com/guide/events/touchMultitouch/.

You would need to get the event.x and event.y values for both touches and then use Pythagora's theorem to calculate the distance.

[TOPIC: post.html]

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,212 posts
  • Enterprise

This is some pretty straightforward math.  You have two touch points that each have an X and Y value. Let's call them touch1 and touch2 and their values are touch1.x, touch1.y, touch2.x and touch2.y. You can then get the distance via:


local dx =  touch1.x - touch2.x
local dy =  touch1.y - touch2.y

local distance = math.sqrt( dx * dx + dy * dy )