How do I turn image into sprite with collision from DrawingCanvas?

0 favourites
  • 14 posts
From the Asset Store
Particles support animations, collisions, effects and etc.
  • Hello everyone, I have a drawingcanvas and I need to turn an image from it into a sprite, and then add physics to this sprite. What should I do?

  • I just want to implement:

    a person draws an image > this image is saved, but not on the computer, but temporarily > this image is loaded in one of the layouts and becomes a sprite

  • This is possible, but not an easy task. You'll need some script that will analyze the shape of the image and will build a collision polygon for it. Then you'll need the Polygon addon to load that polygon into a sprite.

    If the images are something simple, like Tetris shapes, there may be easier ways to do this.

  • dop2000, In theory, if my drawing is just black and white, I can do without this script?

  • You will still need to analyze the pixels to build the shape outline. Writing your own algorithm for that will be very difficult. It's a lot easier to use the script.

  • dop2000, okay. How can I install this script? Do I understand correctly that I just need the JavaScript code from the documentation?

  • You need to upload the script to Scripts folder. And this is how to use it:

    But this particular JS script requires transparent background. If you have black images on white background, it won't work. You might need to look for another library.

    Also, I believe the resulting PolygonArray needs some tweaking before you can load it into a sprite.

  • That is, if I have an ordinary DrawingCanvas on which a black drawing is created, then this script is of no use to me?

  • Is the background in the canvas transparent? If it is, then you can use this script. If the background is white (opaque), you need to erase the background first, or perhaps modify the script.

  • Here's this week's test. Event based Marching Squares to extract polygons from an image, then uses mesh distort to change the polygon. I would have guessed that we'd need js to do this fast enough, but events seem fine.

    dropbox.com/scl/fi/1s0xet28gg4h9aiwaz6li/marchingSquares.c3p

    Here's some older stuff I didn't reference. They use a handmade js implementation of marching squares.

    dropbox.com/scl/fi/1wuqpu4xe05bto5im4kh1/marchingSquares.c3p

    This one seems broken. Sizes are off

    dropbox.com/scl/fi/cuvne5uvea1yhwv1aodlu/marchingSquares2.c3p

    Anyways, just some stuff to mess around with.

  • Hmm.. Looks complicated. Could I start this way: instead of drawing and creating ovals on the DrawingCanvas, just create sprites and then sculpt them? Because my game is not complicated, and you can’t make very complex drawings there: just a circle, and a surface on which 2 wheels drawn by the user will ride

  • Perhaps you can assemble the object from sprites (rectangle, circle) and connect them together with physics joints.

  • Guess you could just start with a polygon with say 30 points and drag the points around to sculp it. Could use some math to do falloff brush so it would move more points at once.

    Then just reuse the small bit that converts a list of points to a distort mesh. If the polygon ever gets concave then it may visually not look right but there are probably ways to solve that. However if the user keeps the shape convex it should be fine.

    Guess there are other interesting ways to modify the shape. Maybe a convex hull of multiple basic shapes you can drag around. Or maybe some csg combinations of basic shapes. We can get away with slightly concave stuff on one axis with one distort mesh, but multiple meshes would be needed for severe concavity.

    Just some ideas. I like to push what’s possible. Construct provides some basic stuff, but with math, geometry and algorithms it’s a bit more open ended. To a point at least. Construct features have limits that can require some creativity to work around.

    Nothing wrong with scoping down to do something that’s more doable. Worst case you could just have a bunch of premade body shapes the user could pick from. Or like dop suggested assemble stuff from parts. You wouldn’t have to deal with polygons then.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Okay, I get it. Thanks dop2000 and R0J0hound for your help!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)