Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Find the distance between two points
Started by decoder143 May 10 2018 07:32 PM

3 replies to this topic
distance axis

Best Answer roaminggamer , 10 May 2018 - 09:07 PM

Answer to title of post:

 

You can get the distance between two points using vector math.  There are many libraries out there, including math2d which is part of SSK2.

 

Using math2d, if you have any object, event, or table with an x and y you can get the distance between them as follows:

local obj1 = display.newCircle( 100, 100, 10) 
local obj2 = display.newCircle( 175, 213, 10 )

--https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/#distance-between
local dist = ssk.math2d.distanceBetween( obj1, obj2 )

or you could do it this way:

local obj1 = display.newCircle( 100, 100, 10) 
local obj2 = display.newCircle( 175, 213, 10 )

-- https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/#subtraction
local vec = ssk.math2d.sub( obj1, obj2 )

-- https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/#length
local dist = ssk.math2d.length( vec )

Answer to body of post, what?  Ah, yes I can think of many ways to move objects but I don't know about your game constraints to answer this question meaninfully.

 

 

PS - Was this posted from a mobile device?  Train of though questions are very hard to understand and answer.  :)

 

Please try to use more line breaks and separate paragraphs for legibility please.   

 

Take some time to clearly structure your posts with fewer sub-questions and more clear examples and you'll get  better help.  

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

decoder143

[GLOBAL: userInfoPane.html]
decoder143
  • Observer

  • 18 posts
  • Corona SDK

Hi guys.In my game i need a function which moves the object towards the location of the event.

If a collision happens,object should go to that place.If i use transition,the speed changes.If the event is nearby,it moves slower.If the object is far,it moves faster.(Since time is same).But i need a constant speed.If I am able to calculate the distance between the object's current position and event's location,I will be able to have a constant (almost) speed.Any ideas or alternatives?



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,377 posts
  • Corona SDK

  Best Answer

Answer to title of post:

 

You can get the distance between two points using vector math.  There are many libraries out there, including math2d which is part of SSK2.

 

Using math2d, if you have any object, event, or table with an x and y you can get the distance between them as follows:

local obj1 = display.newCircle( 100, 100, 10) 
local obj2 = display.newCircle( 175, 213, 10 )

--https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/#distance-between
local dist = ssk.math2d.distanceBetween( obj1, obj2 )

or you could do it this way:

local obj1 = display.newCircle( 100, 100, 10) 
local obj2 = display.newCircle( 175, 213, 10 )

-- https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/#subtraction
local vec = ssk.math2d.sub( obj1, obj2 )

-- https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/#length
local dist = ssk.math2d.length( vec )

Answer to body of post, what?  Ah, yes I can think of many ways to move objects but I don't know about your game constraints to answer this question meaninfully.

 

 

PS - Was this posted from a mobile device?  Train of though questions are very hard to understand and answer.  :)

 

Please try to use more line breaks and separate paragraphs for legibility please.   

 

Take some time to clearly structure your posts with fewer sub-questions and more clear examples and you'll get  better help.  


  • decoder143 likes this

[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,377 posts
  • Corona SDK

One final note.

 

You can give a transition a constant speed as follows:

local speed = 50 -- content space pixels per second

local obj1 = display.newCircle( 100, 100, 10) 
local target = { x = 175, y = 213 }

--https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/#distance-between
local dist = ssk.math2d.distanceBetween( obj1, target )

local time = 1000 * dist / speed

transition.to( obj1, { x = target.x, y = target.y, time = time } )

  • decoder143 likes this

[TOPIC: post.html]
#4

decoder143

[GLOBAL: userInfoPane.html]
decoder143
  • Observer

  • 18 posts
  • Corona SDK

Thanks bro...From next time,I will format my questions.....!! :D  :D




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: distance, axis