Skip to content

Instantly share code, notes, and snippets.

@RichardEllicott
Created December 6, 2024 02:34
Show Gist options
  • Save RichardEllicott/3e7762f4c28190f8784123da1bc90030 to your computer and use it in GitHub Desktop.
Save RichardEllicott/3e7762f4c28190f8784123da1bc90030 to your computer and use it in GitHub Desktop.
"""
"""
@tool
extends Node2D
var particles: Array[RID] = [] ## particle RIDS
@export var particle_radius: float = 5.0
@export var linear_damp: float = 0.1
@export var angular_damp: float = 0.1
@export var friction: float = 0.0
@export var water_fall := false
func _ready() -> void:
PhysicsServer2D.set_active(true) ## not sure if required yet (for tool mode)?
macro_spawn_particles()
func macro_spawn_particles():
for i in range(100): # Adjust number of particles
var _position = Vector2(randi() % 800, randi() % 600)
var velocity := Vector2(randf() -0.5, randf() -0.5) * 1000.0 ## random velocity
create_particle(_position, velocity) # Random positions
var shape: RID:
get:
if not shape:
shape = PhysicsServer2D.circle_shape_create()
PhysicsServer2D.shape_set_data(shape, particle_radius) ## set radius
return shape
var physics_material: PhysicsMaterial:
get:
if not physics_material:
physics_material = PhysicsMaterial.new()
physics_material.friction = friction # Set the desired friction value
return physics_material
func create_particle(position: Vector2, velocity: Vector2):
var body := PhysicsServer2D.body_create() ## create body
PhysicsServer2D.body_set_space(body, get_world_2d().space) ## set the body space to the world
## shape
PhysicsServer2D.body_add_shape(body, shape) ## attach shape to the body
## position
PhysicsServer2D.body_set_state(body, PhysicsServer2D.BODY_STATE_TRANSFORM, Transform2D(0.0, position))
## velocity
PhysicsServer2D.body_set_state(body, PhysicsServer2D.BODY_STATE_LINEAR_VELOCITY, velocity)
## friction
PhysicsServer2D.body_set_param(body, PhysicsServer2D.BODY_PARAM_FRICTION, physics_material)
## velocity damping
PhysicsServer2D.body_set_param(body, PhysicsServer2D.BODY_PARAM_LINEAR_DAMP, linear_damp)
PhysicsServer2D.body_set_param(body, PhysicsServer2D.BODY_PARAM_ANGULAR_DAMP, angular_damp)
particles.append(body)
var timer := 0.0
@export var delay := 1.0
@export var max_particles := 1000
func _physics_process(delta: float) -> void:
if water_fall:
timer += delta
if timer >= delay:
timer -= delay
var random_position = Vector2(randfn(0.0, 1.0), randfn(0.0, 1.0)) * 16.0
create_particle(random_position, Vector2())
while particles.size() > max_particles:
PhysicsServer2D.free_rid(particles.pop_front())
func _process(delta: float) -> void:
queue_redraw()
func _draw() -> void:
for body in particles:
var _transform = PhysicsServer2D.body_get_state(body, PhysicsServer2D.BODY_STATE_TRANSFORM)
draw_circle(_transform.origin, particle_radius, Color(1, 1, 1)) # White circle
## clear the particles
func clear():
while not particles.is_empty():
PhysicsServer2D.free_rid(particles.pop_back())
## clean up on exit
func _exit_tree() -> void:
clear()
func macro_clear():
clear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment