It definitely shouldn’t crash. Looking at the code you included above, I can see it references things that aren’t included in the fragment. Can you boil down the code needed to reproduce the issue to the minimum and submit a bug?
Thanks!
It definitely shouldn’t crash. Looking at the code you included above, I can see it references things that aren’t included in the fragment. Can you boil down the code needed to reproduce the issue to the minimum and submit a bug?
Thanks!
Okay will do thank you very much! Here is plug and play code that works perfect on older build at any angle.
local physics = require (“physics”)
physics.start(true)
–physics.setDrawMode(‘hybrid’)
local lines = {}
local line_number = 1
local line_width = 3.5
local prev_x, prev_y
function drawLine(e)
if e.phase == “began” then
prev_x = e.x
prev_y = e.y
elseif e.phase == “moved” then
lines[line_number] = display.newLine(prev_x, prev_y, e.x, e.y)
lines[line_number]:setColor(0,191,255)
lines[line_number].width = line_width
– group:insert(lines[line_number])
dist_x = e.x - prev_x
dist_y = e.y - prev_y
– Add a physics body that’s a flat polygon that follows each segment of the line
physics.addBody(lines[line_number], “static”, { density = 1, friction = 1, bounce = .1, shape = {0, 0, dist_x, dist_y, 0, 0} } )
prev_x = e.x
prev_y = e.y
line_number = line_number + 1
elseif e.phase == “ended” then
end
end
Runtime:addEventListener(“touch”, drawLine)
When I run that code and draw a line I get the following in the console:
2013-10-23 10:30:14.334 Corona Simulator[7410:707] Runtime error /Users/perry/src/corona/apps/bug-physics/main.lua:65: From Box2D: edge.LengthSquared() \> 1.19209290e-7F \* 1.19209290e-7F stack traceback: [C]: ? [C]: in function 'addBody' /Users/perry/src/corona/apps/bug-physics/main.lua:65: in function 'func'
I think the error relates to having a physics body one of whose dimensions is too close to zero.
After some investigation I determined that this error isn’t displayed in Daily Builds because the Simulator crashes (it’s a difference between a debug and a release build). We’re looking into the issue.
Thank you Perry for looking into this issue as this is the main feature of my games. This is what my terminal says when I try to draw the line:
/Applications/CoronaSDK/Corona Terminal: line 9: 931 Segmentation fault: 11 “$path/Corona Simulator.app/Contents/MacOS/Corona Simulator” $*
logout
You can see the function in action on my latest game
https://itunes.apple.com/us/app/electro-puzzle-brain-arcade/id702376893?ls=1&mt=8
Hi @russm305,
While Perry investigates, I still encourage you to avoid using lines as physics bodies. This is even mentioned in the addBody() documentation under “Gotchas”: http://docs.coronalabs.com/api/library/physics/addBody.html
In either case, you may want to also code a method to prevent it from creating a new physics body on every “moved” state of the touch motion. You’re potentially creating hundreds of physics bodies in the code above, at each micro-increment of the sensory touch motion. This may result in a nice physical curve line, but performance will likely be impacted on some devices.
I once designed a similar line tracing sample using physics, and as the user dragged along the screen, I simply checked the distance between the previous point and the new point. If the distance was sufficient enough (something like 10-20 pixels) then, and only then, would it create a new physics body (rectangle, not line) and place it in the correct location. The result was a fairly nice, basically smooth “line” that other objects could interact with, using a fraction of the physics bodies that your code seems to be generating.
Anyway, just my two cents.
Take care,
Brent
Just to be clear, the bug relates to the reporting of the error not to the error itself which is valid. The message I showed above is the actual error being thrown by Box2D which was invisible until recently. You should consider Brent’s advice and modify your code so it doesn’t throw errors. When we fix the bug, you’ll be able to see the errors yourself.
The code i used was from the code exchange. I have no idea how it works. My coding skills are not sophisticated enough to modify it the way it is suggested by Brent. Would it be possible for someone from Corona to share this workaround code with me in the plug and play format similar to what i have done above? Thank you
Would someone from Corona please show me this modified code that will work in my projects. Thank you.
Hi @russm305,
I can’t write the entire module for you at this time, but basically, you should consider using a distance calculation between the previous point and the new point, like this:
[lua]
local function distBetween( point1x, point1y, point2x, point2y )
local xFactor = point2x-point1x ; local yFactor = point2y-point1y
local dist = math.sqrt((xFactor*xFactor) + (yFactor*yFactor))
return dist
end
local function angleBetween( point1x, point1y, point2x, point2y )
local angle = ( math.deg( math.atan2( dstY-srcY, dstX-srcX ) )+90 )
return angle%360
end
[/lua]
Then, in your “moved” phase, check the distance between the previous point and current touch point:
[lua]
local thisDistance = distBetween( prev_x, prev_y, e.x, e.y )
if ( thisDistance >= 10 ) then
--place a newRect object (small square?) and add a physics body to it
end
[/lua]
The value of “10” above will need to be adjusted… maybe 20 is better, maybe 30, it all depends on the size of your content area and the size of the objects you’ll draw.
You should also use the “angleBetween” function I show to rotate the squares to the correct angle, so the line is smoother (otherwise the little boxes will all be facing 0 degrees straight up, which would result in a bumpy line if a curve is drawn).
Anyway, hope this gets you started. The concept is to just limit the number of objects you draw, while still creating a basically smooth “line” that other objects can roll along, bounce off, etc.
Brent
I thought you had already written it? Sorry that doesn’t help. Like I said I don’t understand how it works. So nobody from Corona can take the time to write 30 or so lines of modified code for me too insert into my project???
Hi @russm305. Code you got from the “Community submitted code” area has to be supported by the author. We really can’t provide support on those codes. Your best bet is to get the author of that code to help you.
You can also ask for help coding your project in one of the job offer/collaboration forums.
You can also post a new thread with the request for help on your specific issue.
Gee I thought Corona would be more helpful.
Oh well Ill repost on the forums maybe i can get help elsewhere.
Hi @russm305,
We try to be as helpful as possible, but we have many people and many cases to respond to each day, in addition to countless other priorities. Thus, we cannot write “copy and paste” code for developers’ projects. When it’s realistic, we try to provide code snippets for a particular purpose and suggest methods/approaches for you to solve the issue.
It will benefit you greatly to implement and understand the nature of code, not just copy it from the Code Exchange. By doing so, your coding will become cleaner, more customizable, and easier to debug if things don’t occur as expected.
Best regards,
Brent
Would it be possible to to be provided a link to one of the multiple relevant threads to this bug like stated above so I can go and research this on my own. thank you.
Hi Russ,
I may be able to work up a quick sample today… not necessarily “copy and paste” but something that shows the process in action a bit clearer.
Take care,
Brent
Hi Russ,
I located a module for you, from Corona ambassador and physics-meister Matt Webster. This shows the functionality I was talking about, where line segments (which are actually newRects) are only drawn when the distance between the previous point and new point is sufficient. This helps to minimize the number of physics bodies created, thus improving performance.
You can download the fully-working project from Dropbox (link below) and parse the necessary function for your usage. It’s not too far un-related to what you use now, so it shouldn’t be too difficult to implement on your side.
https://www.dropbox.com/sh/vf7f52ywuml94t9/57QxCfplAb
Hope this helps,
Brent
Okay i will download monday (no wifi now) i will make sure to study it so i can adapt to changing environments, thank you
@ Brent
Is this fixed now??? I see in a different thread with the same problem this is what you say
http://forums.coronalabs.com/topic/38658-bug-on-physics-shape-crash-corona-sdk/page-2
This should be fixed in Build 1242 and above. Please test your software and let me know.
Brent
Or is my problem different?
edit: tried new build didnt work I posted in that link also.
So should my code work now?
I never had any problem with adding custom shaped physics to lines of any inclination. If you add a body to a line without a custom shape you will get a rectangle, as expected. Why should I not add physics to a line object? Am I missing something?
Hi @paolo,
In my testing, if you add a physics body to a line that’s either horizontal or vertical (not angled), it basically works fine. However, if you draw a line at a 45 degree angle (or whatever angle) then add the body, it creates a larger “bounded” body around the line. As long as you’re aware of that “gotcha”, you should be fine, but it’s not obvious to some developers what’s happening in that instance.
Brent