try MoveAndCollide instead of setting Position directly

pull/11/head
Sascha 2023-10-16 12:00:44 +07:00
parent 51315cfe1d
commit d400f5be3a
7 changed files with 76 additions and 28 deletions

@ -0,0 +1,6 @@
<Project Sdk="Godot.NET.Sdk/4.2.0-dev.6">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
</Project>

File diff suppressed because one or more lines are too long

@ -1,9 +1,12 @@
[gd_scene load_steps=8 format=3 uid="uid://cvlxm2yrohsca"] [gd_scene load_steps=9 format=3 uid="uid://cvlxm2yrohsca"]
[ext_resource type="Texture2D" uid="uid://c48y5hkprbmre" path="res://Textures/prop_asteroid_02_dff.png" id="1_hvwuc"] [ext_resource type="Texture2D" uid="uid://c48y5hkprbmre" path="res://Textures/prop_asteroid_02_dff.png" id="1_hvwuc"]
[ext_resource type="Script" path="res://Scripts/Asteroid.cs" id="1_vm3mg"] [ext_resource type="Script" path="res://Scripts/Asteroid.cs" id="1_vm3mg"]
[ext_resource type="Texture2D" uid="uid://8uqgmlpi6x21" path="res://Textures/prop_asteroid_02_nrm.png" id="2_qs4n0"] [ext_resource type="Texture2D" uid="uid://8uqgmlpi6x21" path="res://Textures/prop_asteroid_02_nrm.png" id="2_qs4n0"]
[sub_resource type="SphereShape3D" id="SphereShape3D_f4akn"]
radius = 0.715
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_n7ttt"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_n7ttt"]
resource_name = "prop_asteroid_02_mat" resource_name = "prop_asteroid_02_mat"
metallic = 0.4 metallic = 0.4
@ -49,11 +52,18 @@ normal_enabled = true
normal_scale = 2.0 normal_scale = 2.0
normal_texture = ExtResource("2_qs4n0") normal_texture = ExtResource("2_qs4n0")
[node name="Asteroid_02" type="Node3D"] [node name="Asteroid_02" type="Node3D" node_paths=PackedStringArray("rigidBody3D")]
transform = Transform3D(0.25, 0, 0, 0, 0.25, 0, 0, 0, 0.25, 0, 0, 0) transform = Transform3D(0.25, 0, 0, 0, 0.25, 0, 0, 0, 0.25, 0, 0, 0)
script = ExtResource("1_vm3mg") script = ExtResource("1_vm3mg")
rigidBody3D = NodePath("RigidBody3D")
[node name="RigidBody3D" type="RigidBody3D" parent="."]
gravity_scale = 0.0
[node name="CollisionShape3D" type="CollisionShape3D" parent="RigidBody3D"]
shape = SubResource("SphereShape3D_f4akn")
[node name="prop_asteroid_02" type="MeshInstance3D" parent="."] [node name="prop_asteroid_02" type="MeshInstance3D" parent="RigidBody3D/CollisionShape3D"]
transform = Transform3D(1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0) transform = Transform3D(1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0)
mesh = SubResource("ArrayMesh_jk0jt") mesh = SubResource("ArrayMesh_jk0jt")
skeleton = NodePath("") skeleton = NodePath("")

@ -1,9 +1,12 @@
[gd_scene load_steps=8 format=3 uid="uid://brqqgidqchi88"] [gd_scene load_steps=9 format=3 uid="uid://brqqgidqchi88"]
[ext_resource type="Script" path="res://Scripts/Asteroid.cs" id="1_ph38x"] [ext_resource type="Script" path="res://Scripts/Asteroid.cs" id="1_ph38x"]
[ext_resource type="Texture2D" uid="uid://4sc40fjd4myy" path="res://Textures/prop_asteroid_03_dff.png" id="1_wdgmr"] [ext_resource type="Texture2D" uid="uid://4sc40fjd4myy" path="res://Textures/prop_asteroid_03_dff.png" id="1_wdgmr"]
[ext_resource type="Texture2D" uid="uid://qy0a83vkelqe" path="res://Textures/prop_asteroid_03_nrm.png" id="2_5yqc5"] [ext_resource type="Texture2D" uid="uid://qy0a83vkelqe" path="res://Textures/prop_asteroid_03_nrm.png" id="2_5yqc5"]
[sub_resource type="SphereShape3D" id="SphereShape3D_iq8kl"]
radius = 0.847
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_pvmxy"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_pvmxy"]
resource_name = "prop_asteroid_03_mat" resource_name = "prop_asteroid_03_mat"
metallic = 0.4 metallic = 0.4
@ -49,11 +52,18 @@ normal_enabled = true
normal_scale = 2.0 normal_scale = 2.0
normal_texture = ExtResource("2_5yqc5") normal_texture = ExtResource("2_5yqc5")
[node name="Asteroid_03" type="Node3D"] [node name="Asteroid_03" type="Node3D" node_paths=PackedStringArray("rigidBody3D")]
transform = Transform3D(0.25, 0, 0, 0, 0.25, 0, 0, 0, 0.25, 0, 0, 0) transform = Transform3D(0.25, 0, 0, 0, 0.25, 0, 0, 0, 0.25, 0, 0, 0)
script = ExtResource("1_ph38x") script = ExtResource("1_ph38x")
rigidBody3D = NodePath("RigidBody3D")
[node name="RigidBody3D" type="RigidBody3D" parent="."]
gravity_scale = 0.0
[node name="CollisionShape3D" type="CollisionShape3D" parent="RigidBody3D"]
shape = SubResource("SphereShape3D_iq8kl")
[node name="prop_asteroid_03" type="MeshInstance3D" parent="."] [node name="prop_asteroid_03" type="MeshInstance3D" parent="RigidBody3D/CollisionShape3D"]
mesh = SubResource("ArrayMesh_rnkma") mesh = SubResource("ArrayMesh_rnkma")
skeleton = NodePath("") skeleton = NodePath("")
surface_material_override/0 = SubResource("StandardMaterial3D_mggxn") surface_material_override/0 = SubResource("StandardMaterial3D_mggxn")

