Transition.to with constant speed ?

Hi,

Is there any way to move object to position with transition.to function using constant speed rather then time ?

Thanks

What do you mean by speed?

If you mean pixels/frame then it won’t be constant because of fluctuating fps.

i want to calculate the time needed for one object to go from one place to the other based on distance and speed

and then just use transition.to(  “calculated time” , x, y)

Hi @mladenjacket,

For this, you’ll probably need a “distance between” function like the following, then use that result to calculate your time over the variable distance.

[lua]

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

[/lua]

Hope this helps,

Brent

Thanks it worked perfectly

If anyone is interested how i used it to achive constant speed, this is my code

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 speed = 0.1 local A physics.addBody(A) times = 1 \* distanceBetween(A,B) / speed transition.to(A, {time=times,x=B.x, y=B.y})

What do you mean by speed?

If you mean pixels/frame then it won’t be constant because of fluctuating fps.

i want to calculate the time needed for one object to go from one place to the other based on distance and speed

and then just use transition.to(  “calculated time” , x, y)

Hi @mladenjacket,

For this, you’ll probably need a “distance between” function like the following, then use that result to calculate your time over the variable distance.

[lua]

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

[/lua]

Hope this helps,

Brent

Thanks it worked perfectly

If anyone is interested how i used it to achive constant speed, this is my code

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 speed = 0.1 local A physics.addBody(A) times = 1 \* distanceBetween(A,B) / speed transition.to(A, {time=times,x=B.x, y=B.y})