| … | … | @@ -1,21 +1,54 @@ |
| 1 | 1 | extends RigidBody |
| 2 | 2 | |
| 3 | | var rolling_force = 10 |
| 4 | | var rolling_force_max : float = 50 |
| 3 | enum BallPhysicsMode { |
| 4 | FREE_FLY, |
| 5 | ENGINE_PHYSICS_ROLL, |
| 6 | ENGINE_PHYSICS_LINEAR, |
| 7 | PHYSICS_MAX |
| 8 | } |
| 9 | |
| 10 | var rolling_force : float = 10.0 |
| 11 | var rolling_force_max : float = 50.0 |
| 12 | |
| 13 | var linear_force : float = 2.0 |
| 14 | |
| 15 | var freefly_force : float = 3.0 |
| 16 | |
| 17 | var ball_physics_mode : int = BallPhysicsMode.ENGINE_PHYSICS_LINEAR |
| 5 | 18 | var bFirstPersonCamera = false |
| 6 | | var bDebugPaused : bool = false |
| 7 | | var ballCameraOriginalOrigin : Vector3 |
| 8 | 19 | |
| 9 | 20 | var lVelocity : Label |
| 10 | 21 | var lAngularVelocity : Label |
| 22 | var lBallPhysicsMode : Label |
| 23 | var sldBallFrictionSlider : Slider |
| 11 | 24 | |
| 12 | 25 | func _ready(): |
| 13 | 26 | lVelocity = get_tree().get_current_scene().get_node("DebugUI").get_node("BallVelocity_Value") |
| 14 | 27 | lAngularVelocity = get_tree().get_current_scene().get_node("DebugUI").get_node("BallAngularVelocity_Value") |
| 28 | lBallPhysicsMode = get_tree().get_current_scene().get_node("DebugUI").get_node("BallPhysicsMode_Value") |
| 29 | sldBallFrictionSlider = get_tree().get_current_scene().get_node("DebugUI").get_node("BallFrictionSlider") |
| 30 | |
| 31 | updateBallPhysicsMode() |
| 15 | 32 | |
| 16 | 33 | func _process(_delta: float) -> void: |
| 17 | 34 | pass |
| 18 | 35 | |
| 36 | func updateBallPhysicsMode() -> void: |
| 37 | if(ball_physics_mode == BallPhysicsMode.FREE_FLY): |
| 38 | lBallPhysicsMode.text = "Free Fly" |
| 39 | self.angular_velocity = Vector3(0, 0, 0) |
| 40 | self.rotation = Vector3(0, 0, 0) |
| 41 | self.linear_velocity = Vector3(0, 0, 0) |
| 42 | self.mode = RigidBody.MODE_STATIC |
| 43 | elif(ball_physics_mode == BallPhysicsMode.ENGINE_PHYSICS_ROLL): |
| 44 | lBallPhysicsMode.text = "Engine Physics (Roll)" |
| 45 | self.physics_material_override.friction = 1.0 |
| 46 | self.mode = RigidBody.MODE_RIGID |
| 47 | else: |
| 48 | lBallPhysicsMode.text = "Engine Physics (Linear)" |
| 49 | self.physics_material_override.friction = 0.035 |
| 50 | self.mode = RigidBody.MODE_RIGID |
| 51 | |
| 19 | 52 | func checkAngularVelocity(delta : float, angVelocity : float, bAdd : bool) -> float: |
| 20 | 53 | if bAdd: |
| 21 | 54 | angVelocity = angVelocity + rolling_force*delta |
| … | … | @@ -36,9 +69,6 @@ func _physics_process(delta: float) -> void: |
| 36 | 69 | # else: |
| 37 | 70 | # bFirstPersonCamera = true |
| 38 | 71 | |
| 39 | | if bDebugPaused: |
| 40 | | return |
| 41 | | |
| 42 | 72 | #if false: |
| 43 | 73 | # ballCamera.global_transform.origin = lerp( |
| 44 | 74 | # ballCamera.global_transform.origin, |
| … | … | @@ -48,22 +78,47 @@ func _physics_process(delta: float) -> void: |
| 48 | 78 | # ballCamera.global_transform.origin, |
| 49 | 79 | # global_transform.origin, 1.0 |
| 50 | 80 | # ) |
| 51 | | |
| 52 | 81 | |
| 82 | if Input.is_action_just_pressed("ball_cycle_physics_mode"): |
| 83 | ball_physics_mode = (ball_physics_mode + 1) % BallPhysicsMode.PHYSICS_MAX |
| 84 | if(ball_physics_mode == BallPhysicsMode.FREE_FLY): |
| 85 | self.mode = RigidBody.MODE_STATIC |
| 86 | else: |
| 87 | self.mode = RigidBody.MODE_RIGID |
| 88 | updateBallPhysicsMode() |
| 53 | 89 | |
| 54 | | #$FloorCheck.global_transform.origin = global_transform.origin |
| 90 | |
| 91 | #if(ball_physics_mode == BallPhysicsMode.FREE_FLY): |
| 92 | # return |
| 93 | |
| 55 | 94 | if Input.is_action_pressed("ball_roll_up"): |
| 56 | | # angular_velocity.x = checkAngularVelocity(delta, angular_velocity.x, false) |
| 57 | | angular_velocity.x = clamp(angular_velocity.x - rolling_force*delta, rolling_force_max * -1, rolling_force_max) |
| 95 | if(ball_physics_mode == BallPhysicsMode.ENGINE_PHYSICS_ROLL): |
| 96 | angular_velocity.x = clamp(angular_velocity.x - rolling_force*delta, rolling_force_max * -1, rolling_force_max) |
| 97 | elif(ball_physics_mode == BallPhysicsMode.FREE_FLY): |
| 98 | self.translate( Vector3(0, 0, -freefly_force) * delta) |
| 99 | else: |
| 100 | linear_velocity.z -= (linear_force * delta); |
| 58 | 101 | elif Input.is_action_pressed("ball_roll_down"): |
| 59 | | # angular_velocity.x = checkAngularVelocity(delta, angular_velocity.x, true) |
| 60 | | angular_velocity.x = clamp(angular_velocity.x + rolling_force*delta, rolling_force_max * -1, rolling_force_max) |
| 102 | if(ball_physics_mode == BallPhysicsMode.ENGINE_PHYSICS_ROLL): |
| 103 | angular_velocity.x = clamp(angular_velocity.x + rolling_force*delta, rolling_force_max * -1, rolling_force_max) |
| 104 | elif(ball_physics_mode == BallPhysicsMode.FREE_FLY): |
| 105 | self.translate( Vector3(0, 0, freefly_force) * delta) |
| 106 | else: |
| 107 | linear_velocity.z += (linear_force * delta); |
| 61 | 108 | if Input.is_action_pressed("ball_roll_left"): |
| 62 | | #angular_velocity.z = checkAngularVelocity(delta, angular_velocity.z, true) |
| 63 | | angular_velocity.z = clamp(angular_velocity.z + rolling_force*delta, rolling_force_max * -1, rolling_force_max) |
| 109 | if(ball_physics_mode == BallPhysicsMode.ENGINE_PHYSICS_ROLL): |
| 110 | angular_velocity.z = clamp(angular_velocity.z + rolling_force*delta, rolling_force_max * -1, rolling_force_max) |
| 111 | elif(ball_physics_mode == BallPhysicsMode.FREE_FLY): |
| 112 | self.translate( Vector3(-freefly_force, 0, 0) * delta) |
| 113 | else: |
| 114 | linear_velocity.x -= (linear_force * delta); |
| 64 | 115 | elif Input.is_action_pressed("ball_roll_right"): |
| 65 | | #angular_velocity.z = checkAngularVelocity(delta, angular_velocity.z, false) |
| 66 | | angular_velocity.z = clamp(angular_velocity.z - rolling_force*delta, rolling_force_max * -1, rolling_force_max) |
| 116 | if(ball_physics_mode == BallPhysicsMode.ENGINE_PHYSICS_ROLL): |
| 117 | angular_velocity.z = clamp(angular_velocity.z - rolling_force*delta, rolling_force_max * -1, rolling_force_max) |
| 118 | elif(ball_physics_mode == BallPhysicsMode.FREE_FLY): |
| 119 | self.translate( Vector3(freefly_force, 0, 0) * delta) |
| 120 | else: |
| 121 | linear_velocity.x += (linear_force * delta); |
| 67 | 122 | |
| 68 | 123 | #if Input.is_action_just_pressed("jump") and $FloorCheck.is_colliding(): |
| 69 | 124 | # apply_impulse(Vector3(), Vector3.UP*1000) |