Creating a trail behind a moving object

Hi,

I was wondering if it is currently possible to create a motion trail behind a moving object. For example, if I shot a cannonball (with physics on) at a vector towards a wall and wanted to show the path developing as it launches, bounces around and eventually comes to a halt.

I know I’ve seen examples of this done in flash and such, but of course I can’t find them currently. Harrumph! Still, I think I’ve explained the concept clearly.

My best guess is that the solution would be using bezier lines somehow… but I’m scratching my head as to how to do it. If anyone has any ideas, or a suggestion of where to start, I would greatly appreciate it!

Thanks! [import]uid: 37168 topic_id: 8277 reply_id: 308277[/import]

I believe this is done in the Ghosts vs Monsters code (can’t thank jonbeebe enough!)

http://developer.anscamobile.com/code/ghosts-vs-monsters

Look for the trail that’s made after the ghost is launched. [import]uid: 35852 topic_id: 8277 reply_id: 29543[/import]

Hmm, interesting… That might work! Thanks!
Is there any way to do this as solid line, though? [import]uid: 37168 topic_id: 8277 reply_id: 29546[/import]

Take a look at, http://developer.anscamobile.com/content/displaynewline I think the Martian Control sample also shows how lines are drawn by connecting the points between touch events. Carlos also put together a point reduction sample together in the Code Exchange that could be used to create nice lines with bezier curves. [import]uid: 27183 topic_id: 8277 reply_id: 29591[/import]

Hey again,

I think I’ve pieced together what I was looking for after looking at those examples and a little experimenting myself. There’s still some bugs, but I thought I’d post it in case it helps someone else. The code I’m posting is based on the physics example program “HelloPhysics,” so you just copy and paste it into its main.lua if you want to tinker with it:

  
local physics = require( "physics" )  
physics.start()  
  
local sky = display.newImage( "bkg\_clouds.png" )  
sky.x = 160; sky.y = 195  
  
local ground = display.newImage( "ground.png" )  
ground.x = 160; ground.y = 445  
  
physics.addBody( ground, "static", { friction=0.5, bounce=0.3 } )  
  
local crate = display.newImage( "crate.png" )  
crate.x = 180; crate.y = -50; crate.rotation = 2  
  
local cratePosX = crate.x --\>creates a varible and sets it to the current x position of the crate  
local cratePosY = crate.y --\>creates a varible and sets it to the current y position of the crate  
physics.addBody( crate, { density=3.0, friction=0.5, bounce=0.3 } )  
  
crate:applyForce( 300,0 , cratePosX,cratePosY ) -- applys force to the crate  
  
  
local drawLine = display.newLine( cratePosX, cratePosY , cratePosX, cratePosY ) --\> sets and draws the initial point of the Motion Trail  
 drawLine.width = 2  
 drawLine:setColor( 0, 0, 0 )  
  
  
  
local function drawMotionLine( event ) --\> draws the Motion Trail  
 local cratePosX = crate.x --\> updates the variable to the current x position  
 local cratePosY = crate.y --\> updates the variable to the current y position  
  
 local useslessFiller = display.newText( "", 0, 0) --\> for some reason, I need this in there to make the following code work... not sure why.  
 drawLine:append( cratePosX,cratePosY) --\>draws a newline from the end of the last line to the current position of the crate  
  
end  
  
Runtime:addEventListener( "enterFrame", drawMotionLine ); --\> starts the Motion Trail function  
  

The main problem I’m having is that the effect only seems to work when I have displayed text (the “uselessFiller” part of the code) in front of it. I’m not sure if this is an error or if I’m just coding sloppily (probably the case. I’m relatively new to this sort of programming).

There’s another issue that occasionally shows up if you mess around with the force or the rotation variables. The line kind of messes up… it’s hard to explain, but I think if you set crate.rotation to 3 you will see what I mean. Again, it could be an error, poor coding on my part, or just a weird system hiccup.

Does anyone know how I might fix these issues? [import]uid: 37168 topic_id: 8277 reply_id: 29652[/import]

Hi pemling,

This seems to work, but the line rendering is really bad when the width is set to something large. Can anyone shed some light on why?

[code]

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

local sky = display.newImage( “bkg_clouds.png” )
sky.x = 160; sky.y = 195

local ground = display.newImage( “ground.png” )
ground.x = 160; ground.y = 445

physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 } )

local crate = display.newImage( “crate.png” )
crate.x = 180; crate.y = -50; crate.rotation = 5

physics.addBody( crate, { density=3.0, friction=0.5, bounce=0.3 } )
local points = { {crate.x, crate.y}}
local drawLine = nil

local function drawMotionLine( event ) --> draws the Motion Trail

if drawLine then
drawLine:removeSelf()
end

table.insert(points, 1, {crate.x, crate.y})

if #points == 20 then
points[#points] = nil
end

if #points > 1 then
drawLine = display.newLine(points[1][1], points[1][2], points[2][1], points[2][2])
drawLine:setColor(0x33, 0xcc, 0, 155)
drawLine.width = 4
end

if #points > 2 then
for i = 3, #points do
drawLine:append(points[i][1], points[i][2])
end
end

end

Runtime:addEventListener(‘enterFrame’, drawMotionLine)
[/code] [import]uid: 27183 topic_id: 8277 reply_id: 29689[/import]

Hi pemling,

Looks like the append function is not realtime so you need to redraw the entire line every frame like in my sample code based on this forum post: http://developer.anscamobile.com/forum/2010/12/15/displaylineappend-doesnt-work-realtime-unless-linewidth-set-eg-linewidth00000001
[import]uid: 27183 topic_id: 8277 reply_id: 29692[/import]