Godot Version
4.3 Stable
Question
In 3D, I made a rigidbody character controller, and to keep its speed detection relative to a moving rigid body that the player is standing on, I need to detect the velocity at the point contacted by the player’s rigidbody.
In following a Reddit post, I saw the function for getting a velocity at a given point on an object was:
func get_point_velocity (point :Vector3)->Vector3:
return linear_velocity + angular_velocity.cross(point - global_transform.origin)
My implemented function in Godot 3.5 and 4.1 ended up looking like this, with contact_position
coming from state.get_contact_collider_position(0)
:
# Gets the velocity of a contacted rigidbody at the point of contact with the player capsule
func get_contacted_body_velocity_at_point(contacted_body: RigidBody3D, contact_position: Vector3):
# Global coordinates of contacted body
var body_position = contacted_body.transform.origin
# Global coordinates of the point of contact between the player and contacted body
var global_contact_position = body_position + contact_position
# Calculate local velocity at point (cross product of angular velocity and contact position vectors)
var local_vel_at_point = contacted_body.get_angular_velocity().cross(
global_contact_position - body_position
)
# Add the current velocity of the contacted body to the velocity at the contacted point
return contacted_body.get_linear_velocity() + local_vel_at_point
In upgrading from 4.1 to 4.3, it only works the same if I change the line to be this:
global_contact_position - body_position - body_position
get_contact_collider_velocity_at_position
returns the velocity at the point as this code intended to, but I’m wondering whether there could be a bug somewhere in one of the utilities in the title. I’m not sure how to test to isolate it.