Add vibration functionality

main
copygirl 3 weeks ago
parent c6f2bd2471
commit 9b949b0dd6
  1. 42
      copyThrower.gd

@ -22,6 +22,7 @@ var DEFAULT_BUMP := {
head = Quaternion.IDENTITY,
current_body = Vector3.ZERO,
current_head = Quaternion.IDENTITY,
vibrate = 0.0,
}
var triggers = {
@ -132,6 +133,9 @@ func _apply_bumping(delta: float) -> void:
for skeleton: Skeleton3D in bump_lookup:
var bump: Dictionary = bump_lookup[skeleton]
if bump.vibrate > 0.35: bump.body = rand_unit_vector3() * bump.vibrate * 0.05
bump.vibrate = lerpf(bump.vibrate, 0.0, 1 - exp(-0.5 * delta))
bump.current_body = bump.current_body.lerp(bump.body, 1 - 0.001 ** delta)
bump.current_head = bump.current_head.slerp(bump.head, 1 - 0.001 ** delta)
bump.body = bump.body.lerp(Vector3.ZERO, 1 - 0.01 ** delta)
@ -211,7 +215,7 @@ func _grab_with_hands() -> void:
elif hand.closed and (closedness < HAND_OPEN_TRHESHOLD):
for hand_child in hand.collider.get_children():
var object := hand_child as RigidBody3D
if not object: continue
if (not object) or (not object.is_in_group("copyThrower/objects")): continue
object.reparent(object.original_parent)
object.linear_velocity = Vector3(0, 1.5, 3.0) * hand.collider.global_basis + Vector3.UP
object.freeze = false
@ -235,31 +239,45 @@ static func _get_total_curl(skeleton: Skeleton3D, bone: int, current := 0.0) ->
func on_collide(object: RigidBody3D, body: CharacterBody3D) -> void:
var collider := body.get_parent() as BoneAttachment3D
if not collider: return
var skeleton := collider.get_parent() as Skeleton3D
if not skeleton: return
var bump = bump_lookup.get(skeleton)
if not bump:
# Create a new entry in `bump_lookup` for this skeleton.
bump = DEFAULT_BUMP.duplicate(true)
bump_lookup[skeleton] = bump
var pos := object.global_position
var vel := object.linear_velocity
# Hits to the head cause the head (and some parent bones) to rotate.
if collider.bone_name == "Head":
var bone_pos := collider.global_position
var rotation := Quaternion(bone_pos.direction_to(pos), bone_pos.direction_to(pos + vel))
rotation = Quaternion.IDENTITY.slerp(rotation, 0.15 * object.size)
bump.head *= rotation
var rot := Quaternion(bone_pos.direction_to(pos), bone_pos.direction_to(pos + vel))
rot = Quaternion.IDENTITY.slerp(rot, 0.15 * object.size)
add_head_bump(skeleton, rot)
# Hits to any other body part cause the entire body to translate.
else:
vel.y *= 0.25 # Less vertical influence.
bump.body += vel * object.size * 0.05
add_body_bump(skeleton, vel * object.size * 0.05)
func lookup_bump_dict(skeleton: Skeleton3D) -> Dictionary:
var bump = bump_lookup.get(skeleton)
if not bump:
# Create a new entry in `bump_lookup` for this skeleton.
bump = DEFAULT_BUMP.duplicate(true)
bump_lookup[skeleton] = bump
return bump
func add_head_bump(skeleton: Skeleton3D, amount: Quaternion) -> void:
lookup_bump_dict(skeleton).head *= amount
func add_body_bump(skeleton: Skeleton3D, amount: Vector3) -> void:
lookup_bump_dict(skeleton).body += amount
func vibrate(skeleton: Skeleton3D, amount: float) -> void:
lookup_bump_dict(skeleton).vibrate += amount
static func pride(value: String) -> Texture2D:
return load("res://Mods/copyThrower/Resources/pride/" + value + ".png")
static func rand_unit_vector3() -> Vector3:
var theta := randf() * TAU
var phi := acos(randf_range(-1.0, 1.0))
return Vector3(cos(theta) * sin(phi), sin(theta) * sin(phi), cos(phi))

Loading…
Cancel
Save