Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you draw like a Crayon?

Tags:

graphics

Crayon Physics Deluxe is a commercial game that came out recently. Watch the video on the main link to get an idea of what I'm talking about.

It allows you to draw shapes and have them react with proper physics. The goal is to move a ball to a star across the screen using contraptions and shapes you build.

While the game is basically a wrapper for the popular Box2D Physics Engine, it does have one feature that I'm curious about how it is implemented.

Its drawing looks very much like a Crayon. You can see the texture of the crayon and as it draws it varies in thickness and darkness just like an actual crayon drawing would look like.

alt text
(source: kloonigames.com)
alt text
(source: kloonigames.com)

The background texture is freely available here.

What kind of algorithm would be used to render those lines in a way that looks like a Crayon? Is it a simple texture applied with a random thickness and darkness or is there something more going on?

like image 469
mmcdole Avatar asked Feb 03 '09 22:02

mmcdole


4 Answers

I remember reading (a long time ago) a short description of an algorithm to do so:

  • for the general form of the line, you split the segment in two at a random point, and move this point slightly away from it's position (the variation depending on the distance of the point to the extremity). Repeat recursively/randomly. In this way, you lines are not "perfect" (straight line)

  • for a given segment you can "overshoot" a little bit, by extending one extremity or the other (or both). In this way, you don't have perfect joints. If i remember well, the best was to extends the original extremities, but you can do this for the sub-segment if you want to visibly split them.

  • draw the lines with pattern/stamp

  • there was also the (already mentioned) possibility to drawn with different starting and ending opacity (to mimic the tendency to release the pen at the end of drawing)

  • You can use a different size for the stamp on the beginning and the end of the line (also to mimic the tendency to release the pen at the end of drawing). For the same effect, you can also draw the line twice, with a small variation for one of the extremity (be careful with the alpha in this case, as the line will be drawn twice)

  • Last, for a given line, you can do the previous modifications several times (ie draw the line twice, with different variations) : human tend to repeat a line if they make some mistakes.

Regards

like image 168
PATRY Guillaume Avatar answered Nov 08 '22 12:11

PATRY Guillaume


If you blow the image up you can see a repeating stamp-pattern .. there's probably a small assortment that it uses as it moves from a to b - might even rotate them ..

The wavering of the line can't be all that difficult to do. Divide into a bunch of random segments, pick positions slightly away from the direct route and draw splines.

like image 20
Scott Evernden Avatar answered Nov 08 '22 10:11

Scott Evernden


Here's a paper that uses a lot of math to simulate the deposition of wax on paper using a model of friction. But I think your best bet is to just use a repeating pattern, as another reader mentioned, and vary the opacity according to pressure.

For the imperfect line drawing parts, I have a blog entry describing how to do it using bezier curves.

like image 36
Steve Hanov Avatar answered Nov 08 '22 12:11

Steve Hanov


You can base darkness on speed. That's just measuring the distance traveled by the cursor between this frame and the last frame (remember Pythagorean theorem) and then when you go to draw that line segment on screen, adjust the alpha (opacity) according to the distance you measured.

like image 41
Breton Avatar answered Nov 08 '22 10:11

Breton