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, lower: bool, 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=not lower and kind in ["gentle","steep"] var t:=.80 if descending else .05 var direction: Vector3=lib.path_tangent(kind,0,branch,lower) var start: Vector3 var target: Vector3 var exit_direction: Vector3 if lower: var guide: Dictionary=lib.lower_passage_spec(kind) arena.add_child(lib.create_box(Vector3(4,.1,4),Vector3(0,float(guide.floor_y)-.05,0),Color.GRAY)) start=lib.path_point(kind,0,branch,true)-direction*.35+Vector3.UP*.015 start.y=float(guide.center_height)+.015 target=lib.path_point(kind,1,branch,true) target.y=float(guide.center_height) exit_direction=lib.path_tangent(kind,1,branch,true) else: 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 ("lower" if lower 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].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_cw","turn_ccw","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,false) await run_path(kind,q,branch,true) for q in range(4): for branch in range(2):await run_path("y_merge",q,branch,false,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)