Skip to content

Commit

Permalink
Merge pull request #15 from ARPGLTD/RefactorTestMotion
Browse files Browse the repository at this point in the history
Refactor test motion
  • Loading branch information
Phazorknight authored Jan 7, 2024
2 parents f75c16d + 8941cbd commit 8b0315e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions COGITO/Scripts/ladder_area.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ func _ready():

func _on_body_entered(body):
if body.is_in_group("Player"):
print("Entered ladder")
#print("Entered ladder")
body.on_ladder = true


func _on_body_exited(body):
if body.is_in_group("Player"):
print("Exited ladder")
#print("Exited ladder")
body.on_ladder = false
41 changes: 23 additions & 18 deletions COGITO/Scripts/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,22 @@ func _process(delta):
if sanity_component.current_sanity <= 0:
take_damage(health_component.no_sanity_damage * delta)

# Cache allocation of test motion parameters.
@onready var _params: PhysicsTestMotionParameters3D = PhysicsTestMotionParameters3D.new()

func params(transform3d, motion):
var params : PhysicsTestMotionParameters3D = PhysicsTestMotionParameters3D.new()
var params : PhysicsTestMotionParameters3D = _params
params.from = transform3d
params.motion = motion
params.recovery_as_collision = true
return params

@onready var self_rid: RID = self.get_rid()
@onready var test_motion_result: PhysicsTestMotionResult3D = PhysicsTestMotionResult3D.new()

func test_motion(transform3d: Transform3D, motion: Vector3) -> bool:
return PhysicsServer3D.body_test_motion(self_rid, params(transform3d, motion), test_motion_result)

### LADDER MOVEMENT
func _process_on_ladder(_delta):
var input_dir = Input.get_vector("left", "right", "forward", "back")
Expand Down Expand Up @@ -298,7 +306,7 @@ func _process_on_ladder(_delta):
if jump:
velocity += look_vector * Vector3(JUMP_VELOCITY, JUMP_VELOCITY, JUMP_VELOCITY)

print("Input_dir:", input_dir, ". direction:", direction)
# print("Input_dir:", input_dir, ". direction:", direction)
move_and_slide()

#Step off ladder when on ground
Expand Down Expand Up @@ -514,26 +522,24 @@ func _physics_process(delta):
is_step = false

if gravity_vec.y >= 0:
for i in range(STEP_CHECK_COUNT):
var test_motion_result: PhysicsTestMotionResult3D = PhysicsTestMotionResult3D.new()

for i in range(STEP_CHECK_COUNT):
var step_height: Vector3 = STEP_HEIGHT_DEFAULT - i * step_check_height
var transform3d: Transform3D = global_transform
var motion: Vector3 = step_height

var is_player_collided: bool = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
var is_player_collided: bool = test_motion(transform3d, motion)

if test_motion_result.get_collision_count() > 0 and test_motion_result.get_collision_normal(0).y < 0:
continue

if not is_player_collided:
transform3d.origin += step_height
motion = velocity * delta
is_player_collided = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
is_player_collided = test_motion(transform3d, motion)
if not is_player_collided:
transform3d.origin += motion
motion = -step_height
is_player_collided = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
is_player_collided = test_motion(transform3d, motion)
if is_player_collided:
if test_motion_result.get_collision_count() > 0 and test_motion_result.get_collision_normal(0).angle_to(Vector3.UP) <= deg_to_rad(STEP_MAX_SLOPE_DEGREE):
head_offset = -test_motion_result.get_remainder()
Expand All @@ -545,11 +551,11 @@ func _physics_process(delta):

transform3d.origin += test_motion_result.get_collision_normal(0) * WALL_MARGIN
motion = (velocity * delta).slide(wall_collision_normal)
is_player_collided = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
is_player_collided = test_motion(transform3d, motion)
if not is_player_collided:
transform3d.origin += motion
motion = -step_height
is_player_collided = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
is_player_collided = test_motion(transform3d, motion)
if is_player_collided:
if test_motion_result.get_collision_count() > 0 and test_motion_result.get_collision_normal(0).angle_to(Vector3.UP) <= deg_to_rad(STEP_MAX_SLOPE_DEGREE):
head_offset = -test_motion_result.get_remainder()
Expand All @@ -560,15 +566,15 @@ func _physics_process(delta):
var wall_collision_normal: Vector3 = test_motion_result.get_collision_normal(0)
transform3d.origin += test_motion_result.get_collision_normal(0) * WALL_MARGIN
motion = step_height
is_player_collided = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
is_player_collided = test_motion(transform3d, motion)
if not is_player_collided:
transform3d.origin += step_height
motion = (velocity * delta).slide(wall_collision_normal)
is_player_collided = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
is_player_collided = test_motion(transform3d, motion)
if not is_player_collided:
transform3d.origin += motion
motion = -step_height
is_player_collided = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
is_player_collided = test_motion(transform3d, motion)
if is_player_collided:
if test_motion_result.get_collision_count() > 0 and test_motion_result.get_collision_normal(0).angle_to(Vector3.UP) <= deg_to_rad(STEP_MAX_SLOPE_DEGREE):
head_offset = -test_motion_result.get_remainder()
Expand All @@ -579,16 +585,15 @@ func _physics_process(delta):


if not is_step and is_on_floor():
var test_motion_result: PhysicsTestMotionResult3D = PhysicsTestMotionResult3D.new()
var step_height: Vector3 = STEP_HEIGHT_DEFAULT
var transform3d: Transform3D = global_transform
var motion: Vector3 = velocity * delta
var is_player_collided: bool = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
var is_player_collided: bool = test_motion(transform3d, motion)

if not is_player_collided:
transform3d.origin += motion
motion = -step_height
is_player_collided = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
is_player_collided = test_motion(transform3d, motion)
if is_player_collided:
if test_motion_result.get_collision_count() > 0 and test_motion_result.get_collision_normal(0).angle_to(Vector3.UP) <= deg_to_rad(STEP_MAX_SLOPE_DEGREE):
head_offset = test_motion_result.get_travel()
Expand All @@ -601,11 +606,11 @@ func _physics_process(delta):
var wall_collision_normal: Vector3 = test_motion_result.get_collision_normal(0)
transform3d.origin += test_motion_result.get_collision_normal(0) * WALL_MARGIN
motion = (velocity * delta).slide(wall_collision_normal)
is_player_collided = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
is_player_collided = test_motion(transform3d, motion)
if not is_player_collided:
transform3d.origin += motion
motion = -step_height
is_player_collided = PhysicsServer3D.body_test_motion(self.get_rid(), params(transform3d, motion), test_motion_result)
is_player_collided = test_motion(transform3d, motion)
if is_player_collided:
if test_motion_result.get_collision_count() > 0 and test_motion_result.get_collision_normal(0).angle_to(Vector3.UP) <= deg_to_rad(STEP_MAX_SLOPE_DEGREE):
head_offset = test_motion_result.get_travel()
Expand Down

0 comments on commit 8b0315e

Please sign in to comment.