Skip to content

Commit

Permalink
PoseWarping Node | Update to Godot 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ywmaa committed Oct 3, 2024
1 parent c074df4 commit ef7cb83
Show file tree
Hide file tree
Showing 67 changed files with 3,833 additions and 440 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Publish-Godot-Project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ env:
PROJECT_NAME: "AMSG" # Needs to be added, PLEASE DON'T USE SPACES
ITCH_PROJECT_NAME: "amsg" # Needs to be added
ITCH_USER_NAME: "ywmaa" # Needs to be added
GODOT_VERSION: 4.1.1 # Needs to be added
GODOT_VERSION: 4.3 # Needs to be added
jobs:
CleanArtifacts:
# This job clears out the previous artifacts made so you don't run out of space in your github account
Expand Down
Binary file modified AMSG_Examples/Character/Animations/Idle.res
100755 → 100644
Binary file not shown.
Binary file modified AMSG_Examples/Character/Animations/JogForward.res
100755 → 100644
Binary file not shown.
Binary file modified AMSG_Examples/Character/Animations/Run.res
100755 → 100644
Binary file not shown.
Binary file modified AMSG_Examples/Character/Animations/Walk.res
100755 → 100644
Binary file not shown.
204 changes: 114 additions & 90 deletions AMSG_Examples/Character/mixamo_character.tscn

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions AMSG_Examples/Maps/LevelManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,3 @@ func create_player(id: int) -> void:

func destroy_player(id: int) -> void:
find_child(str(id),true,false).queue_free()




