Add physics to a drawn line

Hi fellow coders!

I am sure I have seen a video where someone was showing that they drew a line on the screen and added physics to it after the line was drawn, and the line fell down (like in the game Happy Glass).

I am sure it was a made in Corona. 

I can’t find that video and I am starting to think it was a dream.

Please help  :frowning:

In the old days, it was not a good idea to try to add physics bodies to lines.

Today, we have a toy in our toybox that lets us create line bodies:

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html#edge-shape-chain-body

I can’t recall right now, but you may need to do an offset calculation to match the line vertices with the chain vertices (or vice versa).

Thanks!

“You should not construct an edge shape body with self-intersecting segments — in other words, your definition of vertices should not result in any segments of the chain intersecting with other segments. Doing so many break the expected collision detection of the shape.”

That is a problem as I was hoping to be able to allow all kinds of drawings.

Could it be done by drawing the line using small circles, add physics to them and weld them together, or would it be to much for the device to handle if the line is long.

I used to play Plasma Pig That game was made with Corona. I wonder how he did it?

https://www.youtube.com/watch?v=ZIqbZt_S3kY

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/11/draw_physics_line.zip

Yes, I noticed the body doesn’t rotate… not sure why.

re: intersections, perhaps just make it a multibody with chain shapes split at each intersection?

re: plasma pig

  1. I didnt’ see any intersections.

  2. You could do that with a series of tiny static rectangular bodies aligned to the line at that position.

i.e. the body has no line.  you create a bunch of tiny transparent rectangles tracing the line shape with bodies. 4…10 per line would do.

Yeah, I’m pretty sure that is how he did it:

see_segments.png

Having the line fall would be a different trick. As you suggested you might consider connecting the objects together using joints.

I would not use a series of joints.  I’d connect all bodies to a single point on one object.

B–>A

C–>A

D–>A

Z–>A

Yeah it seems like it.

Not perfect, but that would be ok in a children’s game.
Plasma Pig is gone unfortunately (I really liked it), but I can’t remember if lines could intersect in that game.

Your test looked great by the way, except for the not rotating part :slight_smile:

Thanks for the tip about the joints :slight_smile:

Blah!
Seems like using multiple weld joints makes the physics bodies scatter like crazy. It works when I draw a super short line, but not more.

Another thing is that for some reason I can’t seem to get the circles to create with the same distance (circleradius*=0.5). The faster I move the finger the more scattered the circles.

kinematic rects (act static for as long as need, then give velocity to simulate fall)

no chains, welds, multi-element bodies,…

Hmmm…that is clever. It could work. 

@sirmania

The project you are looking for is called “Crayon Physics”. Creates a single multi-element physical body for each line fragment. As @davebollinger suggested.

I share a copy of code that I DID NOT DO but I can not find the author online:

However, I remember that the file had no restrictions.

I’m glad when someone here helps me, and I’m glad to reciprocate.

I hope it help.

Million thanks, @maximo97  :smiley:

This is exactly what I was thinking about. And to get access to the code is just amazing.

I have been spending a lot of time figuring this out, but I realized it was just to complicated for me.

Now we can focus on the game, finally  B)

In the old days, it was not a good idea to try to add physics bodies to lines.

Today, we have a toy in our toybox that lets us create line bodies:

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html#edge-shape-chain-body

I can’t recall right now, but you may need to do an offset calculation to match the line vertices with the chain vertices (or vice versa).

Thanks!

“You should not construct an edge shape body with self-intersecting segments — in other words, your definition of vertices should not result in any segments of the chain intersecting with other segments. Doing so many break the expected collision detection of the shape.”

That is a problem as I was hoping to be able to allow all kinds of drawings.

Could it be done by drawing the line using small circles, add physics to them and weld them together, or would it be to much for the device to handle if the line is long.

I used to play Plasma Pig That game was made with Corona. I wonder how he did it?

https://www.youtube.com/watch?v=ZIqbZt_S3kY

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/11/draw_physics_line.zip

Yes, I noticed the body doesn’t rotate… not sure why.

re: intersections, perhaps just make it a multibody with chain shapes split at each intersection?

re: plasma pig

  1. I didnt’ see any intersections.

  2. You could do that with a series of tiny static rectangular bodies aligned to the line at that position.

i.e. the body has no line.  you create a bunch of tiny transparent rectangles tracing the line shape with bodies. 4…10 per line would do.