Alternative to transition.to( ) with set speed value instead of time (do I just use setLinearVelocity?)

I know, quite the title.

But I managed to fit my question in it :slight_smile:

What I want is for an image (body, if you like, though there are no physics involved) to move from one place to another, then reappear in its initial position and do the same movement again. For now I have a code which involves transition.to( ) and works. Problem is, I have several instances of the object, all starting from different positions and going to different points, but I want them all to have the exact same speed, which is really difficult to do with the time parameter.

So, to that end, can I replace transition.to() with setLinearVelocity or any other physics function, or is there another way of doing it, with or without transition.to() ?

Thank you for your help. [import]uid: 106739 topic_id: 18629 reply_id: 318629[/import]

That’s not that hard :slight_smile:

You can make a distance function and multiply it by a good working number as the time!

[lua]local function distance(x1,y1,x2,y2)
return math.sqrt(math.pow(x2-x1,2)+math.pow(y2-y1,2))
end[/lua]

Now if your Object has to move from 0,0 to 100,0 and you would do
[lua]transition.to(myObj,{time=distance(0,0,100,0),x=100,y=0})[/lua]
it would take 100 miliseconds (since the distance is 100px) [import]uid: 13097 topic_id: 18629 reply_id: 71558[/import]

Greetings Fanta,

Thanks for your swift reply. However, as I suck at math, I fail to understand exactly how x1, x2, y1 and y2 define the speed. I understand what they are doing in the function, but not the association between time and the square root of the power of x2-x1 and 2 plus the power of y2 - y1, 2. Why do I need x1, x2, y1 and y2, and not just x and y? Why do I need to have a subtraction in there? And is the square root of all that the value of speed? And mainly, what does changing x1 x2 y1 and y2 do, respectively?

I think you have to explain everything you are doing in that code :S

Thanks for your help and patience :slight_smile: [import]uid: 106739 topic_id: 18629 reply_id: 71561[/import]

Hey PerturbedMollusk,

It’s the The Pythagorean theorem ( http://en.wikipedia.org/wiki/Pythagoras )
It can be used to measure the distance between two objects!

Using this formula, you’ll get the distance.
When using the distance as the transitionTime, every object will move at the same speed (since speed is just distance/time, which will be always the same)

The distance function I gave you needs 4 parameters: the X/Y-Coordinates of the first and second Object - in your example it will be the objects x and y value and his destination point!

Have fun :slight_smile: [import]uid: 13097 topic_id: 18629 reply_id: 71568[/import]

Check this site out for basic Physics.
http://www.physicsclassroom.com/class/1dkin/u1l1d.cfm

EDIT:
You’ll also need this to explain the Pythagorean Theorem (which explains the need for x1,x2,y1,y2 etc)
http://www.slideshare.net/ianwild1972/pythagorean-theorem-made-simple-1331800 [import]uid: 70847 topic_id: 18629 reply_id: 71567[/import]

Cheers guys, been a while since I was in school and as I never needed this stuff since now, I’ve forgotten most of it! :stuck_out_tongue: I get it now :slight_smile:

EDIT: Oh, one other thing. If I want to have the animation be modular, how would I call the function that included the transition.to( ) ?

For example, I have this right now:

in animate.lua:

[lua]function distance(x1,y1,x2,y2)
return math.sqrt(math.pow(x2-x1,2)+math.pow(y2-y1,2))
end

function newPlayer:animate(params)

startingX = params.startingX
startingY = params.startingY
endX = params.endX
endY = params.endY

transition.to(newPlayer, {time = distance(startingX, startingY, endX, endY), x = endX, y = endY, onComplete = newPlayer.resetPlayer})
end[/lua]

in main.lua

[lua]local player = player.new({x = screenRight/2, y = screenTop-80})
player:animate({time = distance(screenRight/2, screenTop, screenRight/2, screenBottom), startingX = player.x, startingY = screenBottom})[/lua]

which doesn’t work. I obviously have to change the way I call animate, but how? [import]uid: 106739 topic_id: 18629 reply_id: 71569[/import]

I’m not sure but I think you ought to do something like this in your animate function
[lua]transition.to(self,params)[/lua]

BTW you can send all the transition parameters when calling the animate function and use the params table as such in the transition.to of your animate function. No need to specify the parameters twice.
[import]uid: 64174 topic_id: 18629 reply_id: 71595[/import]

Really need some help with this one guys, how do i call the function with the distance meyhod right, so it works? [import]uid: 106739 topic_id: 18629 reply_id: 71706[/import]

Really need some help with this one guys, how do i call the function with the distance meyhod right, so it works? [import]uid: 106739 topic_id: 18629 reply_id: 71707[/import]

[code]

local myDistance = distance(myX, myY, targetX, targetY)

transition.to(object, {time = myDistance})

[/code] [import]uid: 84637 topic_id: 18629 reply_id: 71727[/import]

Really need some help with this one guys, how do i call the function with the distance method right, so it works?

EDIT: Sorry about the triple(!) post, don’t know what happened there, internet must’ve confused itself. [import]uid: 106739 topic_id: 18629 reply_id: 71708[/import]

@Danny, it complains: Runtime error: attempt to call global ‘distance’ (a nil value)

Even though I have the require for the module in main.lua [import]uid: 106739 topic_id: 18629 reply_id: 71831[/import]

@Danny, it complains: Runtime error: attempt to call global ‘distance’ (a nil value)

Even though I have the require for the module in main.lua

EDIT: Excuse the double posts, I have no clue why that is happening. [import]uid: 106739 topic_id: 18629 reply_id: 71832[/import]