Object A follow object B path

Hi,

In my app i have an item with physics that falls on the press of a button. I would like a second item that falls and is attached to the first object.

Can anyone provide me with an example of of how to do this please?

I have looked at a physics join, but it is a bit beyond me at the moment - all help appreciated.

Thank you

[import]uid: 125592 topic_id: 26357 reply_id: 326357[/import]

Your post title and question refer to two different things. Do you want object B to take the same path as object A or be attach (the join?)

The technical answer depends on your situation and what you want to see.

Going on the title you probably want something fairly complex or simply to record the path object A takes and replay that by changing the values of object B over time.

Going with the join you probably just want to look up the distance join. Plenty of examples around and try not to complicate it your head, it’s not as bad as it seems. [import]uid: 8271 topic_id: 26357 reply_id: 106839[/import]

Thanks for the reply

In truth I’m not too sure what i’m after.

I have an object that falls, the second object is pretty much a tail to that first object, so it must appear ‘attached’.

If you can point me in the direction of working examples it would be very helpful.

Thanks again [import]uid: 125592 topic_id: 26357 reply_id: 106886[/import]

Two joint types are probably most useful for this:

The “pivot” joint will attach the 2 bodies at ONE POINT with optional restriction on how much rotation is allowed on that point. Imagine placing two flat boards together and then hammering a nail through them, with the boards being able to rotate around that nail.

The “distance” joint attaches the two bodies by an imaginary line, keeping them the same distance apart during movement… it also allows some spring-like flexibility and damping, but that’s tricky to get the settings correct.

http://developer.anscamobile.com/content/game-edition-physics-joints

Tinkering with joints is largely trial and error. Alot of error in the beginning. :slight_smile: It takes some time before you understand how to properly attach Box2D bodies with the correct joint for each behavior, and set ALL of the parameters correctly so your bodies don’t go flying around or freaking out at random times.

Brent Sorrentino
Ignis Design
[import]uid: 9747 topic_id: 26357 reply_id: 106906[/import]

Well, I would go the other way. Unless the ‘tail’ needs to depart from the path of the original object, I think just using regular display objects and following the A object precisely would do it. Some waggling could be easily be put in there too and wouldn’t need the physics engine. [import]uid: 8271 topic_id: 26357 reply_id: 106974[/import]

Thank you for your replies,

Horacebury can you provide any example code, I think your describing a simple solution, but I’m not sure where to start in implementing it.

Thanks again for your help. I’m pleased that I have not compromised on my app so far, so would like to achieve this.

[import]uid: 125592 topic_id: 26357 reply_id: 106985[/import]

Try this:

main.lua:
[lua]-- tail demo

local physics = require(“physics”)
physics.start()

local a = display.newCircle( 100, 300, 25 )
physics.addBody( a, “dynamic” )

a:applyForce( 5, -8 )

local b = display.newCircle( -100, -100, 10 )
b:setFillColor( 0,255,0 )

local list, dofollow = {}, false

function enterFrame(event)
– record the A object’s location
list[#list+1] = {x=a.x,y=a.y}

– once there is 15 or more frames of location data then we can start following
if (#list >= 15) then
– switch on following
dofollow = true
end

– only bother following when there is a decent distance between the two objects - in this case it is 15 frames worth of data
if (dofollow) then
local t = list[1]
b.x, b.y = t.x, t.y
table.remove( list, 1 )
end
end

Runtime:addEventListener( “enterFrame”, enterFrame )[/lua]
[import]uid: 8271 topic_id: 26357 reply_id: 107002[/import]

Thank you for taking the time to do this, I am looking forward to giving it a try! [import]uid: 125592 topic_id: 26357 reply_id: 107042[/import]