4 changes: 2 additions & 2 deletions AMSG_Examples/Maps/MovementTestMap.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ transform = Transform3D(1, 0, 0, 0, -0.996865, 0.0791203, 0, -0.0791203, -0.9968
[node name="Stairs" type="Node3D" parent="."]

[node name="CSGBox3D8" type="CSGBox3D" parent="Stairs"]
transform = Transform3D(-0.999963, -0.00595152, -0.00617832, -0.0059514, 0.999982, -3.67712e-05, 0.00617843, 0, -0.99998, -1.42272, 1.61775, 1.38929)
transform = Transform3D(-0.999963, -0.00595152, -0.00617832, -0.0059514, 0.999982, -3.67712e-05, 0.00617843, 0, -0.99998, -0.113655, 1.61365, 1.39355)
use_collision = true
size = Vector3(0.392448, 0.135761, 14.107)
size = Vector3(1.77032, 0.135761, 14.107)
material = ExtResource("3_27oba")

[node name="CSGBox3D9" type="CSGBox3D" parent="Stairs"]
Expand Down
229 changes: 118 additions & 111 deletions AMSG_Examples/Player/Player.tscn

Large diffs are not rendered by default.

20 changes: 13 additions & 7 deletions AMSG_Examples/Player/PlayerController.gd
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ func _physics_process(delta):
return

if lock_system != null && lock_system.is_locked:
direction = Vector3.ZERO
character_component.add_movement_input()
return

#------------------ Input Movement ------------------#
Expand All @@ -69,9 +67,6 @@ func _physics_process(delta):
character_component.add_movement_input(direction, character_component.current_movement_data.run_speed,character_component.current_movement_data.run_acceleration)
else:
character_component.add_movement_input(direction, character_component.current_movement_data.walk_speed,character_component.current_movement_data.walk_acceleration)
else:
direction = Vector3.ZERO
character_component.add_movement_input()


#------------------ Input Crouch ------------------#
Expand Down Expand Up @@ -133,11 +128,22 @@ func _physics_process(delta):
if Input.is_action_just_pressed("interaction"):
character_component.camera_root.Camera.get_node("InteractionRaycast").Interact()

if Input.is_action_just_pressed("show_debug"):
pass
if Input.is_action_just_pressed("switch_distance_matching"):
character_component.pose_warping_active = !character_component.pose_warping_active




var view_changed_recently = false
func _input(event):
if !Engine.is_editor_hint():
if Input.is_key_pressed(KEY_1):
DebugDraw3D.debug_enabled = !DebugDraw3D.debug_enabled
if Input.is_key_pressed(KEY_2):
DebugDraw2D.debug_enabled = !DebugDraw2D.debug_enabled
#if Input.is_key_pressed(KEY_3):
#DebugDrawManager.debug_enabled = !DebugDrawManager.debug_enabled

if event is InputEventMouseMotion:
if !character_component or !controls_the_possessed_character:
return
Expand Down
15 changes: 14 additions & 1 deletion AMSG_Examples/Player/Status.gd
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
extends Control
@onready var character_movement_component = $"../CharacterMovementComponent"

@export var camera_root : CameraComponent
@onready var player = get_parent()
@onready var direction = $Control/Direction
@onready var velocity = $Control/Velocity
@onready var mesh = $Control/mesh

@onready var rich_text_label = $RichTextLabel
@onready var rich_text_label_2 = $RichTextLabel2
var debug_text : String = ""
func _physics_process(_delta):
visible = camera_root.Camera.current
var h_rot = get_parent().get_node("SpringArm3D").transform.basis.get_euler().y
Expand All @@ -14,3 +17,13 @@ func _physics_process(_delta):
# direction.rotation = atan2(player.direction.z, player.direction.x)
velocity.position = Vector2(character_node_velocity.x, character_node_velocity.z) * 10
mesh.rotation = 90-get_node("../Armature").rotation.y - player.rotation.y - .5
if Input.is_action_just_pressed("show_panel"):
rich_text_label.visible = !rich_text_label.visible
debug_text = ""
debug_text += "3D_Debug_Enabled:[color="+true_false_text_color(DebugDraw3D.debug_enabled)+"]" + str(DebugDraw3D.debug_enabled) +"[/color] "
debug_text += "Instructions_Visible:[color="+true_false_text_color(rich_text_label.visible)+"]" + str(rich_text_label.visible) +"[/color] "
debug_text += "Distance_Matching_Enabled:[color="+true_false_text_color(character_movement_component.pose_warping_active)+"]" + str(character_movement_component.pose_warping_active) +"[/color]"
rich_text_label_2.text = debug_text

func true_false_text_color(state:bool) -> String:
return "green" if state else "red"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The Project is made using [Godot](https://github.com/godotengine/godot) 4

you can get Godot 4.2 Stable here : https://godotengine.org/
you can get Godot 4.3 Stable here : https://godotengine.org/

### Watch this video for preview :

Expand Down
1 change: 0 additions & 1 deletion addons/AMSG/AI/AI_Base.gd
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ func _physics_process(delta):
ik_look_at(-$CameraRoot/SpringArm3D.transform.basis.z * 2.0 + Vector3(0.0,1.5,0.0))
Global.rotation_mode.aiming:
ik_look_at(-$CameraRoot/SpringArm3D.transform.basis.z * 2.0 + Vector3(0.0,1.5,0.0))

8 changes: 2 additions & 6 deletions addons/AMSG/Components/AnimationComponents/AnimationBlend.gd
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,11 @@ func _physics_process(_delta):

#On Stopped
if !(Input.is_action_pressed("forward") || Input.is_action_pressed("back") || Input.is_action_pressed("right") || Input.is_action_pressed("left")) and (Input.is_action_just_released("right") || Input.is_action_just_released("back") || Input.is_action_just_released("left") || Input.is_action_just_released("forward")):

var seek_time = 0.1#get_node(anim_player).get_animation(tree_root.get_node("VelocityDirection").get_node("StopAnim").animation).length - movement_script.pose_warping_instance.CalculateStopTime((movement_script.actual_velocity * Vector3(1.0,0.0,1.0)),movement_script.deacceleration * movement_script.input_direction)
movement_script.pose_warping.CalculateStopLocation(movement_script.deacceleration)
var seek_time = 0.0#get_node(anim_player).get_animation(tree_root.get_node("VelocityDirection").get_node("StopAnim").animation).length - movement_script.pose_warping_instance.CalculateStopTime((movement_script.actual_velocity * Vector3(1.0,0.0,1.0)),movement_script.deacceleration * movement_script.input_direction)
set("parameters/VelocityDirection/StopSeek/seek_position",seek_time)
if !movement_script.input_is_moving:
set("parameters/VelocityDirection/Standing/transition_request","Stop")
#Rotate In Place
set("parameters/Turn/blend_amount" , 1 if movement_script.is_rotating_in_place else 0)
set("parameters/RightOrLeft/blend_amount" ,0 if movement_script.rotation_difference_camera_mesh > 0 else 1)




2 changes: 0 additions & 2 deletions addons/AMSG/Components/AttributesComponent/AttributesManager.gd
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ func _ready():
if !(child is GameAttribute):
assert("Only GameAttribute childs are allowed")
attributes[child.attribute_name] = child


135 changes: 63 additions & 72 deletions addons/AMSG/Components/CharacterMovementComponent.gd
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ var gravity : float = ProjectSettings.get_setting("physics/3d/default_gravity")
@export var max_stair_climb_height : float = 0.5

## the distance to the stair that the script will start detecting it
@export var max_close_stair_distance : float = 0.75
@export var max_close_stair_distance : float = 0.5
@export var stair_collision_shape_3d: CollisionShape3D


@export var roll_magnitude := 17.0
Expand Down Expand Up @@ -298,13 +299,19 @@ func _ready():

update_animations()
update_character_movement()

stair_collision_shape_3d.shape.length = max_stair_climb_height
stair_collision_shape_3d.position.y = max_stair_climb_height

func _process(delta):
calc_animation_data()
pose_warping.character_velocity = actual_velocity


var pose_warping_active : bool = true
func _physics_process(delta):
set_movement_info()
apply_deacceleration()
pose_warping.stride_warping_enable = pose_warping_active
pose_warping.slope_warping_enable = pose_warping_active
#Debug()
#
aim_rate_h = abs((camera_root.HObject.rotation.y - previous_aim_rate_h) / delta)
Expand All @@ -315,8 +322,6 @@ func _physics_process(delta):
Global.movement_state.none:
pass
Global.movement_state.grounded:
pose_warping.stride_warping_enable = true
pose_warping.slope_warping_enable = true
#------------------ Rotate Character Mesh ------------------#
match movement_action:
Global.movement_action.none:
Expand All @@ -341,8 +346,6 @@ func _physics_process(delta):
smooth_character_rotation(input_acceleration ,2.0,delta)

Global.movement_state.in_air:
pose_warping.stride_warping_enable = false
pose_warping.slope_warping_enable = false
#------------------ Rotate Character Mesh In Air ------------------#
if mantle_component and !mantle_component.is_climbing:
match rotation_mode:
Expand Down Expand Up @@ -383,9 +386,6 @@ func _physics_process(delta):

if character_node is CharacterBody3D and character_node.is_on_ceiling():
vertical_velocity.y = 0
#------------------ Stair climb ------------------#
#stair movement must happen after gravity so it can override in air status
stair_move()

func crouch_update(delta):
var direct_state = character_node.get_world_3d().direct_space_state
Expand Down Expand Up @@ -414,43 +414,12 @@ func crouch_update(delta):
collision_shape_ref.shape.height = clamp(collision_shape_ref.shape.height,crouch_height,default_height)


func stair_move():
var direct_state = character_node.get_world_3d().direct_space_state
var obs_ray_info : PhysicsRayQueryParameters3D = PhysicsRayQueryParameters3D.new()
obs_ray_info.exclude = [RID(character_node)]
obs_ray_info.from = mesh_ref.global_transform.origin
if movement_direction:
obs_ray_info.to = obs_ray_info.from + Vector3(0, 0, max_close_stair_distance).rotated(Vector3.UP,movement_direction)

#this is used to know if there is obstacle
var first_collision = direct_state.intersect_ray(obs_ray_info)
if first_collision and input_is_moving:
var climb_ray_info : PhysicsRayQueryParameters3D = PhysicsRayQueryParameters3D.new()
climb_ray_info.exclude = [RID(character_node)]
climb_ray_info.from = first_collision.collider.global_position + Vector3(0, max_stair_climb_height, 0)
climb_ray_info.to = first_collision.collider.global_position
var stair_top_collision = direct_state.intersect_ray(climb_ray_info)
if stair_top_collision:
if stair_top_collision.position.y - character_node.global_position.y > 0 and stair_top_collision.position.y - character_node.global_position.y < 0.15:
movement_state = Global.movement_state.grounded
is_moving_on_stair = true
character_node.position.y += stair_top_collision.position.y - character_node.global_position.y
character_node.global_position += Vector3(0, 0, 0.01).rotated(Vector3.UP,movement_direction)
else:
await get_tree().create_timer(0.4).timeout
is_moving_on_stair = false
else:
await get_tree().create_timer(0.4).timeout
is_moving_on_stair = false
else:
await get_tree().create_timer(0.4).timeout
is_moving_on_stair = false



func smooth_character_rotation(Target:Vector3,nodelerpspeed,delta):
mesh_ref.rotation.y = lerp_angle(mesh_ref.rotation.y, atan2(Target.x,Target.z) , delta * nodelerpspeed)

stair_collision_shape_3d.position.x = Target.normalized().x * max_close_stair_distance
stair_collision_shape_3d.position.z = Target.normalized().z * max_close_stair_distance

func set_bone_x_rotation(skeleton,bone_name, x_rot,CharacterRootNode):
var bone = skeleton.find_bone(bone_name)
Expand Down Expand Up @@ -500,49 +469,37 @@ func ik_look_at(position: Vector3):
lookatobject.position = position


var PrevVelocity :Vector3

var prev_velocity :Vector3
var current_max_speed : float
## Adds input to move the character, should be called when Idle too, to execute deacceleration for CharacterBody3D or reset velocity for RigidBody3D.
## when Idle speed and direction should be passed as 0, and deacceleration passed, or leave them empty.
func add_movement_input(direction: Vector3 = Vector3.ZERO, Speed: float = 0, Acceleration: float = deacceleration if character_node is CharacterBody3D else 0) -> void:
func add_movement_input(p_direction: Vector3 = Vector3.ZERO, p_speed: float = 0, Acceleration: float = deacceleration if character_node is CharacterBody3D else 0) -> void:
if mantle_component and mantle_component.is_climbing:
return
var max_speed : float = Speed
input_direction = direction

current_max_speed = p_speed
input_direction = p_direction
input_acceleration = Acceleration * input_direction * (1 if current_max_speed != 0 else -1)
if character_node is RigidBody3D:
if is_flying == false:
velocity.x = direction.x * Acceleration * character_node.mass * get_physics_process_delta_time()
velocity.z = direction.z * Acceleration * character_node.mass * get_physics_process_delta_time()
velocity.x = p_direction.x * Acceleration * character_node.mass * get_physics_process_delta_time()
velocity.z = p_direction.z * Acceleration * character_node.mass * get_physics_process_delta_time()
else:
velocity = direction * Acceleration * character_node.mass * get_physics_process_delta_time()
velocity = p_direction * Acceleration * character_node.mass * get_physics_process_delta_time()

if is_inf(character_node.linear_velocity.length()):
character_node.linear_velocity = velocity
if character_node.linear_velocity.length() > max_speed:
velocity = direction
if character_node.linear_velocity.length() > current_max_speed:
velocity = p_direction
character_node.apply_central_impulse(velocity)

if character_node is CharacterBody3D:
if is_flying == false:
character_node.velocity.x = lerp(character_node.velocity.x,(direction*max_speed).x,Acceleration/(max_speed if max_speed != 0 else (abs(character_node.velocity.x) if character_node.velocity.x != 0 else 1.0))*get_physics_process_delta_time())
character_node.velocity.z = lerp(character_node.velocity.z,(direction*max_speed).z,Acceleration/(max_speed if max_speed != 0 else (abs(character_node.velocity.z) if character_node.velocity.z != 0 else 1.0))*get_physics_process_delta_time())
character_node.velocity.x = lerp(character_node.velocity.x,(p_direction*current_max_speed).x,Acceleration/(current_max_speed if current_max_speed != 0 else 1.0)*get_physics_process_delta_time())
character_node.velocity.z = lerp(character_node.velocity.z,(p_direction*current_max_speed).z,Acceleration/(current_max_speed if current_max_speed != 0 else 1.0)*get_physics_process_delta_time())
else:
character_node.velocity = character_node.velocity.lerp((direction*max_speed),Acceleration/(max_speed if max_speed != 0 else character_node.velocity.x if character_node.velocity.x != 0 else 1.0)*get_physics_process_delta_time())
character_node.velocity = character_node.velocity.lerp((p_direction*current_max_speed),Acceleration/(current_max_speed if current_max_speed != 0 else 1.0)*get_physics_process_delta_time())
character_node.move_and_slide()
# Get the velocity from the character node
var character_node_velocity = character_node.velocity if character_node is CharacterBody3D else character_node.linear_velocity

input_velocity = direction*max_speed if character_node is CharacterBody3D else velocity
movement_direction = atan2(input_velocity.x,input_velocity.z)
input_is_moving = input_velocity.length() > 0.0
input_acceleration = Acceleration * direction * (1 if max_speed != 0 else -1)
#

actual_acceleration = (character_node_velocity - PrevVelocity) / (get_physics_process_delta_time())
PrevVelocity = character_node_velocity
#
actual_velocity = character_node_velocity

#tiltCharacterMesh
if tilt == true:
var MovementDirectionRelativeToCamera = input_velocity.normalized().rotated(Vector3.UP,-camera_root.HObject.transform.basis.get_euler().y)
Expand All @@ -555,6 +512,42 @@ func add_movement_input(direction: Vector3 = Vector3.ZERO, Speed: float = 0, Acc
mesh_ref.rotation.z = lerp(mesh_ref.rotation.z,tiltVector.z,Acceleration * get_physics_process_delta_time())
#

var prev_acceleration : Vector3
var prev_position : Vector3
var current_position : Vector3
func set_movement_info():
input_velocity = input_direction*current_max_speed if character_node is CharacterBody3D else velocity
movement_direction = atan2(input_velocity.x,input_velocity.z)
input_is_moving = input_velocity.length() > 0.0

prev_position = current_position
prev_velocity = actual_velocity
prev_acceleration = actual_acceleration

current_position = character_node.global_position
# Distance/Time = Velocity
actual_velocity = (current_position - prev_position) / get_physics_process_delta_time()
# Delta Velocity / Delta Time = Acceleration
actual_acceleration = (actual_velocity - prev_velocity) / get_physics_process_delta_time()

current_max_speed = 0


func apply_deacceleration():
if input_is_moving:
return
if character_node is CharacterBody3D:
var velocity_without_up : Vector3 = (character_node.velocity*Vector3(1,0,1))
if velocity_without_up == Vector3.ZERO:
velocity_without_up = Vector3.ONE
if is_flying == false:
character_node.velocity.x = lerp(character_node.velocity.x, 0.0, (deacceleration/velocity_without_up.length())*get_physics_process_delta_time())
character_node.velocity.z = lerp(character_node.velocity.z, 0.0, (deacceleration/velocity_without_up.length())*get_physics_process_delta_time())
else:
character_node.velocity.x = lerp(character_node.velocity.x, 0.0, (deacceleration/velocity_without_up.length())*get_physics_process_delta_time())
character_node.velocity.y = lerp(character_node.velocity.y, 0.0, (deacceleration/velocity_without_up.length())*get_physics_process_delta_time())
character_node.velocity.z = lerp(character_node.velocity.z, 0.0, (deacceleration/velocity_without_up.length())*get_physics_process_delta_time())
character_node.move_and_slide()

func calc_animation_data(): # it is used to modify the animation data to get the wanted animation result
animation_is_moving_backward_relative_to_camera = false if -actual_velocity.rotated(Vector3.UP,-camera_root.HObject.transform.basis.get_euler().y).z >= -0.1 else true
Expand All @@ -575,5 +568,3 @@ func jump() -> void:
character_node.apply_impulse(Vector3.UP * jump_magnitude * character_node.mass)
else:
vertical_velocity = Vector3.UP * jump_magnitude


3 changes: 0 additions & 3 deletions addons/AMSG/Components/PlayerGameplayComponent.gd
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ func _physics_process(delta):
add_movement_input(input_direction, current_movement_data.run_speed,current_movement_data.run_acceleration)
else:
add_movement_input(input_direction, current_movement_data.walk_speed,current_movement_data.walk_acceleration)
else:
add_movement_input(input_direction,0,deacceleration)

return
#------------------ Look At ------------------#
match rotation_mode:
Expand Down
2 changes: 0 additions & 2 deletions addons/AMSG/Global.gd
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ enum movement_direction {forward , right, left, backward}
func map_range_clamped(value,InputMin,InputMax,OutputMin,OutputMax):
value = clamp(value,InputMin,InputMax)
return ((value - InputMin) / (InputMax - InputMin) * (OutputMax - OutputMin) + OutputMin)


Loading

0 comments on commit ef7cb83

Please sign in to comment.