From c3f3beaa55fcbdb8833167403bdb5c95d423c7d2 Mon Sep 17 00:00:00 2001 From: Sascha Date: Wed, 13 Sep 2023 17:40:17 +0200 Subject: [PATCH] free bouncing with the floor --- project.godot | 8 +++++++- scenes/OrangeBox.tscn | 6 +++--- scenes/World.tscn | 29 +++++++++++++++++++++-------- scripts/Box.cs | 13 +++++++++---- scripts/Floor.cs | 14 ++++++++++++++ scripts/World.cs | 4 ++-- 6 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 scripts/Floor.cs diff --git a/project.godot b/project.godot index 442428d..0b84c8a 100644 --- a/project.godot +++ b/project.godot @@ -13,7 +13,13 @@ config_version=5 config/name="Evolution" run/main_scene="res://scenes/World.tscn" config/features=PackedStringArray("4.1", "C#", "Forward Plus") -config/icon="res://graphics/icon.svg" +config/icon="res://graphics/Orange/texture_01.png" + +[display] + +window/size/viewport_width=1920 +window/size/viewport_height=1024 +window/size/mode=2 [dotnet] diff --git a/scenes/OrangeBox.tscn b/scenes/OrangeBox.tscn index a2b0891..3a9b1da 100644 --- a/scenes/OrangeBox.tscn +++ b/scenes/OrangeBox.tscn @@ -11,12 +11,12 @@ albedo_texture = ExtResource("1_kfglw") [node name="OrangeBox" type="Node3D"] script = ExtResource("1_rm42w") -[node name="OrangeBox" type="RigidBody3D" parent="."] +[node name="OrangeBoxRigidBody3D" type="RigidBody3D" parent="."] mass = 1.5 -[node name="CollisionShapeBox" type="CollisionShape3D" parent="OrangeBox"] +[node name="OrangeBoxCollisionShapeBox" type="CollisionShape3D" parent="OrangeBoxRigidBody3D"] transform = Transform3D(1.5, 0, 0, 0, 1.5, 0, 0, 0, 1.5, 0, 0, 0) shape = SubResource("BoxShape3D_6js7j") -[node name="CSGBox" type="CSGBox3D" parent="OrangeBox/CollisionShapeBox"] +[node name="OrangeBoxCSGBox" type="CSGBox3D" parent="OrangeBoxRigidBody3D/OrangeBoxCollisionShapeBox"] material_override = SubResource("StandardMaterial3D_dsxvl") diff --git a/scenes/World.tscn b/scenes/World.tscn index 325e192..7ede122 100644 --- a/scenes/World.tscn +++ b/scenes/World.tscn @@ -1,7 +1,11 @@ -[gd_scene load_steps=4 format=3 uid="uid://bwn0di86nkots"] +[gd_scene load_steps=6 format=3 uid="uid://bwn0di86nkots"] [ext_resource type="Script" path="res://scripts/World.cs" id="1_2chdl"] [ext_resource type="Texture2D" uid="uid://bw4vb1yfno12m" path="res://graphics/Light/texture_06.png" id="1_7bu66"] +[ext_resource type="Script" path="res://scripts/Floor.cs" id="3_q36sk"] + +[sub_resource type="BoxShape3D" id="BoxShape3D_fl40q"] +size = Vector3(100, 0.1, 100) [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_t4i04"] albedo_texture = ExtResource("1_7bu66") @@ -10,15 +14,24 @@ uv1_scale = Vector3(8, 8, 8) [node name="World" type="Node3D"] script = ExtResource("1_2chdl") -[node name="Floor" type="CSGBox3D" parent="."] -transform = Transform3D(50, 0, 0, 0, 0.1, 0, 0, 0, 50, 0, 0, 0) -use_collision = true -material = SubResource("StandardMaterial3D_t4i04") - [node name="Camera3D" type="Camera3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 0.925183, 0.379521, 0, -0.379521, 0.925183, 0, 14.6844, 32.8237) -fov = 90.0 +transform = Transform3D(1, 0, 0, 0, 0.925183, 0.379521, 0, -0.379521, 0.925183, 0, 24.4204, 48.4601) +fov = 120.0 [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] transform = Transform3D(1, 0, 0, 0, 0.826207, 0.563367, 0, -0.563367, 0.826206, 0, 14.8709, 19.8929) shadow_enabled = true + +[node name="Floor" type="Node3D" parent="."] +script = ExtResource("3_q36sk") + +[node name="FloorRigidBody3D" type="RigidBody3D" parent="Floor"] +mass = 1000.0 +gravity_scale = 0.25 + +[node name="FloorCollisionShape3D" type="CollisionShape3D" parent="Floor/FloorRigidBody3D"] +shape = SubResource("BoxShape3D_fl40q") + +[node name="FloorCSGBox3D" type="CSGBox3D" parent="Floor/FloorRigidBody3D/FloorCollisionShape3D"] +transform = Transform3D(100, 0, 0, 0, 0.1, 0, 0, 0, 100, 0, 0, 0) +material = SubResource("StandardMaterial3D_t4i04") diff --git a/scripts/Box.cs b/scripts/Box.cs index de7dda3..74adaea 100644 --- a/scripts/Box.cs +++ b/scripts/Box.cs @@ -3,11 +3,16 @@ using Godot; namespace Evolution.scripts; public partial class Box : Node3D { - private RigidBody3D rigidbody3d; - public override void _Ready() => rigidbody3d = GetChild(0); + private RigidBody3D rb; + private Vector3 rotation; + private const int randomRange = 100; + public override void _Ready() { + rb = GetChild(0); + rotation = new(GD.RandRange(-randomRange, randomRange), GD.RandRange(-randomRange, randomRange), GD.RandRange(-randomRange, randomRange)); + } public override void _Process(double delta) { - Vector3 rotation = new(GD.RandRange(-5, 5), GD.RandRange(-5, 5), GD.RandRange(-5, 5)); - // rigidbody3d.Rotate(rotation, 0.1f); + rb.AngularVelocity = rotation * (float)delta; + if (rb.Position.Y < -50) QueueFree(); //Clear all boxes outside } } \ No newline at end of file diff --git a/scripts/Floor.cs b/scripts/Floor.cs new file mode 100644 index 0000000..52b1c8b --- /dev/null +++ b/scripts/Floor.cs @@ -0,0 +1,14 @@ +using Godot; + +namespace Evolution.scripts; + +public partial class Floor : Node3D { + private RigidBody3D rb; + public override void _Ready() => rb = (RigidBody3D)FindChild("FloorRigidBody3D"); + + public override void _Process(double delta) { + if (Input.IsKeyPressed(Key.Space)) { + rb.ApplyImpulse(new(0, GD.RandRange(1000, 2000), 0)); + } + } +} \ No newline at end of file diff --git a/scripts/World.cs b/scripts/World.cs index be9572d..e9306af 100644 --- a/scripts/World.cs +++ b/scripts/World.cs @@ -25,9 +25,9 @@ public partial class World : Node3D { private void CreateNewBox() { Node newBox = boxScenes[GD.RandRange(0, boxScenes.Count - 1)].Instantiate(); RigidBody3D rb = newBox.GetChild(0); - rb.Position = new(GD.RandRange(-25, 25), GD.RandRange(25, 50), GD.RandRange(-25, 25)); + rb.Position = new(GD.RandRange(-50, 50), GD.RandRange(50, 100), GD.RandRange(-50, 50)); rb.Rotation = new(GD.RandRange(-30, 30), GD.RandRange(-30, 30), GD.RandRange(-30, 30)); - float mass =(float)GD.RandRange(0.1, 1); + float mass =(float)GD.RandRange(0.1, 3); rb.Mass = mass; CollisionShape3D cs = rb.GetChild(0); cs.Scale = new(mass, mass, mass);