初步物理计算

This commit is contained in:
des
2026-01-06 00:18:13 +08:00
parent 89e0b55794
commit e349a9fc27
2 changed files with 48 additions and 20 deletions

View File

@@ -1,29 +1,56 @@
extends CharacterBody3D extends CharacterBody3D
var speed = 14 ## 角色的质量,单位 kg
const weight := 60
## 角色前进的动力,同样先简化处理,单位 N
const power := 800
## 一个最简单的阻尼系数[br]
## 阻力计算公式 速度² × 阻尼系数[br]
## 假设最高速度是慢跑 10km/h , 2.77m/s那么阻力需要在这个速度下维持和动力平衡。也就是...104.3
const damping := 104.3
## 最后一次计算之后的,当前运动状态的向量
var last_vector = Vector3.ZERO
var fall_acceleration = 75 var fall_acceleration = 75
var target_velocity = Vector3.ZERO var target_velocity = Vector3.ZERO
func _physics_process(delta: float): func _physics_process(delta: float) -> void:
var direction = Vector3.ZERO # 指令方向
if Input.is_action_pressed("move_forward"): var cmd_vector_direction := get_cmd_vector()
direction.z -= 1 # 运动方向
if Input.is_action_pressed("move_back"): var pre_vector_direction = last_vector.normalized()
direction.z += 1 # 计算当前运动状态,方向在上一步已经更新过了。剩下的是模量问题。
if Input.is_action_pressed("move_left"): # 指令力
direction.x -= 1 var cmd_vector_power = cmd_vector_direction * power
if Input.is_action_pressed("move_right"): # 阻力
direction.x += 1 var damping_vector = -pre_vector.normalized() * last_vector.length_squared() * damping
if direction != Vector3.ZERO:
direction = direction.normalized() # 若指令方向和运动方向不同,则必然会产生
$pivot.basis = Basis.looking_at(direction) if cmd_vector_direction != pre_vector_direction:
target_velocity.x = direction.x * speed pre_vector_direction = (pre_vector_direction + cmd_vector_direction).normalized()
target_velocity.z = direction.z * speed $pivot.basis = Basis.looking_at(pre_vector_direction)
# 速度计算部分
#target_velocity.x = direction.x * now_speed
#target_velocity.z = direction.z * now_speed
if !is_on_floor(): if !is_on_floor():
target_velocity.y = target_velocity.y - ( fall_acceleration * delta ) target_velocity.y = target_velocity.y - ( fall_acceleration * delta )
velocity = target_velocity velocity = target_velocity
move_and_slide() move_and_slide()
## 获取当前指令的关联向量[br]
## 难道需要两层才生效?
func get_cmd_vector() -> Vector3:
var vector := Vector3.ZERO
if Input.is_action_pressed("move_forward"):
vector.z -= 1
if Input.is_action_pressed("move_back"):
vector.z += 1
if Input.is_action_pressed("move_left"):
vector.x -= 1
if Input.is_action_pressed("move_right"):
vector.x += 1
return vector.normalized()

View File

@@ -3,8 +3,9 @@
[ext_resource type="PackedScene" uid="uid://dolueyckipny0" path="res://art/player.glb" id="1_ed8k8"] [ext_resource type="PackedScene" uid="uid://dolueyckipny0" path="res://art/player.glb" id="1_ed8k8"]
[ext_resource type="Script" uid="uid://di50p6h46nmlw" path="res://palyer.gd" id="1_vgmp0"] [ext_resource type="Script" uid="uid://di50p6h46nmlw" path="res://palyer.gd" id="1_vgmp0"]
[sub_resource type="BoxShape3D" id="BoxShape3D_vgmp0"] [sub_resource type="CylinderShape3D" id="CylinderShape3D_vgmp0"]
size = Vector3(1.954834, 3.081192, 1.1972656) height = 1.2167969
radius = 1.0029297
[node name="palyer" type="CharacterBody3D"] [node name="palyer" type="CharacterBody3D"]
script = ExtResource("1_vgmp0") script = ExtResource("1_vgmp0")
@@ -14,5 +15,5 @@ script = ExtResource("1_vgmp0")
[node name="player" parent="pivot" instance=ExtResource("1_ed8k8")] [node name="player" parent="pivot" instance=ExtResource("1_ed8k8")]
[node name="CollisionShape3D" type="CollisionShape3D" parent="."] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.0023910538, 0.99999714, 0, -0.99999714, 0.0023910538, 0.0345459, 0.12650633, 0.40051335) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.018975854, 0.14160156, 0)
shape = SubResource("BoxShape3D_vgmp0") shape = SubResource("CylinderShape3D_vgmp0")