Math helper functions: "distanceBetween" and "angleBetween"

Hi Andrew,
The function wasn’t necessarily intended for use in an event listener, but you can just set it up like this, for example:

local function distanceBetween( point1, point2 )  
  
 local xfactor = point2.x-point1.x ; local yfactor = point2.y-point1.y  
 local distanceBetween = math.sqrt((xfactor\*xfactor) + (yfactor\*yfactor))  
 return distanceBetween  
  
end   
  
local function checkDistance( event )  
  
 local thisDistance = distanceBetween( [one object], [other object] )  
 print(thisDistance) --for testing only, remove this line later  
  
end  
  
Runtime:addEventListener( "enterframe", checkDistance )  

So in essence, the Runtime listener is actually calling the function “checkDistance” which in turn calls (and receives in return) the distance from “distanceBetween”. This structure will also allow you to retrieve the distance between any number of objects in the same game cycle. You’d just need to call “distanceBetween” for each pair, all within the “checkDistance” function.

Does this help?

Brent

[import]uid: 9747 topic_id: 3709 reply_id: 53978[/import]

You should also be able to nest the “distanceBetween” function as follows. This should improve performance slightly, since Corona doesn’t need to constantly up-value lookup the function for each pair of objects you need to work with.

local function checkDistance( event )  
  
 ---nest this function; should improve performance slightly  
 local function distanceBetween( point1, point2 )  
 local xfactor = point2.x-point1.x ; local yfactor = point2.y-point1.y  
 local distanceBetween = math.sqrt((xfactor\*xfactor) + (yfactor\*yfactor))  
 return distanceBetween  
 end   
  
 local thisDistance = distanceBetween( [one object], [other object] )  
 print(thisDistance) --for testing only, remove this line later  
  
end  
  
Runtime:addEventListener( "enterframe", checkDistance )  

[import]uid: 9747 topic_id: 3709 reply_id: 53980[/import]

Hi,

I’m eleven years old and a novice at Corona. I’m having an issue with the argument you are talking about. I try making point 1 a cannon and point 2 the touch (but not the x and y coordinates, just the things in general.) However, the terminal says ‘name’ or ‘…’ expected near ‘“touch”’ (‘name’ is actually in < > brackets.) Here’s the code:

[code]
local function angleBetween ( cannon, “touch” )

local xDist = event.x-cannon.x; local yDist = event.y-cannon.y
local angleBetween = math.deg( math.atan( yDist/xDist ) )
if ( cannon.x < event.x ) then
angleBetween = angleBetween+90
else
angleBetween = angleBetween-90
end
end [import]uid: 82408 topic_id: 3709 reply_id: 56739[/import]

Hi perseusspartacus,

Try this code. It will rotate the ‘rect’ display object so that it points to wherever on the screen you touch. You should be able to adapt this to your needs:

[lua]local rect = display.newRect( 200, 200, 10, 5 )

local function rotateTo(event)
local deg = math.atan2(event.y - rect.y, event.x - rect.x)
rect.rotation = math.deg(deg)
end

Runtime:addEventListener(“touch”, rotateTo)[/lua]

Thanks,
Darren [import]uid: 1294 topic_id: 3709 reply_id: 56787[/import]

Thanks a lot. It adapted wonderfully. I may be able to use this in future games. Cya! :wink: [import]uid: 82408 topic_id: 3709 reply_id: 56948[/import]

I am also very keen to see the preferred vector math solutions from Carlos. [import]uid: 83823 topic_id: 3709 reply_id: 84113[/import]

I just thought I’d throw this out there, since sometimes, you can get negative angles or if you’re adding two angles, they can end up making the object spin excessively around itself.

local function cleanAngle( angle ) if angle \< 0 then angle = angle + 360 end return angle % 360 end

Basically, this will ensure that your angle will always be in the range of 0-359 when used like this:

rotateTo = cleanAngle( angleBetween( object1, object2 ) )

@BeyondtheTech,
Awesome, thank you! I wrote that angle function long, long ago, and it definitely had some “issues” which were pointed out along the way. I think your addition makes it perfect. :slight_smile:

Brent Sorrentino
[import]uid: 9747 topic_id: 3709 reply_id: 109748[/import]