Bezier curves example and physics

I downloaded Bezier curve example from here: http://developer.anscamobile.com/code/bezier-object-along-curves-path

Does anybody know how to add physics body for that line? I have tried to do that, but i have had few problems. So does anyone know how to do that properly? [import]uid: 18445 topic_id: 12802 reply_id: 312802[/import]

At present you cannot add physics to a line you manually draw. [import]uid: 52491 topic_id: 12802 reply_id: 46943[/import]

Yeah, I know that. But i tried to make physics another way: i made little balls, which are in the line and added physics body to balls. So “line” has physics, but i had little problems with it. If i change line, balls in old line stays in old place and i couldnt remove them. I just got some error.

Here is some code:

local function drawBezierSegment(granularity,r,g,b)  
  
 if line then   
 line:removeSelf()   
 end  
  
 line = display.newLine(bezierSegment[1].x,bezierSegment[1].y,bezierSegment[2].x,bezierSegment[2].y);  
  
  
  
 for i = 3, granularity, 1 do   
 line:append( bezierSegment[i].x,bezierSegment[i].y);  
  
 a = display.newCircle(bezierSegment[i].x,bezierSegment[i].y ,4)  
 physics.addBody( a,"static", {bounce = 0.0, friction = 0,radius=4})  
  
 end  
 line:setColor(r,g,b);  
 line.width=2;  
  
  
end  
  

But when i tried to remove those balls with a:removeSelf(), i just got error: "attempt to call method removeSelf a nil value

I dont know where problem is. [import]uid: 18445 topic_id: 12802 reply_id: 46947[/import]

Well, where are you doing a:removeSelf() ? :wink:

Likely you need to be creating a table of the balls as you add them so you can then loop through that table and remove each ball.

lineBalls = {}  
  
local function drawBezierSegment(granularity,r,g,b)  
  
 if line then   
 line:removeSelf()   
 end  
  
 line = display.newLine(bezierSegment[1].x,bezierSegment[1].y,bezierSegment[2].x,bezierSegment[2].y);  
  
  
  
 for i = 3, granularity, 1 do   
 line:append( bezierSegment[i].x,bezierSegment[i].y);  
  
 a = display.newCircle(bezierSegment[i].x,bezierSegment[i].y ,4)  
 physics.addBody( a,"static", {bounce = 0.0, friction = 0,radius=4})  
  
 lineBalls[#lineBalls+1] = a;  
  
 end  
 line:setColor(r,g,b);  
 line.width=2;  
  
  
end  
  
local function clearBalls (tableOfBalls)  
  
 local k,v  
 for k,v in pairs (tableOfBalls) do  
 if (v and v.removeSelf) then  
 v:removeSelf()  
 end   
 end  
  
end  
  

Very creative implementation though. [import]uid: 13859 topic_id: 12802 reply_id: 46982[/import]

Thanks for help, but clearballs() function didnt work for me. When i call it, i get error: “bad argument #1 to ‘pairs’ table expected, got nil”

Could you help little bit?

PS: you need to be creative, to create cool games :stuck_out_tongue: [import]uid: 18445 topic_id: 12802 reply_id: 47118[/import]

Never mind. I figured this out little bit different way. Now it is working perfectly. [import]uid: 18445 topic_id: 12802 reply_id: 47166[/import]