You almost got it there dude, here is a piece of code that is working as you want:
--constant you can modify local SPEED\_BALL = 10 local RADIUS\_BALL = 20 local STEP = 3 local TRAIL\_FADE\_DURATION = 500 local ball = display.newCircle(0, 0, RADIUS\_BALL) ball.vx = SPEED\_BALL ball.vy = SPEED\_BALL ball:setFillColor(1,0,0) local onUpdate = function(e) local A = {x = ball.x, y = ball.y}--previous position --move the ball ball.x = ball.x + ball.vx ball.y = ball.y + ball.vy local B = {x = ball.x, y = ball.y}--new position --get the distance local dx = B.x - A.x local dy = B.y - A.y local dist = math.sqrt(dx\*dx + dy\*dy) --draw trail particles local nbIterations = math.floor(dist / STEP) for i=0, nbIterations do local xx = A.x + i \* STEP \* dx/dist local yy = A.y + i \* STEP \* dy/dist local trailParticle = display.newCircle(xx, yy, RADIUS\_BALL) transition.to(trailParticle, {time = TRAIL\_FADE\_DURATION, alpha = 0, onComplete = function() trailParticle:removeSelf() end}) end --bounce on arena bounds if ball.x \> 320 then ball.x = 320 ball.vx = -ball.vx end if ball.x \< 0 then ball.x = 0 ball.vx = -ball.vx end if ball.y \> 480 then ball.y = 480 ball.vy = -ball.vy end if ball.y \< 0 then ball.y = 0 ball.vy = -ball.vy end --display ball over trail ball:toFront() end Runtime:addEventListener("enterFrame", onUpdate)
if you’re interpolating the trail positions separately from the ball’s position, you’re likely to get weird results whenever the ball bounces off a wall (because the shadows interpolate assuming full vx/vy were applied, but wall prevented that). to solve: interpolate ball’s position in smaller dt’s, creating trails as you go.
when bouncing (aka reflecting) off walls you shouldn’t clamp position, because you’ll lose velocity. (player will see it as a “stutter”) work it out in your head: say x=473, xmax=480, vx=10. after a full time step x would be 483, but reflects back to 477, so that all vx=10 is accounted for. pseuducode is “if (x > xmax) then x=xmax-(x-xmax) end” and similarly for other walls.
@naveen_pcs I might have a go at it, though some ideas will need time to stew. Also it’s not clear if the curve logic (most likely curated from those modules linked earlier) should be included or a separate plugin it would build on. Anyhow, I’ve got a list to whittle down first, things like Clipper…
@davebollinger…and it sounded like you might have been alluding to Cosmin’s wrapper in Luapower , which was where I first discovered it. I think I’ll have to adapt that sample. :) Originally I meant to hew more closely to his API, but there seems to have been some drift since then in Clipper itself. I should probably send stuff like this along to LuaRocks too, at some point.
A follow-up about the Clipper plugin mentioned above. I’ve been putting together some examples for it and am looking to submit in another week or so. It will probably land along with msquares (whose own samples I’m meaning to spruce up) around the same time.
Sample so far, with maybe two or three more ideas pending:
@Sig.g1 I haven’t submitted yet, but might over the weekend. (Lately I’ve managed to distract myself binding this and this.) You should be able to see the sample’s code through the Clipper link above, though. There were a couple other cases I planned to add (Minkowski difference; Luapower-ish all-in-one scene), but it’s probably ready enough now.
The “Polygon offset” scene shown in the last few seconds of the video, adapted from an example in Clipper’s docs, demonstrates the capability @davebollinger mentioned earlier.
[quote name=“davebollinger” post=“383840” timestamp=“1528914020”]@StarCrunch - i thought i’d seen one, but couldn’t remember, that may have been it. aside: tinkered briefly cleaning up some personal code, still pretty special-use, might eventually? be suitable for market [/quote] This is exactly the kind of thing I’m looking for! I really hope you consider making this into a plug-in.