@ -6,7 +6,8 @@
[ext_resource type="Texture2D" uid="uid://dd4f3mkovliof" path="res://Textures/part_jet_flare_dff.png" id="4_87x8f"] [ext_resource type="Texture2D" uid="uid://dd4f3mkovliof" path="res://Textures/part_jet_flare_dff.png" id="4_87x8f"]
[ext_resource type="Texture2D" uid="uid://dtiljorcwbex" path="res://Textures/part_jet_core_dff.png" id="5_034vf"] [ext_resource type="Texture2D" uid="uid://dtiljorcwbex" path="res://Textures/part_jet_core_dff.png" id="5_034vf"]
[sub_resource type="BoxShape3D" id="BoxShape3D_218i1"] [sub_resource type="SphereShape3D" id="SphereShape3D_330dr"]
radius = 0.87
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_gt35v"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_gt35v"]
resource_name = "vehicle_playerShip_metal_mat" resource_name = "vehicle_playerShip_metal_mat"
@ -87,35 +88,35 @@ normal_enabled = true
normal_scale = 2.0 normal_scale = 2.0
normal_texture = ExtResource("3_2vqaj") normal_texture = ExtResource("3_2vqaj")
[node name="PlayerShip" type="Node3D" node_paths=PackedStringArray("jet")] [node name="PlayerShip" type="Node3D" node_paths=PackedStringArray("jet", "rigidBody3D")]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74227e-08, 0, -1, 0, 0, 0) transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74227e-08, 0, -1, 0, 0, 0)
script = ExtResource("1_cpbf1") script = ExtResource("1_cpbf1")
jet = NodePath("RigidBody3D/Jet") jet = NodePath("RigidBody3D/CollisionShape3D/Jet")
rigidBody3D = NodePath("RigidBody3D")
[node name="RigidBody3D" type="RigidBody3D" parent="."] [node name="RigidBody3D" type="RigidBody3D" parent="."]
mass = 100.0 mass = 100.0
gravity_scale = 0.0 gravity_scale = 0.0
[node name="CollisionShape3D" type="CollisionShape3D" parent="RigidBody3D"] [node name="CollisionShape3D" type="CollisionShape3D" parent="RigidBody3D"]
transform = Transform3D(1.2, 0, 0, 0, 0.5, 0, 0, 0, 1.8, 0, 0, 0) shape = SubResource("SphereShape3D_330dr")
shape = SubResource("BoxShape3D_218i1")
[node name="PlayerShipMesh" type="MeshInstance3D" parent="RigidBody3D"] [node name="PlayerShipMesh" type="MeshInstance3D" parent="RigidBody3D/CollisionShape3D"]
transform = Transform3D(0.01, 0, 0, 0, 0, -0.01, 0, 0.01, 0, 0, -0.0836085, 0) transform = Transform3D(0.01, 0, 0, 0, 0, -0.01, 0, 0.01, 0, 0, -0.0836085, 0)
gi_mode = 2 gi_mode = 2
mesh = SubResource("ArrayMesh_gcv6f") mesh = SubResource("ArrayMesh_gcv6f")
skeleton = NodePath("") skeleton = NodePath("")
surface_material_override/0 = SubResource("StandardMaterial3D_jlx1o") surface_material_override/0 = SubResource("StandardMaterial3D_jlx1o")
[node name="Jet" type="Node3D" parent="RigidBody3D"] [node name="Jet" type="Node3D" parent="RigidBody3D/CollisionShape3D"]
visible = false visible = false
[node name="JetCore" type="Sprite3D" parent="RigidBody3D/Jet"] [node name="JetCore" type="Sprite3D" parent="RigidBody3D/CollisionShape3D/Jet"]
transform = Transform3D(1, -3.55271e-14, 3.82137e-15, 3.82137e-15, 4.37114e-08, -1, 3.55271e-14, 1, 4.37114e-08, 0, 0.01, -1.392) transform = Transform3D(1, -3.55271e-14, 3.82137e-15, 3.82137e-15, 4.37114e-08, -1, 3.55271e-14, 1, 4.37114e-08, 0, 0.01, -1.392)
shaded = true shaded = true
texture = ExtResource("5_034vf") texture = ExtResource("5_034vf")
[node name="JetFlare" type="Sprite3D" parent="RigidBody3D/Jet"] [node name="JetFlare" type="Sprite3D" parent="RigidBody3D/CollisionShape3D/Jet"]
transform = Transform3D(1, -2.84217e-14, 3.82137e-15, 3.82137e-15, 4.37114e-08, -1, 2.84217e-14, 1, 4.37114e-08, 0, -0.01, -1.392) transform = Transform3D(1, -2.84217e-14, 3.82137e-15, 3.82137e-15, 4.37114e-08, -1, 2.84217e-14, 1, 4.37114e-08, 0, -0.01, -1.392)
shaded = true shaded = true
texture = ExtResource("4_87x8f") texture = ExtResource("4_87x8f")

