3D Raycast suspension behaves weirdly when not positioned in the middle of the world

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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 :frowning:
Scene tree:

  • Rigidbody
    • Collision
  • raycasts (script is attched to 4 raycasts, one at each corner)


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
		prev_compression = 0
:bust_in_silhouette: Reply From: klaas

have your read this in the RigidBody docs?

The position uses the rotation of the global coordinate system, but is centered at the object’s origin.

So i think when the “physbod” leaves the worlds zero coordinate the force isn’t applied properly.

can’t believe I missed this!
tysm its fixed now :slight_smile:

FellowGamedev | 2021-07-10 10:59