Drag an object along a specific path

  • [import]uid: 148623 topic_id: 35544 reply_id: 143644[/import]

Ok, I see now.

You could start with making a number of shapes that are built in certain paths/trails (like a bit of the Tiled maze example that I posted) and add them into their own group; when you want to rotate them, rotate the group. Or even make physics bodies that accomodate the ball/cube/whatever that you’re trying to move and use them.

C [import]uid: 147322 topic_id: 35544 reply_id: 144158[/import]

I have the rotating platforms with an image of what I want to use for the ‘track’ which rotates accordingly when I rotate the circular platform… The only two things I don’t have “coded” yet, are getting the ball to rotate if it is on the circular platfrom (I’m getting close to working that out), and getting the ball to follow the track when dragged.

Using your Tiled example, would I just place my ‘track’ objects where I want them and then code invisible walls to keep the ball moving steadily along the ‘track’?

-Saer
[import]uid: 148623 topic_id: 35544 reply_id: 144218[/import]

  • [import]uid: 148623 topic_id: 35544 reply_id: 144358[/import]

You could also try level director (www.retrofitproductions.com/level-director) as this allows you to create a bezier path and exports the points for you.
I actually use the curves with physics but it does contain an example that has a ball follow the path so it might be of some help to you.
[import]uid: 158620 topic_id: 35544 reply_id: 145123[/import]

Quite right - you’d create as many “track” physics bodies as you need and add the track image to a group with that physics body attached to it.

When your player enters the track, add the player to that group and rotate the group.

At least I think that would work… You might get a problem with different-group physics.

C [import]uid: 147322 topic_id: 35544 reply_id: 144267[/import]

Ok, I see what you mean. (By the way… I love The Room :slight_smile: )

So do you have a physics shape for that circle path? That’s what you’re going to need - a physics shape that “closes in” all of the paths, just like my tiled example has. You might want to go for a smaller size physics path, though.

C [import]uid: 147322 topic_id: 35544 reply_id: 145164[/import]

Hey, I’m not sure if this is what you’re looking for now, because I’ve not read the whole conversation thread, but based on the thread title does this help? You’ll need the mathlib from here:

https://developer.coronalabs.com/code/maths-library

Tap on the screen to create points along a line. Touch and drag around the screen to have the closest point on the line highlighted.

If this is what you’re looking for but you want it with physics, let me know and I’ll make the appropriate adjustements.

main.lua:
[lua]
– move along path

require(“mathlib”)

– physics not doing anything yet
require(“physics”)
physics.start()
physics.setGravity(0,10)
physics.setDrawMode(“hybrid”)

sWidth, sHeight = display.contentWidth, display.contentHeight

gravity = {x=0,y=10}

dots = display.newGroup()
lines = display.newGroup()
tracker = display.newGroup()
tracker.line = display.newGroup()
tracker.dot = display.newCircle(tracker,0,0,10)

function tap(e)
local dot = display.newCircle(dots,e.x,e.y,15)
dot:setFillColor(0,0,0,0)
dot:setStrokeColor(0,255,0)
dot.strokeWidth = 5

while (lines.numChildren > 0) do
lines[1]:removeSelf()
end

if (dots.numChildren > 1) then
for i=2, dots.numChildren do
local line = display.newLine(lines,dots[i-1].x,dots[i-1].y,dots[i].x,dots[i].y)
line.width = 5
line:setColor(0,0,255)
end
end
return true
end
Runtime:addEventListener(“tap”,tap)

function findClosestPoint(e)
local closestDist = 1000000
local closestPt = nil

for i=2, dots.numChildren do
local pt = GetClosestPoint( dots[i-1], dots[i], e )
local len = lengthOf( pt, e )

if (len < closestDist) then
closestDist = len
closestPt = pt
end
end

return closestPt
end

function refreshTrack(e)
local pt = findClosestPoint(e)
tracker.dot.x, tracker.dot.y = pt.x, pt.y

tracker.line:removeSelf()
tracker.line = display.newLine(tracker,e.x,e.y,pt.x,pt.y)
end
function touch(e)
if (e.phase == “began”) then
tracker.alpha = 1
refreshTrack(e)
elseif (e.phase == “moved”) then
refreshTrack(e)
else
tracker.alpha = 0
end
return true
end
Runtime:addEventListener(“touch”,touch)
[/lua] [import]uid: 8271 topic_id: 35544 reply_id: 145182[/import]

Ahh, I see. cool! :smiley:

Thanks, I made some changes and it works now.

Pretty much. It is a maze game that has a small line/track that the ball moves along, and there are some platforms that can rotate to connect with a different piece of track so the ball can continue until it reaches the goal/end-point etc… etc…
I have almost the entire game done (menus, scenes, sounds, and such ) and the only thing stopping me from finishing it is this whole ball-follow-path thing lol.

Actually, that was my first thought - create invisible rects that would border the entire track&platforms.
I would think you’d have to have the rect-borders partially overlap the the ball so there wouldn’t be any odd movement - such as the ball not moving in a straight line - when the user is dragging it…?

I’d very much appreciate your help with it. :slight_smile:

Would Tiled be a better choice over LevelHelper?

-Saer

[import]uid: 148623 topic_id: 35544 reply_id: 142679[/import]

If you’d prefer, feel free to email and/or skype me sometime.
Tdg_Photography@yahoo.com
Skype User-Name: Saerothir [import]uid: 148623 topic_id: 35544 reply_id: 142710[/import]

