Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | FellowGamedev |
In short, when I move my rigidbody away from X = 0 and Z = 0,(y doesn’t matter,and least i dont think it does…) it starts to break and just flip out like crazy
Scene tree:
- Rigidbody
- Collision
- raycasts (script is attched to 4 raycasts, one at each corner)
code:
extends RayCast
onready var physbod = get_parent()
export var dampness = 0.025
export var stiffness = 0.25
export var relaxedlength = -1
var prev_compression = 0
var y_force = 0
func _ready():
cast_to.y = relaxedlength
func _physics_process(delta):
if is_colliding():
#calculate the compression ratio -0 means spring is fully relaxed, 1 means its fully compressed
var compression = 1 - (global_transform.origin.distance_to(get_collision_point()) / abs(relaxedlength))
#calculate forces and apply them
y_force = stiffness * compression * abs(relaxedlength)
y_force += dampness * (compression - prev_compression) / delta
physbod.apply_impulse(get_collision_point() , transform.basis.y * y_force)
prev_compression = compression
else:
prev_compression = 0