How to make object follow another object?

Hello guys!

I’ve been trying to make an object follow another object.
Someone who would be kind and point me in the right direction? :slight_smile:

Its two physical objects that’s dragable but I want one of the object to constantly follow the other one.

Thanks in advance
/Rickwhy [import]uid: 10657 topic_id: 3241 reply_id: 303241[/import]

Hi Rick,

Pretty much any action that’s “constant” (i.e. constantly following) needs to be tied into the system’s game timer… basically the internal timer that starts when the game starts, and keeps going until the game ends. This timer is firing off rapidly during any game, something like 30+ times per second if I’m not mistaken.

For a follow routine, I think that’s a bit too fast to constantly update two objects’ trajectories. In my current game, I slowed things down somewhat by implementing a timer loop which checks itself every 500 milliseconds or so… half a second per “update” seems plenty, but you could make it faster.

Here’s a sample piece of code… hopefully it helps!

local prevTime = 0 --this is just the "starting" timer loop number  
  
local function timeLoop( event )  
  
 --this compares the system timer against the loop timer  
 --if 500 milliseconds have elapsed since the previous check, perform an action!  
 if ( system.getTimer()-prevTime \>= 500 ) then  
 -- update the object(s) trajectories to make them follow each other  
 prevTime = system.getTimer() --set prevTime to system time for looping purposes  
 end  
 return  
  
end  
  
Runtime:addEventListener( "enterFrame", timeLoop );  

Brent
[import]uid: 9747 topic_id: 3241 reply_id: 9596[/import]

Thank you Brent!

That’s excellent!
For people that looking for the same thing this is how I solved it:

[code]
local function timeLoop( event )

–this compares the system timer against the loop timer
–if 500 milliseconds have elapsed since the previous check, perform an action!
if ( system.getTimer()-prevTime >= 1500 ) then
– update the object(s) trajectories to make them follow each other
transition.to( b2, { time=1500, x=b1.x, y=b1.y, transition = easingx.easeInOut} )
prevTime = system.getTimer() --set prevTime to system time for looping purposes
end
return

end

Runtime:addEventListener( “enterFrame”, timeLoop ); [import]uid: 10657 topic_id: 3241 reply_id: 9601[/import]

If you are wanting to make one object follow the other, exactly I would put the code for that object’s movement in the same function that controls the movement of the master object.

That was you only have one listener [import]uid: 5354 topic_id: 3241 reply_id: 9661[/import]

yeah but I want it to chase the master object that I control with flick and touch! [import]uid: 10657 topic_id: 3241 reply_id: 9665[/import]

so what was the easingx.easeInOut thing about? I’m trying to do the same thing that you’re doing. [import]uid: 54001 topic_id: 3241 reply_id: 35445[/import]

How do you get the chaser to turn towards the chasee in this case the Master Object? I could use this and thanks to IgnisDesign and rickwhy for sharing.

if() then  
 -- object.xScale = -1  
elseif() then  
 -- object.xScale = 1  
end  

Can anyone post a working sample of how to get the chaser object to always look at the object being chased…

Thanks [import]uid: 53149 topic_id: 3241 reply_id: 38064[/import]

I changed time with this
timeTaken = distance/speed * 1000 with speed a fixed value
time = timeTaken
distance is based on the chaser and chasee x and y and the only value that changes constantly
but in my case the chaser only move once to the first position of the chasee once the chasse moves the chaser does not follow
Is there a reason why it only works when time = “fixed number” [import]uid: 43696 topic_id: 3241 reply_id: 38525[/import]

I posted this in another thread. It uses vector subtraction (target - chaser) to set the chaser’s velocity.

[code]
local physics = require(“physics”)
physics.start()

local redOrb = display.newImage(“RedGlass.png”)
local yellowOrb = display.newImage(“YellowGlass.png”)
local SPEED = 0.3

– put redOrb at top left
redOrb.x = redOrb.contentWidth/2
redOrb.y = redOrb.contentHeight/2

– put yellowOrb middle right
yellowOrb.x = display.contentWidth - yellowOrb.contentWidth/2
yellowOrb.y = display.contentHeight/2

physics.addBody(redOrb, “kinematic”,
{ friction=0.0, bounce=0.0, density=0.0, radius=redOrb.contentWidth/2.0 }
)

physics.addBody(yellowOrb, “kinematic”,
{ friction=0.0, bounce=0.0, density=0.0, radius=yellowOrb.contentWidth/2.0 }
)

– move towards the left at constant velocity
yellowOrb:setLinearVelocity(-15, 0)

function gameLoop(event)
– constantly adjust velocity to track yellowOrb
redOrb:setLinearVelocity(
SPEED * (yellowOrb.x - redOrb.x),
SPEED * (yellowOrb.y - redOrb.y)
)
end

Runtime:addEventListener(“enterFrame”, gameLoop)
[/code] [import]uid: 58455 topic_id: 3241 reply_id: 40893[/import]

Oh my gosh. I think you guys over think some things a little too much. The objective should be to do things in few lines of code as possible. All you have to do is look into the joint API for corona.

Here is what the distant joint example looks like:
myJoint = physics.newJoint( “distance”, crateA, crateB, crateA.x,crateA.y, crateB.x,crateB.y )

No matter where the crateA goes, the crateB will follow.

Check out the joins API:
http://developer.anscamobile.com/resources/apis

It has all the stuff you need for adding joints and joint details.

If you wanted the joint to get closer then just remove the joint and decrease distance every 2 seconds with multiple timers. This is just an idea and I guess you can expand on it. [import]uid: 54001 topic_id: 3241 reply_id: 41057[/import]

Good stuff. I posted this code in another thread with the disclaimer that there may be a way to do it with the API, but that I am new to Corona so I wasn’t sure. Thanks for the info. [import]uid: 58455 topic_id: 3241 reply_id: 41063[/import]