Hello Guest

Author Topic: Cutting/Splitting a Sprite with Mouse/Touch  (Read 4274 times)

ExCx

  • Newbie
  • *
  • Posts: 10
    • View Profile
Cutting/Splitting a Sprite with Mouse/Touch
« on: April 15, 2013, 10:57:35 pm »
Ok, I know, this is a wide question to ask. Maybe this forum isn't a place for this at all. But as I'm fully using 2d Toolkit in my projects, I believe I can find better answers in here rather than those 3d oriented Unity forums.

Anyway, what I'm trying to accomplish in my upcoming project is, splitting a 2d gameobject (2dtk Sprite or a simple quad plane -which would be suitable-) precisely from the traced path (straight or wavy) drawn with a mouse or touch input. After the process, one part will be vanished, while other part is the main object now, that could be splitted in smaller parts.

I'm aware that this could be accomplished with some sophisticated algorithms and such, but the thing is, I'm clueless even about where to start. Should I tweak with shaders (to fake a masking effect) or should I literally split the mesh somehow. Can anyone show me some pointers on this issue? Any piece of idea will be greatly appreciated.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Cutting/Splitting a Sprite with Mouse/Touch
« Reply #1 on: April 15, 2013, 11:39:22 pm »
I wouldn't tweak shaders to do this, it would be far too wasteful.

Since one part will vanish, then the most logical option is just to clip the original mesh. If the line will split the sprite in two always (i.e. a full line, not a partial line), then the result of the clip will always be convex as it starts from a convex shape (a quad).

With a convex shape, clipping should be pretty straightforward.

- Read up on (or find some code for) line clipping algorithms.
- Calculate your four points for the quad (clipped sprite has code describing this)
- Calculate the lines forming your quad
- Foreach line, clip with your clipping line - if its clipped, insert the clipped point into the point list
- Foreach point, discard if it is "behind" the clipping line.
- With the remaining lines, sort and triangulate.
- If you keep the polygon shape in addition to triangulated polys, performing subsequent clips on it will be quick. Just repeat the above process :)

I would use the clipped sprite code as a basis and go from there.
It probably sounds really complicated, but break it down to the core steps and each one is easily realizable.

39thstreet

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 77
    • View Profile
Re: Cutting/Splitting a Sprite with Mouse/Touch
« Reply #2 on: May 17, 2013, 08:38:27 pm »
I'm looking to do something very similar, so if anyone has gone down this road and feels like sharing code, that'd be great  :D.