objects(slave) who follow an object(master) with delay

Hi guys.

in my game I have a ball table. I wish that by moving a ball the others would follow it with a little delay.

I’ve tried various solutions but I think the best idea is something with math.

small basic code:

local balls = {} for i = 1, 7 do local ball = display.newCircle( 0, 250, 14) ball.x = 40\*i ball:setFillColor( math.random(), math.random(), math.random() ) function ball:touch(event) local phase = event.phase if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( self ) self.x0 = self.x --self.y0 = self.y print("began") elseif( self.isFocus ) then if(phase == "moved")then local dx = event.x - event.xStart --local dy = event.y - event.yStart self.x = self.x0 + dx --self.y = self.y0 + dy event.dx = dx --event.dy = dy print("move") elseif(phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( self ) print("ended") end end return true end ball:addEventListener( "touch" ) balls[i] = ball end

Looking on the internet I found a famous game. I would like to be able to achieve an equal movement effect.

https://www.youtube.com/watch?v=ifuOYkIqH6k

(at time 1:30-1:38 if necessary, look in slow motion)

As you can see the objects do not follow the main at the same speed but with a slight delay that gives a nice effect

I managed to do something similar without math, using the timer but I think there is something better. Furthermore, the effect does not satisfy me yet:

local balls = {} for i = 1, 7 do local ball = display.newCircle( 0, 250, 14) ball.x = 40\*i ball:setFillColor( math.random(), math.random(), math.random() ) ball.id = i function ball:touch(event) local phase = event.phase if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( self ) self.x0 = self.x --self.y0 = self.y for i =1,#balls do balls[i].x0 = balls[i].x end print("began") elseif( self.isFocus ) then if(phase == "moved")then local dx = event.x - event.xStart --local dy = event.y - event.yStart self.x = self.x0 + dx --self.y = self.y0 + dy event.dx = dx --event.dy = dy for n =1,#balls do if(balls[n].id ~= self.id)then local num = math.abs(n-self.id) timer.performWithDelay( 40\*num, function() local dx = event.x - event.xStart --local dy = event.y - event.yStart balls[n].x = balls[n].x0 + dx --self.y = self.y0 + dy end,1) end end print("move") elseif(phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( self ) print("ended") end end return true end ball:addEventListener( "touch" ) balls[i] = ball end

if the movement is too abrupt, the elements overlap

Does anyone have an idea for this?

I do have ideas, but I can almost be 100% sure, they are not doing what you think they are in that game.  I think you’re guessing at how that move was solved and the guess doesn’t work well for the game type.

i.e. The pieces are each moving independently of the one before them.  I am very sure each piece has its own movement path calculated and timed as part of the ‘drop in pieces’ algorithm.

Using a follow the leader approach would not fit well with the other constraints:

  • end on a grid position
  • know what grid you landed on
  • have the board know the spot is filled
  • all kinds of other effects going on, including jiggles, upscale, downscale, curve following.
    • Doing these things by observing the path and the behavior of the prior object is prohibitive and will not work due to timing issues.
    • The only way to get consistent look-and-feel is to move each piece individually according to its own calculated times and then using multiple tween-style (transition.*) controllers (per-piece) to handle all the changes to the piece.

I have and old ‘follow the leader’ example: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/05/followTheLeader.zip

  • One of many ways to do this.
  • Again, not really what is going on in that game.

https://www.youtube.com/watch?v=1H4jATyCbew

Thank you @roaminggamer

As you said it’s not really what I want. I had already seen and analyzed your project before. I’m always up to date with your examples are very useful.

I also watched the project “swarm” of @StarCrunch and I adapted the code, but again I did not get what I wanted.

He began to believe it was much harder than expected :wacko:

Yeah.  I’m sorry to say, but what I see in the video is pretty hard looking.

The way I would initially try it is, for every piece that is coming out on the board in that sequence…

  • determine end target
  • determine path.
  • calculate duration of complete move as a factor of path length and preferred pixel speed.
  • Follow the path using a transition to control the speed.  This way the speed doesn’t need to be linear.
  • Decide on what juicy effects I want: Shrinks, jiggles, grows, etc.
  • For each juicy effect add code driven by a transition (again to control time attack) as well as a transition to control the effect it that is suitable.

In the end, each moving piece will have many overlapping transition.* calls and some sequential transition.* calls.

Oh, and when I got it all working.  I’d probably feel it was too ‘heavy’ and want to optimize it.

Then I wouldn’t, not until I’d determined I really needed to.

One last note.  I hope I wasn’t abrasive in my original response.

My big point was, “we often see what we want to see and/or what our experience lets us see”.

I think there are many ways to get that effect.  

@roaminggamer

Thank you so much for your time. Actually some of your passages are the ones I was trying to do.

Every help is a great help right now.

However I do not know if I can create an effect similar to that. And even if I succeeded as you said then there would be the problem of performance that is not to be neglected.

I’ll make some other attempts but with my math I’m starting to have doubts. However, if I have news, I’ll let you know!

I managed to do something similar without math, using the timer but I think there is something better. Furthermore, the effect does not satisfy me yet:

local balls = {} for i = 1, 7 do local ball = display.newCircle( 0, 250, 14) ball.x = 40\*i ball:setFillColor( math.random(), math.random(), math.random() ) ball.id = i function ball:touch(event) local phase = event.phase if( phase == "began" ) then self.isFocus = true display.currentStage:setFocus( self ) self.x0 = self.x --self.y0 = self.y for i =1,#balls do balls[i].x0 = balls[i].x end print("began") elseif( self.isFocus ) then if(phase == "moved")then local dx = event.x - event.xStart --local dy = event.y - event.yStart self.x = self.x0 + dx --self.y = self.y0 + dy event.dx = dx --event.dy = dy for n =1,#balls do if(balls[n].id ~= self.id)then local num = math.abs(n-self.id) timer.performWithDelay( 40\*num, function() local dx = event.x - event.xStart --local dy = event.y - event.yStart balls[n].x = balls[n].x0 + dx --self.y = self.y0 + dy end,1) end end print("move") elseif(phase == "ended" or phase == "cancelled" ) then self.isFocus = false display.currentStage:setFocus( self ) print("ended") end end return true end ball:addEventListener( "touch" ) balls[i] = ball end

if the movement is too abrupt, the elements overlap

Does anyone have an idea for this?

I do have ideas, but I can almost be 100% sure, they are not doing what you think they are in that game.  I think you’re guessing at how that move was solved and the guess doesn’t work well for the game type.

i.e. The pieces are each moving independently of the one before them.  I am very sure each piece has its own movement path calculated and timed as part of the ‘drop in pieces’ algorithm.

Using a follow the leader approach would not fit well with the other constraints:

  • end on a grid position
  • know what grid you landed on
  • have the board know the spot is filled
  • all kinds of other effects going on, including jiggles, upscale, downscale, curve following.
    • Doing these things by observing the path and the behavior of the prior object is prohibitive and will not work due to timing issues.
    • The only way to get consistent look-and-feel is to move each piece individually according to its own calculated times and then using multiple tween-style (transition.*) controllers (per-piece) to handle all the changes to the piece.

I have and old ‘follow the leader’ example: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/05/followTheLeader.zip

  • One of many ways to do this.
  • Again, not really what is going on in that game.

https://www.youtube.com/watch?v=1H4jATyCbew

Thank you @roaminggamer

As you said it’s not really what I want. I had already seen and analyzed your project before. I’m always up to date with your examples are very useful.

I also watched the project “swarm” of @StarCrunch and I adapted the code, but again I did not get what I wanted.

He began to believe it was much harder than expected :wacko:

Yeah.  I’m sorry to say, but what I see in the video is pretty hard looking.

The way I would initially try it is, for every piece that is coming out on the board in that sequence…

  • determine end target
  • determine path.
  • calculate duration of complete move as a factor of path length and preferred pixel speed.
  • Follow the path using a transition to control the speed.  This way the speed doesn’t need to be linear.
  • Decide on what juicy effects I want: Shrinks, jiggles, grows, etc.
  • For each juicy effect add code driven by a transition (again to control time attack) as well as a transition to control the effect it that is suitable.

In the end, each moving piece will have many overlapping transition.* calls and some sequential transition.* calls.

Oh, and when I got it all working.  I’d probably feel it was too ‘heavy’ and want to optimize it.

Then I wouldn’t, not until I’d determined I really needed to.

One last note.  I hope I wasn’t abrasive in my original response.

My big point was, “we often see what we want to see and/or what our experience lets us see”.

I think there are many ways to get that effect.  

@roaminggamer

Thank you so much for your time. Actually some of your passages are the ones I was trying to do.

Every help is a great help right now.

However I do not know if I can create an effect similar to that. And even if I succeeded as you said then there would be the problem of performance that is not to be neglected.

I’ll make some other attempts but with my math I’m starting to have doubts. However, if I have news, I’ll let you know!