From 5f5337c91f12c0d02d9b2c09b88ef3846a17b566 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 30 Sep 2023 22:53:38 +0200 Subject: [PATCH] "Fix" physics --- bottle/bottle.tscn | 2 +- entity/coin.tres | 5 +++-- entity/coin_copper.gd | 5 +++++ entity/coin_copper.tscn | 11 ++++++----- project.godot | 5 +++++ root.tscn | 4 ++-- spawner/spawner.gd | 4 +++- spawner/spawner.tscn | 14 ++++++++++++-- 8 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 entity/coin_copper.gd diff --git a/bottle/bottle.tscn b/bottle/bottle.tscn index 795e641..83d5ffe 100644 --- a/bottle/bottle.tscn +++ b/bottle/bottle.tscn @@ -22,4 +22,4 @@ collision_layer = 3 collision_mask = 0 [node name="Poly" type="CollisionPolygon2D" parent="StaticBody2D"] -polygon = PackedVector2Array(-64, -480, -64, -416, -128, -416, -128, 0, 128, 0, 128, -416, 64, -416, 64, -480, 48, -480, 48, -400, 112, -400, 112, -16, -112, -16, -112, -400, -48, -400, -48, -480) +polygon = PackedVector2Array(-320, -576, -320, 192, 320, 192, 320, -576, 48, -576, 48, -400, 112, -400, 112, -16, -112, -16, -112, -400, -48, -400, -48, -576) diff --git a/entity/coin.tres b/entity/coin.tres index 8799c73..5bfe039 100644 --- a/entity/coin.tres +++ b/entity/coin.tres @@ -1,4 +1,5 @@ -[gd_resource type="PhysicsMaterial" format=3 uid="uid://wjrjkmxp41dt"] +[gd_resource type="PhysicsMaterial" format=3 uid="uid://c6kn1an85lccr"] [resource] -rough = true +friction = 0.5 +bounce = 0.1 diff --git a/entity/coin_copper.gd b/entity/coin_copper.gd new file mode 100644 index 0000000..b4db63b --- /dev/null +++ b/entity/coin_copper.gd @@ -0,0 +1,5 @@ +extends RigidBody2D +class CoinCopper + + +func _integrate_forces(state: PhysicsDirectBodyState2D): diff --git a/entity/coin_copper.tscn b/entity/coin_copper.tscn index 912f3b6..572b778 100644 --- a/entity/coin_copper.tscn +++ b/entity/coin_copper.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=5 format=3 uid="uid://c3kitncwpi42j"] +[gd_scene load_steps=6 format=3 uid="uid://c3kitncwpi42j"] -[ext_resource type="PhysicsMaterial" uid="uid://wjrjkmxp41dt" path="res://entity/coin.tres" id="1_1merf"] +[ext_resource type="PhysicsMaterial" uid="uid://c6kn1an85lccr" path="res://entity/coin.tres" id="1_1merf"] [ext_resource type="Texture2D" uid="uid://dbdkb4vt7dh85" path="res://entity/coin_copper_4.png" id="1_wedw1"] [ext_resource type="Texture2D" uid="uid://2vtvoj6ua3cb" path="res://entity/coin_copper_outline_2.png" id="2_2ifq3"] +[ext_resource type="Script" path="res://entity/coin_copper.gd" id="2_y0e7k"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_c6byl"] size = Vector2(14, 3) @@ -10,15 +11,15 @@ size = Vector2(14, 3) [node name="CoinCopper" type="RigidBody2D"] collision_layer = 5 collision_mask = 7 -center_of_mass_mode = 1 -center_of_mass = Vector2(10, 0) +inertia = 20000.0 physics_material_override = ExtResource("1_1merf") continuous_cd = 1 linear_damp = 0.1 angular_damp = 0.1 +script = ExtResource("2_y0e7k") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] -scale = Vector2(2, 2) +scale = Vector2(3, 3) shape = SubResource("RectangleShape2D_c6byl") [node name="Sprite" type="Sprite2D" parent="CollisionShape2D"] diff --git a/project.godot b/project.godot index fb63014..94683e1 100644 --- a/project.godot +++ b/project.godot @@ -32,6 +32,11 @@ window/handheld/orientation=1 2d_physics/layer_2="Bottle" 2d_physics/layer_3="Entities" +[physics] + +2d/solver/contact_max_allowed_penetration=1.0 +2d/solver/default_contact_bias=1.0 + [rendering] renderer/rendering_method="mobile" diff --git a/root.tscn b/root.tscn index f179bc0..ecb05c2 100644 --- a/root.tscn +++ b/root.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=4 format=3 uid="uid://cbccs6kwwf265"] -[ext_resource type="PackedScene" path="res://bottle/bottle.tscn" id="1_4fmd3"] +[ext_resource type="PackedScene" uid="uid://bllsprv8orpn4" path="res://bottle/bottle.tscn" id="1_4fmd3"] [ext_resource type="PackedScene" uid="uid://c3kitncwpi42j" path="res://entity/coin_copper.tscn" id="2_dv01l"] [ext_resource type="PackedScene" uid="uid://c67lfbk4gf1ga" path="res://spawner/spawner.tscn" id="3_pubxn"] @@ -76,6 +76,6 @@ texture_filter = 1 [node name="Bottle" parent="UI/GameContainer/Game" instance=ExtResource("1_4fmd3")] [node name="Spawner" parent="UI/GameContainer/Game" instance=ExtResource("3_pubxn")] -position = Vector2(-9, -333) +position = Vector2(2, -410) scene = ExtResource("2_dv01l") target_parent = NodePath("..") diff --git a/spawner/spawner.gd b/spawner/spawner.gd index d6981c7..21c7ca4 100644 --- a/spawner/spawner.gd +++ b/spawner/spawner.gd @@ -4,7 +4,9 @@ class_name Spawner @export var target_parent: NodePath func spawn(): + if len($Area2D.get_overlapping_bodies()) > 0: + return + var scene_instant= scene.instantiate() scene_instant.position=Vector2.ZERO add_child(scene_instant) - print(scene_instant.global_position) diff --git a/spawner/spawner.tscn b/spawner/spawner.tscn index fb47b82..0023f33 100644 --- a/spawner/spawner.tscn +++ b/spawner/spawner.tscn @@ -1,12 +1,22 @@ -[gd_scene load_steps=2 format=3 uid="uid://c67lfbk4gf1ga"] +[gd_scene load_steps=3 format=3 uid="uid://c67lfbk4gf1ga"] [ext_resource type="Script" path="res://spawner/spawner.gd" id="1_xqfmg"] +[sub_resource type="RectangleShape2D" id="RectangleShape2D_xnebx"] +size = Vector2(100, 20) + [node name="Spawner" type="Node2D"] script = ExtResource("1_xqfmg") [node name="Timer" type="Timer" parent="."] -wait_time = 0.008 +wait_time = 0.02 autostart = true +[node name="Area2D" type="Area2D" parent="."] +collision_mask = 4 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +position = Vector2(0, -10) +shape = SubResource("RectangleShape2D_xnebx") + [connection signal="timeout" from="Timer" to="." method="spawn"]