From 15cae7fb8e87947aefdac8e76551490acf46e50c Mon Sep 17 00:00:00 2001 From: Aiksi Date: Wed, 6 Sep 2023 08:54:43 +0300 Subject: [PATCH] perfect --- Ball/ball_test.gd | 38 ++++++++++++++++---------------------- Ball/ball_test.tscn | 32 +++++--------------------------- 2 files changed, 21 insertions(+), 49 deletions(-) diff --git a/Ball/ball_test.gd b/Ball/ball_test.gd index 84c0374..368338e 100644 --- a/Ball/ball_test.gd +++ b/Ball/ball_test.gd @@ -8,42 +8,36 @@ extends RigidBody3D @export var gravity : Vector3 @export var stiffness : float -var previous_collision_normal_array = [] -var normalize_velocity = true -var previous_travel : Vector3 func _ready(): - body.add_collision_exception_with(body_anchor) - body_anchor.add_collision_exception_with(body) linear_velocity = linear_velocity.normalized() * speed -# Engine.time_scale = 1 + DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN) func _physics_process(delta): if global_position.length() >= 100: - print("a") + print("ball escaped") queue_free() + + # Visual var collision : KinematicCollision3D = body.move_and_collide(linear_velocity * delta, true) if collision: var collision_position = collision.get_position() - $TestMesh.global_position = collision_position - mesh.look_at($TestMesh.global_position, Vector3.UP) + mesh.look_at(collision_position, Vector3.UP) + if collision.get_collider().owner.is_in_group("balls"): mesh.scale.z = (global_position - collision.get_collider().global_position).length() else: mesh.scale.z = (collision_position - global_position).length() * 2 - mesh.scale.x = 1.5 - mesh.scale.z / 2 - mesh.scale.y = 1.5 - mesh.scale.z / 2 -# var current_travel = collision.get_travel().normalized() -# linear_velocity += current_travel * pow(speed, 2) * delta -# normalize_velocity = false + var scalexy = clamp(sqrt(1 / mesh.scale.z), 0.0, 2.0) + mesh.scale.x = scalexy + mesh.scale.y = scalexy else: mesh.scale = Vector3.ONE - linear_velocity = linear_velocity.normalized() * speed - constant_force = gravity + (body_anchor.global_position - global_position) * 10 * pow(speed, 2) - body_anchor.velocity = linear_velocity - body_anchor.move_and_slide() - - - - + # Physics + body_anchor.velocity = (global_position - body_anchor.global_position).normalized() * speed + if not body_anchor.move_and_slide(): + body_anchor.global_position = global_position + + constant_force = gravity + (body_anchor.global_position - global_position) * stiffness * pow(speed, 2) + linear_velocity = linear_velocity.normalized() * clamp(linear_velocity.length(), 0, speed) diff --git a/Ball/ball_test.tscn b/Ball/ball_test.tscn index dd56b85..abd0398 100644 --- a/Ball/ball_test.tscn +++ b/Ball/ball_test.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=3 uid="uid://dd1e3u5cyy3mk"] +[gd_scene load_steps=7 format=3 uid="uid://dd1e3u5cyy3mk"] [ext_resource type="Material" uid="uid://ccktnp41cys0r" path="res://Ball/Meshes/ball_material.tres" id="1_5n3i6"] [ext_resource type="Script" path="res://Ball/ball_test.gd" id="1_s3xmr"] @@ -12,28 +12,17 @@ radius = 0.1 [sub_resource type="SphereShape3D" id="SphereShape3D_dwt7q"] -[sub_resource type="BoxMesh" id="BoxMesh_1auv0"] - -[sub_resource type="SphereMesh" id="SphereMesh_xquva"] -radius = 0.2 -height = 0.4 - -[sub_resource type="SphereMesh" id="SphereMesh_bnhf3"] - -[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_5h0ki"] -transparency = 1 -albedo_color = Color(1, 0.372549, 1, 0.701961) - [node name="BallBase" type="RigidBody3D" groups=["balls"]] collision_layer = 0 collision_mask = 0 gravity_scale = 0.0 -continuous_cd = true lock_rotation = true linear_damp_mode = 1 angular_damp_mode = 1 script = ExtResource("1_s3xmr") speed = 10.0 +gravity = Vector3(0, -10, 0) +stiffness = 10.0 [node name="CollisionShape3D" type="CollisionShape3D" parent="."] shape = SubResource("SphereShape3D_jblqj") @@ -54,19 +43,8 @@ shape = SubResource("SphereShape3D_862wy") top_level = true collision_layer = 8 collision_mask = 9 +motion_mode = 1 +wall_min_slide_angle = 0.0 [node name="CollisionShape3D" type="CollisionShape3D" parent="AnchorBody"] shape = SubResource("SphereShape3D_dwt7q") - -[node name="MeshInstance3D" type="MeshInstance3D" parent="AnchorBody"] -visible = false -mesh = SubResource("BoxMesh_1auv0") - -[node name="TestMesh" type="MeshInstance3D" parent="."] -visible = false -mesh = SubResource("SphereMesh_xquva") - -[node name="TestMesh2" type="MeshInstance3D" parent="."] -visible = false -mesh = SubResource("SphereMesh_bnhf3") -surface_material_override/0 = SubResource("StandardMaterial3D_5h0ki")