I chose Tiled because a) it’s simpler to use, b) we don’t need it to be extremely complicated, and c) it can do things extremely exactly.

So to start with, build a Tiled map. The tileset doesn’t really matter. It could even just be two images - a wall and your character.

Then, in your code, do an iteration so that if the data is 1 build a wall at that position, if the data is 2 build the character at that position.

There’s a simple framework.

I’ll build some code if you want me to :slight_smile:

Caleb [import]uid: 147322 topic_id: 35544 reply_id: 142921[/import]

  • [import]uid: 148623 topic_id: 35544 reply_id: 143644[/import]
  • Sorry for the delayed response -

Okay, sounds good.
I haven’t used Tiled before, so your assistance would be more than welcome! :smiley:

-Saer [import]uid: 148623 topic_id: 35544 reply_id: 143430[/import]

  • [import]uid: 148623 topic_id: 35544 reply_id: 144358[/import]

You could also try level director (www.retrofitproductions.com/level-director) as this allows you to create a bezier path and exports the points for you.
I actually use the curves with physics but it does contain an example that has a ball follow the path so it might be of some help to you.
[import]uid: 158620 topic_id: 35544 reply_id: 145123[/import]

Ok, I see what you mean. (By the way… I love The Room :slight_smile: )

So do you have a physics shape for that circle path? That’s what you’re going to need - a physics shape that “closes in” all of the paths, just like my tiled example has. You might want to go for a smaller size physics path, though.

C [import]uid: 147322 topic_id: 35544 reply_id: 145164[/import]

Hey, I’m not sure if this is what you’re looking for now, because I’ve not read the whole conversation thread, but based on the thread title does this help? You’ll need the mathlib from here:

https://developer.coronalabs.com/code/maths-library

Tap on the screen to create points along a line. Touch and drag around the screen to have the closest point on the line highlighted.

If this is what you’re looking for but you want it with physics, let me know and I’ll make the appropriate adjustements.

main.lua:
[lua]
– move along path

require(“mathlib”)

– physics not doing anything yet
require(“physics”)
physics.start()
physics.setGravity(0,10)
physics.setDrawMode(“hybrid”)

sWidth, sHeight = display.contentWidth, display.contentHeight

gravity = {x=0,y=10}

dots = display.newGroup()
lines = display.newGroup()
tracker = display.newGroup()
tracker.line = display.newGroup()
tracker.dot = display.newCircle(tracker,0,0,10)

function tap(e)
local dot = display.newCircle(dots,e.x,e.y,15)
dot:setFillColor(0,0,0,0)
dot:setStrokeColor(0,255,0)
dot.strokeWidth = 5

while (lines.numChildren > 0) do
lines[1]:removeSelf()
end

if (dots.numChildren > 1) then
for i=2, dots.numChildren do
local line = display.newLine(lines,dots[i-1].x,dots[i-1].y,dots[i].x,dots[i].y)
line.width = 5
line:setColor(0,0,255)
end
end
return true
end
Runtime:addEventListener(“tap”,tap)

function findClosestPoint(e)
local closestDist = 1000000
local closestPt = nil

for i=2, dots.numChildren do
local pt = GetClosestPoint( dots[i-1], dots[i], e )
local len = lengthOf( pt, e )

if (len < closestDist) then
closestDist = len
closestPt = pt
end
end

return closestPt
end

function refreshTrack(e)
local pt = findClosestPoint(e)
tracker.dot.x, tracker.dot.y = pt.x, pt.y

tracker.line:removeSelf()
tracker.line = display.newLine(tracker,e.x,e.y,pt.x,pt.y)
end
function touch(e)
if (e.phase == “began”) then
tracker.alpha = 1
refreshTrack(e)
elseif (e.phase == “moved”) then
refreshTrack(e)
else
tracker.alpha = 0
end
return true
end
Runtime:addEventListener(“touch”,touch)
[/lua] [import]uid: 8271 topic_id: 35544 reply_id: 145182[/import]

Ahh, I see. cool! :smiley:

Thanks, I made some changes and it works now.

Pretty much. It is a maze game that has a small line/track that the ball moves along, and there are some platforms that can rotate to connect with a different piece of track so the ball can continue until it reaches the goal/end-point etc… etc…
I have almost the entire game done (menus, scenes, sounds, and such ) and the only thing stopping me from finishing it is this whole ball-follow-path thing lol.

Actually, that was my first thought - create invisible rects that would border the entire track&platforms.
I would think you’d have to have the rect-borders partially overlap the the ball so there wouldn’t be any odd movement - such as the ball not moving in a straight line - when the user is dragging it…?

I’d very much appreciate your help with it. :slight_smile:

Would Tiled be a better choice over LevelHelper?

-Saer

[import]uid: 148623 topic_id: 35544 reply_id: 142679[/import]

So here’s an example I put together:

https://www.dropbox.com/s/a3mb52ihagdlfqp/TiledMapDemo.zip

Let me know if there’s something you don’t understand :slight_smile:

C [import]uid: 147322 topic_id: 35544 reply_id: 143636[/import]

If you’d prefer, feel free to email and/or skype me sometime.
Tdg_Photography@yahoo.com
Skype User-Name: Saerothir [import]uid: 148623 topic_id: 35544 reply_id: 142710[/import]