I wanted to make a game that would have curved lines as physics objects. Does anyone know how to do that? Thanks! [import]uid: 29181 topic_id: 32302 reply_id: 332302[/import]
http://karnakgames.com/wp/corona-svg-level-builder/
Hope you didn’t want $100 XD
(If you ask him nicely he might sell it to you without his angry trolls source code for a bit cheaper) [import]uid: 144339 topic_id: 32302 reply_id: 128512[/import]
Assuming it doesn’t Just Work to put a body on the line (I guess the convexity requirement breaks down?), my guess would be something like the following:
-
Given a curve function:
x, y = curve(t), for t in [0, 1] -
Given another function:
[lua]local function CurveStuff (curve, i, n)
local x1, y1 = curve(i / n)
local x2, y2 = curve((i + 1) / n)
local dx, dy = x2 - x1, y2 - y1
local len = math.sqrt(dx * dx + dy * dy)
return len, x1, y1, x2, y2, dx, dy
end[/lua]
- Optional: Choose some number of segments, n, and then do
[lua]local len = 0
for i = 0, n - 1 do
len = len + CurveStuff(curve, i, n)
end[/lua]
to find out approximately how long your curve is (assuming you don’t have an analytic solution)
You could do other stuff like comparing angles between consecutive segments, etc. too
- Choose a number of segments (if you did the last step, the length may help you pick a better number), and then do
[lua]for i = 0, n - 1 do
local len, x1, y1, x2, y2, dx, dy = CurveStuff(curve, i, n)
dx, dy = dx / len, dy / len – Unit tangent
– Approximate the segment with a small box
– Ugh, I don’t want to look all this stuff up, so this is pseudo-code 
– (-dy, +dx) and (+dy, -dx) are the unit normals to the curve
local corner1 = { x1 - dy * THICKNESS, y1 + dx * THICKNESS }
local corner2 = { x1 + dy * THICKNESS, y1 - dx * THICKNESS }
local corner3 = { x2 + dy * THICKNESS, y2 - dx * THICKNESS }
local corner4 = { x2 - dy * THICKNESS, y2 + dx * THICKNESS }
AddShape(corner1, corner2, corner3, corner4, Density / n) – Assuming the shapes are about equal
end[/lua]
and then just stuff all those shapes into a body and add it to the line.
Anyhow, that’s my idea. This of course assumes the gist of the question wasn’t more basic, like “How do I compute the curve?”, say. [import]uid: 27791 topic_id: 32302 reply_id: 128560[/import]
Hi @NinjaPig (real name Jordan, yes? Sorry, can’t quit remember),
As you probably know, concave shapes won’t work in Box2D. So, depending on the level of complexity of these curved lines you want, I might suggest:
A) multi-element bodies… like a “line” composed of several slim rectangles.
B) thin rectangles joined like a chain with weld joints or pivot joints with no pivot flexibility.
I can’t think of any better options really. 
Brent [import]uid: 9747 topic_id: 32302 reply_id: 128566[/import]
Yeah thats what I was reading about. I was wondering if there was a hack around it. When I played around with the Tiny Wings sample, it was apparent that there was no way to do it efficiently.
@Star Crunch thank you very much for your in dept response. I will have to try that code about and see how it works!
Thanks everyone! [import]uid: 29181 topic_id: 32302 reply_id: 128571[/import]
http://karnakgames.com/wp/corona-svg-level-builder/
Hope you didn’t want $100 XD
(If you ask him nicely he might sell it to you without his angry trolls source code for a bit cheaper) [import]uid: 144339 topic_id: 32302 reply_id: 128512[/import]
Assuming it doesn’t Just Work to put a body on the line (I guess the convexity requirement breaks down?), my guess would be something like the following:
-
Given a curve function:
x, y = curve(t), for t in [0, 1] -
Given another function:
[lua]local function CurveStuff (curve, i, n)
local x1, y1 = curve(i / n)
local x2, y2 = curve((i + 1) / n)
local dx, dy = x2 - x1, y2 - y1
local len = math.sqrt(dx * dx + dy * dy)
return len, x1, y1, x2, y2, dx, dy
end[/lua]
- Optional: Choose some number of segments, n, and then do
[lua]local len = 0
for i = 0, n - 1 do
len = len + CurveStuff(curve, i, n)
end[/lua]
to find out approximately how long your curve is (assuming you don’t have an analytic solution)
You could do other stuff like comparing angles between consecutive segments, etc. too
- Choose a number of segments (if you did the last step, the length may help you pick a better number), and then do
[lua]for i = 0, n - 1 do
local len, x1, y1, x2, y2, dx, dy = CurveStuff(curve, i, n)
dx, dy = dx / len, dy / len – Unit tangent
– Approximate the segment with a small box
– Ugh, I don’t want to look all this stuff up, so this is pseudo-code 
– (-dy, +dx) and (+dy, -dx) are the unit normals to the curve
local corner1 = { x1 - dy * THICKNESS, y1 + dx * THICKNESS }
local corner2 = { x1 + dy * THICKNESS, y1 - dx * THICKNESS }
local corner3 = { x2 + dy * THICKNESS, y2 - dx * THICKNESS }
local corner4 = { x2 - dy * THICKNESS, y2 + dx * THICKNESS }
AddShape(corner1, corner2, corner3, corner4, Density / n) – Assuming the shapes are about equal
end[/lua]
and then just stuff all those shapes into a body and add it to the line.
Anyhow, that’s my idea. This of course assumes the gist of the question wasn’t more basic, like “How do I compute the curve?”, say. [import]uid: 27791 topic_id: 32302 reply_id: 128560[/import]
Hi @NinjaPig (real name Jordan, yes? Sorry, can’t quit remember),
As you probably know, concave shapes won’t work in Box2D. So, depending on the level of complexity of these curved lines you want, I might suggest:
A) multi-element bodies… like a “line” composed of several slim rectangles.
B) thin rectangles joined like a chain with weld joints or pivot joints with no pivot flexibility.
I can’t think of any better options really. 
Brent [import]uid: 9747 topic_id: 32302 reply_id: 128566[/import]
Yeah thats what I was reading about. I was wondering if there was a hack around it. When I played around with the Tiny Wings sample, it was apparent that there was no way to do it efficiently.
@Star Crunch thank you very much for your in dept response. I will have to try that code about and see how it works!
Thanks everyone! [import]uid: 29181 topic_id: 32302 reply_id: 128571[/import]