Files
IntoHole/playtest/baseline/checks/validate_physics.gd
T
Yanchen d1141b04c7 feat: 更新 IntoHole 的球体、滑槽和环境资产
- 为铁球、陶瓷球和木球制作风格化材质,归档 CC0 原始参考
- 更新方形球洞和实心滑槽,统一 90° 弯槽并兼容旧存档
- 用顺流泡沫表示水流方向,为冰块制作三种交错的裂纹
- 同步 Blender 源资产、Godot 加载、工坊和试玩预览
2026-11-19 16:18:41 +08:00

179 lines
8.3 KiB
GDScript

extends SceneTree
const Library = preload("res://playtest/baseline/scripts/asset_library.gd")
var lib = Library.new()
var arena: Node3D
var results: Array = []
var failures := 0
func _initialize() -> void:
call_deferred("run")
func vec(v: Vector3) -> Array:
return [snappedf(v.x,.000001),snappedf(v.y,.000001),snappedf(v.z,.000001)]
func clear_arena() -> void:
if is_instance_valid(arena):
root.remove_child(arena)
arena.queue_free()
arena=Node3D.new()
root.add_child(arena)
await physics_frame
await physics_frame
func record(name: String, passed: bool, detail: Dictionary) -> void:
results.append({"name":name,"passed":passed,"detail":detail})
if not passed:failures+=1
print(("PASS " if passed else "FAIL ")+name+" "+JSON.stringify(detail))
func run_path(kind: String, rotation_step: int, branch: int, connected := false) -> void:
await clear_arena()
var basis:=Basis(Vector3.UP,-rotation_step*PI/2)
arena.add_child(lib.create_piece(kind,Vector3.ZERO,rotation_step))
var sphere=lib.create_ball()
sphere.freeze=true
var descending: bool=kind in ["gentle","steep"]
var t:=.80 if descending else .05
var direction: Vector3=lib.path_tangent(kind,0,branch)
var start: Vector3
var target: Vector3
var exit_direction: Vector3
start=lib.ball_on_surface(kind,t,branch)
target=lib.ball_on_surface(kind,0 if descending else 1,branch)
exit_direction=-lib.path_tangent(kind,0,branch) if descending else lib.path_tangent(kind,1,branch)
if connected:
for local_center in lib.y_connectors():arena.add_child(lib.create_piece("straight",basis*local_center,rotation_step))
start=lib.path_point(kind,0,branch)+Vector3(-.65,-float(lib.catalog[kind].grooveRadius)+sphere.radius+.018,0)
target=lib.path_point(kind,1,branch)+Vector3(.85,-float(lib.catalog[kind].grooveRadius)+sphere.radius,0)
direction=Vector3.RIGHT
arena.add_child(sphere)
sphere.position=basis*start
await physics_frame
sphere.freeze=false
var initial_speed: float=1.6 if connected else 1.2
sphere.linear_velocity=Vector3.ZERO if descending else basis*direction*initial_speed
sphere.angular_velocity=Vector3.UP.cross(sphere.linear_velocity)/sphere.radius
var closest:=100.0
var reached:=false
var contact_frames:=0
var highest_speed:=0.0
var trace: Array=[]
var time_at_exit:=-1.0
for frame in range(720):
await physics_frame
var p: Vector3=basis.inverse()*sphere.position
var delta_to_goal:=p-target
var axial:=delta_to_goal.dot(exit_direction)
var lateral:=(delta_to_goal-exit_direction*axial).length()
closest=minf(closest,delta_to_goal.length())
highest_speed=maxf(highest_speed,sphere.linear_velocity.length())
if sphere.contact_count>0:contact_frames+=1
if frame%12==0:trace.append({"t":frame/120.0,"p":vec(p),"v":vec(basis.inverse()*sphere.linear_velocity)})
if axial>.04 and lateral<.22:
reached=true
time_at_exit=frame/120.0
break
if p.y < -2:break
var path_name:="connected" if connected else "upper"
record("%s_%s_r%d_b%d" % [path_name,kind,rotation_step*90,branch],reached and contact_frames>0,
{"reached_exit":reached,"time":time_at_exit,"initial_speed":0.0 if descending else initial_speed,"closest_m":closest,"contact_frames":contact_frames,"max_speed":highest_speed,"final":vec(basis.inverse()*sphere.position),"trace":trace})
func run() -> void:
Engine.physics_ticks_per_second=120
await clear_arena()
var falling=lib.create_ball()
falling.drag_enabled=false
falling.rolling_enabled=false
arena.add_child(falling)
falling.position=Vector3(0,10,0)
await physics_frame
var start_y: float=falling.position.y
var start_v: float=falling.linear_velocity.y
for i in range(120):await physics_frame
var expected_drop: float=-start_v+4.905
var observed_drop: float=start_y-falling.position.y
record("vacuum_freefall_1s",absf((falling.linear_velocity.y-start_v)+9.81)<.02 and absf(observed_drop-expected_drop)<.05,
{"initial_velocity":start_v,"velocity_delta":falling.linear_velocity.y-start_v,"distance":observed_drop,"analytic_distance":expected_drop,"position_error":absf(observed_drop-expected_drop)})
await clear_arena()
arena.add_child(lib.create_piece("straight"))
var resting=lib.create_ball()
arena.add_child(resting)
resting.position=Vector3(0,1.8,0)
for i in range(300):await physics_frame
record("sphere_groove_rest",absf(resting.position.y-(1.0-float(lib.catalog["straight"].grooveRadius)+resting.radius))<.035 and resting.linear_velocity.length()<.1,
{"position":vec(resting.position),"speed":resting.linear_velocity.length(),"contacts":resting.contact_count})
# An independent analytic benchmark: sphere rolling on a 30 degree plane.
await clear_arena()
var plane=lib.create_box(Vector3(20,.2,4),Vector3(0,-.1,0),Color.GRAY)
plane.rotation.z=-PI/6
arena.add_child(plane)
var ramp_ball=lib.create_ball()
ramp_ball.drag_enabled=false
ramp_ball.rolling_enabled=false
arena.add_child(ramp_ball)
var normal: Vector3=plane.basis*Vector3.UP
var downhill: Vector3=plane.basis*Vector3.RIGHT
ramp_ball.position=plane.transform*Vector3(0,.1,0)+normal*(ramp_ball.radius+.005)
for i in range(30):await physics_frame
var first_velocity: float=ramp_ball.linear_velocity.dot(downhill)
for i in range(60):await physics_frame
var acceleration: float=(ramp_ball.linear_velocity.dot(downhill)-first_velocity)/.5
var slip: float=absf(ramp_ball.linear_velocity.dot(downhill)-ramp_ball.angular_velocity.length()*ramp_ball.radius)
record("solid_sphere_30_degree_plane",absf(acceleration-3.503571)<.09 and slip<.06,{"acceleration":acceleration,"analytic_acceleration":3.503571,"rolling_slip":slip})
var horizontal_speeds: Array=[]
for enabled in [false,true]:
await clear_arena()
arena.add_child(lib.create_box(Vector3(30,.2,4),Vector3(0,-.1,0),Color.GRAY))
var rolling_ball=lib.create_ball()
rolling_ball.drag_enabled=false
rolling_ball.rolling_enabled=enabled
arena.add_child(rolling_ball)
rolling_ball.position=Vector3(0,rolling_ball.radius+.001,0)
rolling_ball.linear_velocity=Vector3(2,0,0)
rolling_ball.angular_velocity=Vector3(0,0,-2/rolling_ball.radius)
for i in range(240):await physics_frame
horizontal_speeds.append(rolling_ball.linear_velocity.length())
record("rolling_resistance_dissipates",absf(horizontal_speeds[0]-2)<.05 and horizontal_speeds[1]<horizontal_speeds[0]-.15,{"disabled_enabled_speeds":horizontal_speeds})
await clear_arena()
var left=lib.create_ball()
var right=lib.create_ball()
for sphere in [left,right]:
sphere.gravity_scale=0
sphere.drag_enabled=false
sphere.rolling_enabled=false
arena.add_child(sphere)
left.position=Vector3(-1,2,0);right.position=Vector3(1,2,0)
left.linear_velocity=Vector3.RIGHT;right.linear_velocity=Vector3.LEFT
for i in range(150):await physics_frame
var momentum_error: float=(left.linear_velocity+right.linear_velocity).length()
record("ball_to_ball_collision_enabled",left.linear_velocity.x<-.02 and right.linear_velocity.x>.02 and momentum_error<.02,{"left_velocity":vec(left.linear_velocity),"right_velocity":vec(right.linear_velocity),"normalized_momentum_error":momentum_error})
for kind in ["straight","turn_90","gentle","steep","y_merge"]:
for q in range(4):
for branch in range(2 if kind=="y_merge" else 1):
await run_path(kind,q,branch)
for q in range(4):
for branch in range(2):await run_path("y_merge",q,branch,true)
# Air response independently of contacts; compare actual simulated velocities.
var air_velocities: Array=[]
for mass_factor in [1.0,10.0]:
await clear_arena()
var sphere=lib.create_ball()
sphere.gravity_scale=0
sphere.mass*=mass_factor
sphere.wind_velocity=Vector3(20,0,0)
arena.add_child(sphere)
sphere.position=Vector3(0,5,0)
for i in range(60):await physics_frame
air_velocities.append(sphere.linear_velocity.x)
var ratio: float=air_velocities[0]/maxf(air_velocities[1],.000001)
record("wind_mass_response",absf(ratio-10)<.15,{"speeds":air_velocities,"ratio":ratio})
var output={"engine":Engine.get_version_info().string,"physics_engine":ProjectSettings.get_setting("physics/3d/physics_engine"),"ticks_per_second":120,"passed":results.size()-failures,"failed":failures,"results":results}
var path: String=ProjectSettings.globalize_path("res://playtest/artifacts/baseline/physics_report.json")
var file:=FileAccess.open(path,FileAccess.WRITE)
file.store_string(JSON.stringify(output," "))
file.close()
print("PHYSICS_REPORT "+path+" failures="+str(failures))
if is_instance_valid(arena):arena.queue_free()
await process_frame
quit(1 if failures else 0)