Measuring negative distance

Hi guys, I’m building a runner where the travel-distance is measured using this method:

local function distance(start,stick) return math.sqrt((start.x-stick.x)^2) end -- Displaying like this (not actual code, just an example): distance\_text.text = distance(start, stick) distance\_text.text = math.round(distance(start, stick)) distance\_text = display.newText("", 0, 0, native.systemFont, 34)

‘Start’ is just an invisible rectangle that I’m using as a reference point.

This works fine, and reports the travel distance (in pixels) nicely… There are two things I’me trying to figure out:

1/ I’d like to divide the number by 10, so the reported values aren’t quite so big (I managed to get rid of the decimal increments by using math.round (as you can see), so taking this a step further would be nice for my game.

2/ I’d like to display a negative number when the ‘character’ travels to a negative position (relative to ‘start’), so, ‘start’ is currently positioned at *.contentCenterX.

Thanks.

it would appear from your code that the “stick” can only move on the x-axis.

if so, no need for pythagoras, the distance is simply:

math.abs(x2-x1)

to divide by ten, you’d…, um, …, well, … um…  divide by ten:  :D

math.abs(x2-x1)/10

to get a signed distance (potentially negative), just leave out the math.abs:

(x2-x1)/10

hth

it would appear from your code that the “stick” can only move on the x-axis.

if so, no need for pythagoras, the distance is simply:

math.abs(x2-x1)

to divide by ten, you’d…, um, …, well, … um…  divide by ten:  :D

math.abs(x2-x1)/10

to get a signed distance (potentially negative), just leave out the math.abs:

(x2-x1)/10

hth