updated from build 1076 to 1202 and will crash upon function use

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

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

You are adding a physics body to your line. This did not return any errors in the past but now it does. You should use display.newRect() to fix this.

Yes the physics in the line is main part of the game

Do yo u mean instead of :

 lines[line_number] = display.newLine(prev_x, prev_y, e.x, e.y)

do this:

 lines[line_number] = display.newRect(prev_x, prev_y, e.x, e.y)

because now i am getting an error in the folowng line-

/Users/russm305/Desktop/Electic Portals/pepper15.lua:611: attempt to call method ‘setColor’ (a nil value)

stack traceback:

    [C]: in function ‘setColor’

    /Users/russm305/Desktop/Electic Portals/pepper15.lua:611: in function </Users/russm305/Desktop/Electic Portals/pepper15.lua:604>

    ?: in function <?:218>

How should I change the affected lines of code?

Is this considerd a bug that I should report?

You are getting this error because lines[line_number] is a nil value. This means that there is an error in the creation process of the object.

I can see that you are using 

lines[line\_number] = display.newRect(prev\_x, prev\_y, e.x, e.y) &nbsp;

 to create a rect…

If you take a look at Corona docs you will see that you need to change this code to create a rect:

display.newRect( left, top, width, height )

You can not take the same arguments you used for display.newLine since it uses the third and fourth one to create the ending points of a line.

Edit: Unfortunately I do not know a workaround if you are using dynamically created objects with different length values :confused:

Its a shame that things I have been using for months no longer work when I upgrade my build.  

Would you consider this a bug that needs to be reported?

I don´t think that CoronaLabs recognises this as a bug. But you should ask someone from their staff if they have a workaround for this unless someone else is posting here one :slight_smile:

Hi @russm305,

I believe this was already filed as a bug. When I investigated and tested the issue last (about a month ago), it seems that lines were accepting physics bodies in a previous build, but the results were very inconsistent, especially if you tried to apply it to an angular line (not a 90-degree increment). In addition, lines are not really intended to work with physics bodies, since other physics objects may pass through a thin line. As a result, I think the engineers are steering people away from lines as physics bodies, for better functionality and reliability in the physics engine.

As for the new error you’re getting when you change it over, rectangles accept :setFillColor(), not :setColor() as lines do. In Graphics 2.0, these methods should be more consistent, so you don’t need to think about which object type requires which fill function.

Best regards,

Brent

There are multiple threads about this.  Using really thin rectangles is the answer and those threads have suggestions on how to make it work.

Please point me to a relevant thread.  I’ve tried using thin rectangles, it doesn’t work.  You will have spacing in between the rectangles when a line is drawn too fast and physics objects will fall through.  

I have been using lines a physics body function for a long time and the results are always consistent.  So all of a sudden Corona Engineers decided lines cannot be physics bodies!!!???   I am absolutely disappointed and blown away!!!

Hi @russm305,

I retract my former statement. You can still use lines as physics bodies, according to my tests in #1202:

[lua]

local line = display.newLine( 400,400,400,500 )

line:setColor( 255,0,0 )

line.width = 10

physics.addBody( line, “static” )

line.rotation = 40

[/lua]

This being said, the results are not ideal and I still encourage you to use rectangles instead. For example, this line (sample) gets the body drawn around it essentially correct, but it’s a bit wider than the visible line, whereas a rectangle would have a perfectly-traced outline. Beyond that, if you drew this line at an angle originally (not a 90-degree multiple), the physics body gets drawn incorrectly (like a square box that is positioned around the starting point of the line).

So, you can still use lines as physics bodies. I just recommend that you don’t.

Best regards,

Brent

Okay that is a relief to hear.  Why do you think my line drawing function now crashing the simulator in build 1202?

It says:  Terminal: line 9:  2966 Segmentation fault: 11  “$path/Corona Simulator.app/Contents/MacOS/Corona Simulator” $*

logout

[lua]

    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)

[/lua]