Move object on path

how to move object over a path, with saving the speed and change the angle of the rotation, the path is created by drawing it using touch event.

I tried to use transition.to function, but the problem is the distance between the points not fixed, so transition function will make different speed between each two points, also what should i do with rotating the object (heading).

please help :frowning: [import]uid: 79884 topic_id: 15108 reply_id: 315108[/import]

i am facing the same problem.rasheed and really i want code sample
to simulate the object movement on a certain path [import]uid: 74537 topic_id: 15108 reply_id: 55882[/import]

hope this helps
http://developer.anscamobile.com/code/move-object-through-path
[import]uid: 71210 topic_id: 15108 reply_id: 55888[/import]

There are so many ways to skin a cat…
An easy way to do what you are attempting without heavy mathmatics might be to use a timer and every so many ticks grab the users x,y coordinates into a buffer. Then have another timer set to read from this buffer of coordinates and use transition.to to move the object… just food for thought

EDIT: lol…kinda like the linky above…:slight_smile: [import]uid: 21331 topic_id: 15108 reply_id: 55889[/import]

this will not work with me, i need to move it in constant speed, and rotate the object with the carve. like the robot in this video

http://www.youtube.com/watch?v=SNm8bArIZww&feature=related

[import]uid: 79884 topic_id: 15108 reply_id: 55990[/import]

i am stuck in this problem tooo [import]uid: 74537 topic_id: 15108 reply_id: 56030[/import]

i have the same problem too , y isn’t there a way to do it just like we do with flash , i need a solution plz !!! [import]uid: 86096 topic_id: 15108 reply_id: 56033[/import]

hi renvis, I used this code too, but the speed looks funny when the object is moving in a curve. like…it move fast in a straight line but move slow in a curve. [import]uid: 38556 topic_id: 15108 reply_id: 56040[/import]

NOTE: I have not looked at these examples, so what I’m about to say may be purely speculative nonsense.

But from hat your describing and from the move along a path code samples I’ve seen, it seems to me that there are more points involved in drawing a curve than the line. Perhaps during the part where you draw the line more points are being captured during the curve than the straight line. So if you move from x1,y1 to x2,y2 over time T then try to move from x2,y2 to x3,y3 which is a shorter distance and you’re still using time T, that second move will be faster.

You have to factor the distance between x1,y1 and x2,y2 into your transition timer.

So you could as your moving along calculate the distance:

dX = math.abs(x1-x2)  
dY = maty.abs(y1-y2)  
d = math.sqrt((dX\*dX)+(dY\*dY))  
transition.to(object, {time=speed\*d, x=x2, y=y2})  

That should address the problem being described. Of course you have to figure out speed and if d needs scaled any.
[import]uid: 19626 topic_id: 15108 reply_id: 56053[/import]

thanks all …

hope anybody write to us sample code that demonstrate the object movement through (curved) path ,take the rotation of object in eye also ,this will help any developer stuck in the same problem [import]uid: 74537 topic_id: 15108 reply_id: 56075[/import]

thanks all …

hope anybody write to us sample code that demonstrate the object movement through (curved) path ,take the rotation of object in eye also ,this will help any developer stuck in the same problem [import]uid: 74537 topic_id: 15108 reply_id: 56076[/import]

Thats pretty easy too:

local atan2 = math.atan2  
local pi = math.pi  
  
deltax = point2.x - point1.x  
deltay = point2.y - point1.y  
  
object.rotation = atan2(deltay,deltax) \* 180.0 / pi  
  

[import]uid: 19626 topic_id: 15108 reply_id: 56084[/import]

Thank you robmiracle . [import]uid: 74537 topic_id: 15108 reply_id: 56098[/import]

the object move through the path at a speed with which you draw the line. try drawing the line at two different speeds and see what happens. for rotation you can easily incorporate the code that robmiracle had given. thanks @robmiracle.
[import]uid: 71210 topic_id: 15108 reply_id: 56109[/import]

it will be something like this. you need to tweak it for perfect movement and rotation.
[lua]local path = {}
local i =1;local j =1
local atan2 = math.atan2
local pi = math.pi

local function traceClicks(event)
if event.phase == “began” then
print(“reset”)
path ={}
i = 1
elseif event.phase == “moved” then
path[i] = {}
path[i].x = event.x
path[i].y = event.y
print(path[i].x)
i=i+1
end
end
Runtime:addEventListener(“touch”, traceClicks)

–create a circle ball
local ball = display.newText(“B”, 150, 150, nil,40 )

local animate
local posCount = 1
local prevx = 0
local prevy = 0

–moving the object – this is where animation takes place
local function moveObject(event)
if posCount <= #path then

if prevx == 0 then prevx = path[posCount].x ; prevy = path[posCount].y end
local deltax = path[posCount].x - prevx
local deltay = path[posCount].y - prevy
ball.rotation = atan2(deltay,deltax) * 180.0 / pi

ball.x = path[posCount].x
ball.y = path[posCount].y
prevx = path[posCount].x
prevy = path[posCount].y
posCount = posCount + 1
else
timer.cancel(animate)
end
end

– to move ball when we click the animate text
local function move()
posCount = 1
animate = timer.performWithDelay( 10, moveObject, #path )
return true
end

–create a text which when clicked will animate the object
local text = display.newText( “Animate!”, 220, 50, nil, 40 )
text:addEventListener(“touch”, move)[/lua] [import]uid: 71210 topic_id: 15108 reply_id: 56111[/import]

Thank you renvis@technowand for your sharing …

but if you speed up , the event.x and event.y will take small number of points then it will be fast movement for that object + how can we control the speed of rotation ? [import]uid: 74537 topic_id: 15108 reply_id: 56175[/import]

This is cool guys. Been looking for this. Anyway to include a trail as you drag you fingers across the screen. Of course it should fade as you drag. Just for kicks!!

Thanks All [import]uid: 53149 topic_id: 15108 reply_id: 56186[/import]

I did not like that you had to draw then click to move. In some cases it works. I wanted to move as soon as I pick fingers up the character moved. My 2 cents :slight_smile:

local function listenForEnd (event)  
 if event.phase == "ended" then  
 print ("Finger Up Now move yo ass")  
 move();  
 end  
end  
  
Runtime:addEventListener("touch", listenForEnd)  
  

Something I noticed… the ball snaps to the first point placed instead of transition from it’s current position to the new event touch or point placed then continuing to the last event registered. Any way to make that smoother? [import]uid: 53149 topic_id: 15108 reply_id: 56188[/import]

Thanks spenggong >>

i want the smoothness in movement also [import]uid: 74537 topic_id: 15108 reply_id: 56201[/import]

Hi everybody, hoping it’s the right place to ask (seems there are some top coders here) :

Id’ like to draw/add images along a simple path (a line), considering a certain distance between each point.
What would be the most efficicent solution.

I had a look on martian control, but it’s a bit hard for me to follow and find what is really neccessary.

I “just” would like to do :

-- I should use x1,y1, x2,y2  
-- space (space between dots)  
-- or numbers (number of dots if space is impossible to deal with)  
for i to n do  
local dot = display.newImage("dot.png, x,y)  
end  

Thx for any hints :slight_smile:
[import]uid: 9328 topic_id: 15108 reply_id: 58845[/import]