added some bucket features and two modes
This commit is contained in:
1 parent
4ec4dbb5ad
commit
536b212426
3 files changed
+106
-9
No files matched your search
+45
-3
@@ -1,7 +1,49 @@
|
||||
class_name Droplet extends RigidBody2D
|
||||
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
# 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
|
||||
|
||||
func _process(delta):
|
||||
func _ready():
|
||||
var mat = PhysicsMaterial.new()
|
||||
mat.bounce = .1 if mode == Mode.BOUNCE else 0
|
||||
mat.friction = 0.0
|
||||
physics_material_override = mat
|
||||
|
||||
# setup body entered signal, seems to also matter for STICK mode
|
||||
contact_monitor = true
|
||||
max_contacts_reported = 10
|
||||
body_entered.connect(_on_body_entered)
|
||||
|
||||
func _physics_process(delta):
|
||||
pass
|
||||
|
||||
func _integrate_forces(state):
|
||||
if mode == Mode.STICK:
|
||||
var v = state.linear_velocity
|
||||
var contacts = state.get_contact_count()
|
||||
for i in range(contacts):
|
||||
var body = state.get_contact_collider_object(i)
|
||||
var normal = state.get_contact_local_normal(i)
|
||||
var self_damp = .9
|
||||
var other_damp = 1 - self_damp
|
||||
v = v.bounce(normal) * self_damp
|
||||
state.linear_velocity = v # self bounce
|
||||
if body is RigidBody2D: # other body
|
||||
body.apply_central_impulse(normal * v.length() * (other_damp/contacts))
|
||||
|
||||
func _on_body_entered(body):
|
||||
if mode == Mode.BOUNCE:
|
||||
# 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
|
||||
# at damp_factor of .5, it's pretty calm, but then can't overfill (expected?)
|
||||
var vel = get_linear_velocity()
|
||||
const damp_factor = 0.7
|
||||
var normal = (body.global_position - global_position).normalized()
|
||||
# reflect velocity across collision direction
|
||||
var bounced = vel.bounce(normal) # reflection
|
||||
set_linear_velocity(bounced * damp_factor)
|
||||
if body is RigidBody2D:
|
||||
body.apply_central_impulse(-bounced * damp_factor)
|
||||
+8
-4
@@ -1,15 +1,19 @@
|
||||
class_name Model extends Node2D
|
||||
|
||||
const PERIOD: int = 300
|
||||
const PERIOD: int = 20 # smaller makes droplets faster
|
||||
var CURRENT_TIME: int = 0
|
||||
var last_droplet: int = 0
|
||||
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
|
||||
func _process(delta: float):
|
||||
CURRENT_TIME += delta * 100
|
||||
if CURRENT_TIME % 100 == 0:
|
||||
print("Current time: ", str(CURRENT_TIME))
|
||||
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()
|
||||
droplet.global_position = Vector2(100, 100)
|
||||
var shape = droplet.get_node("CollisionShape2D")
|
||||
shape.shape.radius = 5
|
||||
droplet.global_position = Vector2(450, 100)
|
||||
add_child(droplet)
|
||||
Reference in new issue
Block a user