Find the distance between two points

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?

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.  :slight_smile:

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.  

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 } )

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

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.  :slight_smile:

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.  

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 } )

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