Godot asset to slice 2D sprites.
Slicer2D
is the main node and contains all the functionality to slice sprites.Sliceable2D
is the type of the object sliced bySlicer2D
.
This document uses the format of the official Godot docs.
That is type name
for variables and return_type name(type name)
for functions.
SlicingData
is the type which contains information of a sliced object.
It contains the following fields:
class SlicingData:
Sliceable2D object
Vector2 global_enter
Vector2 global_out
int cut_number
Array slices
-
object
: the original sliced object, this reference might be invalidated byqueue_free()
if you configure the Slicer2D to destroy that object after the slices are created. -
global_enter
: global coordinate of the entry of the cut in the object. -
global_out
: global coordinate of the exit of the cut in the object. -
cut_number
: number of the actual cut. -
slices
: array of references to the slices generated by the processed slice.
Tip: you can use global_enter
and global_out
to create animations.
Array slice_world(Vector2 start, Vector2 end, int collision_layer = 0x7FFFFFFF, bool destroy = true)
Tries to find Sliceable2D
s to cut from start to end. Returns an Array of SlicingData one for each sliced object.
-
start
: the start of the cut in global coordinates. -
end
: the end of the cut in global coordinates. -
collision_layer
: optional parameter to restrict the cut using a collision layer. -
destroy
: if true the object will be destroyed with queue_free() so only its slices are active.
SlicingData slice_one(Sliceable2D item, Vector2 start, Vector2 end, int collision_layer = 0x7FFFFFFF, bool destroy = true)
Does the same as slice_world
but only cuts item
if it is found in the cut.
Physics2DDirectSpaceState space_state
For advanced users. Assign a custom space state and it will be used to detect sliceable objects. That way you can isolate the interaction of a Slicer2D
as the sliceable objects are identified using raycasts. Default value: null
float min_area
Minimum approximate area. Default value: 0.01
float impulse_intensity
This value is multiplied to the force applied to the slices when you cut them. Default value: 100.0
The demo
folder contains a basic demo showing the functionality of this asset.
Open demo/TestScene.tscn
and test the asset in action!
- apple.png: CC-BY-SA 3.0
- Copy
Sliceable2D.gd
andSlicer2D.gd
to your game. - Add a
Slicer2D
node to your scene.
In order to create a sliceable object:
- Create a new scene with a
RigidBody2D
as the root node. - Add a script to the
RigidBody2D
and put at the beginning of the scriptextends Sliceable2D
- Add a Sprite node and add a texture.
- Click on the
RigidBody2D
and check the inspector. In Script Variables you should see a field "Sprite Node". Click on it and select yourSprite
. - Add a
CollisionPolygon2D
or aCollisionShape2D
to yourRigidBody2D
.
Notes:
- The order of the required children doesn't matter, they just need to exist. The only special requirement is defining the "Sprite Node" field in the inspector.
- Collisions must fit the figure of the sprite because the slices are generated using the collision shapes.
- The
Sprite
can haveflip_h
andflip_v
enabled. - You can add as many collision shapes as you wish. I you use
CollisionShape2D
the supportedShape2D
is:ConvexPolygonShape2D
. In the future it will supportCapsuleShape2D
,CircleShape2D
andRectangleShape2D
too. - After a cut, the created slices will replace the node pointed by "Sprite Node" with a
MeshInstance2D
. - The
Sliceable2D
created by a cut will be reparented to the same parent of the original sliced object.
Advanced usage:
If you want to change the texture of a Sliceable2D
just access the property texture
of the "Sprite Node". Before the first cut you will access the Sprite
you defined, and if you are accessing a sliced Sliceable2D
you will be accessing the texture
property of a MeshInstance2D
. The node name of the original Sprite
will be respected by the new MeshInstance2D
so it is secure to access with the $
operator. Just keep in mind the new texture will be mapped based on the UV of the original texture.
You can define the following in your script to gain more control over the slicing process:
int cut_limit_val
If the cut_number
is >= than cut_limit_val
this object wont be sliced anymore.
You can disable slicing on an object defining cut_limit_val
with values of 0 or lower.
void _on_sprite_sliced(SlicingData data)
This function will be called on the sliced object (not the slices generated by the cut) and data will contain the information about the slicing of your object.
If you want you can donate some bucks with the following options: