refactored droplet mode-ing
This commit is contained in:
1 parent
536b212426
commit
f00568c781
3 files changed
+61
-21
No files matched your search
+53
-14
@@ -1,26 +1,65 @@
|
||||
class_name Droplet extends RigidBody2D
|
||||
|
||||
# bounce mode seems to settle with a consistent surface, but is erratic
|
||||
# stick mode seems to make sand castles, but is very calm
|
||||
enum Mode { BOUNCE, STICK }
|
||||
const mode: Mode = Mode.BOUNCE
|
||||
# default mode: a bit sluggish, tendency to pile
|
||||
# bounce mode: doesn't settle down at all, but keeps flat surface
|
||||
# bounce_wip: wonky AF, probably due to extra compute
|
||||
# stick mode: piles tightly, very calm
|
||||
# wip mode: interesting, the base stays together, and the new one jumps into place
|
||||
enum Mode { DEFAULT, BOUNCE, BOUNCE_WIP, STICK, WIP }
|
||||
const MODE: Mode = Mode.WIP
|
||||
|
||||
class DropletConfig:
|
||||
var bounce
|
||||
var friction
|
||||
var linear_damp
|
||||
var angular_damp
|
||||
func _init(mode):
|
||||
linear_damp = ProjectSettings.get_setting("physics/2d/default_linear_damp")
|
||||
angular_damp = ProjectSettings.get_setting("physics/2d/default_angular_damp")
|
||||
if mode == Mode.DEFAULT:
|
||||
bounce = 0
|
||||
friction = 0.1
|
||||
elif mode == Mode.BOUNCE:
|
||||
bounce = 0.5
|
||||
friction = 0.1
|
||||
elif mode == Mode.BOUNCE_WIP:
|
||||
bounce = 0.5
|
||||
friction = 0.1
|
||||
elif mode == Mode.STICK:
|
||||
bounce = 0.1
|
||||
friction = 0.3
|
||||
else: # WIP
|
||||
bounce = 0.1
|
||||
friction = 1
|
||||
|
||||
func _ready():
|
||||
var config = DropletConfig.new(MODE)
|
||||
var mat = PhysicsMaterial.new()
|
||||
mat.bounce = .1 if mode == Mode.BOUNCE else 0
|
||||
mat.friction = 0.0
|
||||
mat.bounce = config.bounce
|
||||
mat.friction = config.friction
|
||||
physics_material_override = mat
|
||||
|
||||
linear_damp = config.linear_damp
|
||||
angular_damp = config.angular_damp
|
||||
# setup body entered signal, seems to also matter for STICK mode
|
||||
contact_monitor = true
|
||||
max_contacts_reported = 10
|
||||
body_entered.connect(_on_body_entered)
|
||||
if not MODE == Mode.DEFAULT:
|
||||
contact_monitor = true
|
||||
max_contacts_reported = 10
|
||||
body_entered.connect(_on_body_entered)
|
||||
|
||||
func _physics_process(delta):
|
||||
pass
|
||||
|
||||
if MODE == Mode.WIP:
|
||||
for body in get_colliding_bodies():
|
||||
if body is RigidBody2D:
|
||||
var dir = global_position - body.global_position
|
||||
var dist = dir.length()
|
||||
if dist < 64:
|
||||
## IIUC: should act to keep close droplets together
|
||||
var force = -dir.normalized() * (1.0 - dist / 64.0)
|
||||
body.apply_central_force(force * 100)
|
||||
|
||||
func _integrate_forces(state):
|
||||
if mode == Mode.STICK:
|
||||
# this is probably overkill since we are overriding the default physics engine
|
||||
if MODE == Mode.STICK:
|
||||
var v = state.linear_velocity
|
||||
var contacts = state.get_contact_count()
|
||||
for i in range(contacts):
|
||||
@@ -34,7 +73,7 @@ func _integrate_forces(state):
|
||||
body.apply_central_impulse(normal * v.length() * (other_damp/contacts))
|
||||
|
||||
func _on_body_entered(body):
|
||||
if mode == Mode.BOUNCE:
|
||||
if MODE == Mode.BOUNCE_WIP:
|
||||
# trying to elicit stronger response from rigidBody that was struck
|
||||
# ends up being kind of erratic
|
||||
# at damp_factor .75: settles laterally, but erratic past half-full
|
||||
|
||||
+3
-2
@@ -3,17 +3,18 @@ class_name Model extends Node2D
|
||||
const PERIOD: int = 20 # smaller makes droplets faster
|
||||
var CURRENT_TIME: int = 0
|
||||
var last_droplet: int = 0
|
||||
const DROP_PT_X = 450
|
||||
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
|
||||
func _process(delta: float):
|
||||
CURRENT_TIME += delta * 100
|
||||
CURRENT_TIME += delta * 100
|
||||
if CURRENT_TIME - last_droplet > PERIOD:
|
||||
last_droplet = CURRENT_TIME
|
||||
# print("new droplet at time: ", str(CURRENT_TIME))
|
||||
var droplet = preload("res://scenes/droplet.tscn").instantiate()
|
||||
var shape = droplet.get_node("CollisionShape2D")
|
||||
shape.shape.radius = 5
|
||||
droplet.global_position = Vector2(450, 100)
|
||||
droplet.global_position = Vector2(DROP_PT_X, 100)
|
||||
add_child(droplet)
|
||||
Reference in new issue
Block a user