This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.
Custom move_and_slide function
Hi, I'm working on a custom move_and_slide function, and have tried to copy the function from the source code. https://raw.githubusercontent.com/godotengine/godot/c9b57d4b4017a9bc25478a6334e3e1c74311f9c6/scene/3d/physics_body.cpp is where I tried copying from.
func move_and_slide_custom(var lv, var floor_direction = Vector3(0,1,0),
var physics_delta = get_physics_process_delta_time(),
var max_slides = 4,
var slope_stop_min_velocity = 0.05,
var floor_max_angle = deg2rad(45)):
var motion = (floor_velocity + lv) * physics_delta
floor_velocity = Vector3(0,0,0)
on_floor = false
while(max_slides):
var collision = move_and_collide(motion)
if collision:
motion = collision.remainder
if (collision.normal.dot(floor_direction) >= cos(floor_max_angle)):
on_floor = true
floor_velocity = collision.collider_velocity
var rel_v = lv - floor_velocity
var hor_v = rel_v - floor_direction * floor_direction.dot(rel_v)
if collision.get_travel().length() < 0.05 and hor_v.length() < slope_stop_min_velocity:
var gt = get_global_transform()
gt.origin -= collision.travel
set_global_transform(gt)
return (floor_velocity - floor_direction * floor_direction.dot(floor_velocity))
var n = collision.normal
motion = motion.slide(n)
lv = lv.slide(n)
else:
break
max_slides -= 1
if motion.length() == 0:
break
return lv
However, there seems to be a problem with the custom function. I am able to achieve nearly the same results as the regular move_and_slide function, except the KinematicBody's CollisionShape is going a bit inside static bodies. Does anyone know why this happens?
Tags :
Best Answer
-
IceColors Posts: 2
I found out the problem, i had the wrong indenting for part of the code. The corrected code is:
func move_and_slide_custom(var lv, var floor_direction = Vector3(0,1,0), var physics_delta = get_physics_process_delta_time(), var max_slides = 4, var slope_stop_min_velocity = 0.05, var floor_max_angle = deg2rad(45)): var motion = (floor_velocity + lv) * physics_delta floor_velocity = Vector3(0,0,0) on_floor = false while(max_slides): var collision = move_and_collide(motion) if collision: motion = collision.remainder on_floor = true if (collision.normal.dot(floor_direction) >= cos(floor_max_angle)): floor_velocity = collision.collider_velocity var rel_v = lv - floor_velocity var hor_v = rel_v - floor_direction * floor_direction.dot(rel_v) if collision.get_travel().length() < 0.05 and hor_v.length() < slope_stop_min_velocity: var gt = get_global_transform() gt.origin -= collision.travel set_global_transform(gt) return (floor_velocity - floor_direction * floor_direction.dot(floor_velocity)) var n = collision.normal motion = motion.slide(n) lv = lv.slide(n) else: print("no collision") break max_slides -= 1 if motion.length() == 0: break return lv
Answers
I found out the problem, i had the wrong indenting for part of the code. The corrected code is: