SoftBody2D Documentation

website·Indexed 31 days ago

https://softbody2d.appsinacup.com

Documentation for the SoftBody2D Godot plugin, which allows developers to create 2D softbodies from textures using Polygon2D. It includes technical references for the SoftBody2D node, helper classes like SoftBody2DPhysics, and guides on regions, joints, rigidbodies, and skin smoothing. The plugin supports breaking and cutting mechanics and is compatible with Godot Physics and Godot Rapier Physics.

Tokens
9.2K
Snippets
16
Records
75
Agent score
53%

What's inside SoftBody2D

  1. Overview of SoftBody2D plugin

    SoftBody2D is a Godot plugin that transforms a texture into a 2D squishy body. It achieves this by creating a lattice of RigidBody2D particles connected by Joint2Ds, using a Skeleton2D to deform the texture over the particles. The plugin is built using standard Godot nodes.
  2. Overview of SoftBody2D

    SoftBody2D inherits from Polygon2D and creates a 2D softbody based on a texture. It functions by generating one RigidBody2D per region (each with a CollisionShape2D), connecting neighbors with Joint2Ds, and using a Skeleton2D to deform the texture based on the physics bodies.
  3. Understand the SoftBody2D Rigidbody hierarchy

    Each region of a softbody is represented as a rigidbody. These bodies act as the softbody's particles, handling world collisions while joints maintain the structure and the texture follows their movement. The internal node hierarchy for each body is:

    • RigidBody2D
      • CollisionShape2D
      • RemoteTransform2D
      • Joint2D nodes
  4. Create softbodies with internal holes

    The 'With Holes' feature allows for the creation of softbody physics objects that contain internal holes.
  5. Understand SoftBody2D skinning and deformation

    The SoftBody2D node functions as a Polygon2D that is deformed by a Skeleton2D. It assigns one Bone2D per region. To maintain surface continuity, RemoteTransform2D nodes move each bone to its corresponding rigidbody, and vertices located on the borders between regions are weighted to both adjacent bones.
  6. Implement bouncy and squishy softbodies

    Use the 'Squishy' softbody type to create physics objects that exhibit bouncy and squishy behavior.
  7. Understand and visualize SoftBody2D regions

    A softbody is composed of multiple regions. Each region consists of one rigidbody, one bone, and one patch of texture. Regions are generated as a hexagon or rectangle grid that follows and is clipped to the texture's contour. To visualize these regions in the editor, enable the Draw Regions checkbox.
  8. Create breakable softbodies

    Configure softbodies to be 'Breakable' so they permanently deform or break once a specific force threshold is reached.
  9. Handle soft body tearing and detachment

    Use the joint_removed and body_detached signals to react when a SoftBody2D breaks. joint_removed provides the two connected children, and body_detached provides the child that has lost all connections. Use get_clusters() to determine if the soft body has split into multiple pieces.
    func _ready() -> void:
    	$SoftBody2D.joint_removed.connect(_on_joint_removed)
    	$SoftBody2D.body_detached.connect(_on_body_detached)
    
    func _on_joint_removed(a: SoftBody2D.SoftBodyChild, _b: SoftBody2D.SoftBodyChild) -> void:
    	# Play a rip sound where the joint gave way.
    	$Rip.global_position = a.rigidbody.global_position
    	$Rip.play()
    
    func _on_body_detached(child: SoftBody2D.SoftBodyChild) -> void:
    	# Detached bodies are frozen with their shape disabled by default. 
    	# Enable them to let the piece live as debris.
    	var body := child.rigidbody as RigidBody2D
    	body.freeze = false
    	child.shape.disabled = false
    
    func _process(_delta: float) -> void:
    	if $SoftBody2D.get_clusters().size() > 1:
    		print("the softbody is in pieces")
  10. Optimize SoftBody2D performance

    Performance cost is determined by the total number of bodies and joints. Use the following guidelines to maintain frame rates:

    • Vertex Interval: This controls the body count. Increasing the interval reduces the number of bodies (e.g., increasing from 30 to 60 significantly increases the number of softbodies that can be simulated at 60 fps).
    • Joint Reach: Setting Joint Reach to 1 reduces the joint count but may cause the softbody to sag and self-collide.
    • Physics Ticks: Keep physics/common/physics_ticks_per_second at 60. Increasing this to 120 can halve the number of softbodies the engine can handle before the frame rate drops.
  11. Configure Godot Rapier Physics for force-based breaking

    While SoftBody2D works with stock Godot Physics, the 'Force based breaking' feature requires the Godot Rapier Physics engine because it is the only engine that exposes how hard each joint is being pulled. To enable this, install Godot Rapier Physics and select Rapier2D under Project Settings → Physics → 2D.
  12. Choose between Circle and Square shapes for softbody patterns

    The choice of shape type depends on the softbody pattern to avoid excessive self-collisions:

    • Hexagon Pattern: Use Circles. Using squares on a hexagon pattern causes significant overlap with diagonal neighbors (increasing self-contacts from ~2 to 40-86 on a resting body). The editor will provide a warning if squares are used on hexagons.
    • Rectangle Pattern: Squares are suitable for this pattern.