@ -1,14 +1,17 @@
using Godot; using Godot;
using System;
[GlobalClass] [GlobalClass]
public partial class Asteroid : Node3D { public partial class Asteroid : Node3D {
[Export] private float speed; [Export] private RigidBody3D rigidBody3D;
[Export] private float size;
[Export] private float rotSpeed = 100f; private float speed;
private float rotSpeed = 100f;
private float size;
private Vector3 rotation; private Vector3 rotation;
public override void _Ready() { public override void _Ready() {
size = GD.Randf(); size = GD.Randf();
rigidBody3D.Mass = size;
speed = GD.Randf() * 5f; speed = GD.Randf() * 5f;
Scale = new(size, size,size); Scale = new(size, size,size);
@ -17,11 +20,17 @@ public partial class Asteroid : Node3D {
} }
public override void _Process(double delta) { public override void _Process(double delta) {
Position += new Vector3(0, 0, speed * (float)delta); // Position += new Vector3(0, 0, speed * (float)delta);
RotationDegrees += rotation * (float)delta; RotationDegrees += rotation * (float)delta;
}
public override void _PhysicsProcess(double delta) {
KinematicCollision3D collision = rigidBody3D.MoveAndCollide(new Vector3(0, 0, speed * (float)delta));
if (collision?.GetCollider() is Node3D collider) GD.Print($"{Name} collides with {collider.GetParentNode3D().Name}");
if (Position.Z > 7) { if (Position.Z > 7) {
GD.Print($"Asteroid {Name} below screen!"); GD.Print($"Asteroid {Name} below screen!");
GameManager.Instance.RemoveLife();
QueueFree(); QueueFree();
} }
} }

@ -3,6 +3,7 @@ using Godot;
public partial class PlayerShip : Node3D { public partial class PlayerShip : Node3D {
[Export] private float moveVelocity = 10f; [Export] private float moveVelocity = 10f;
[Export] private Node3D jet; [Export] private Node3D jet;
[Export] private RigidBody3D rigidBody3D;
private const float jetTimerMax = 0.5f; private const float jetTimerMax = 0.5f;
private float jetTimer = jetTimerMax; private float jetTimer = jetTimerMax;
@ -19,7 +20,7 @@ public partial class PlayerShip : Node3D {
public override void _Ready() => Position = new(0, 0, 5); public override void _Ready() => Position = new(0, 0, 5);
public override void _Process(double delta) { public override void _PhysicsProcess(double delta) {
HandleMovement(delta); HandleMovement(delta);
if (JetActive) { if (JetActive) {
jetTimer -= (float)delta; jetTimer -= (float)delta;
@ -54,7 +55,8 @@ public partial class PlayerShip : Node3D {
rotDir.X = 15f; rotDir.X = 15f;
} }
Position += moveDir; KinematicCollision3D collision = rigidBody3D.MoveAndCollide(moveDir);
// Position += moveDir;
RotationDegrees = rotDir; RotationDegrees = rotDir;
CheckBoundaries(); CheckBoundaries();
} }