From 6bfac4e8d6557d743ce201a48a90bd4616b58434 Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Fri, 15 Mar 2024 09:47:15 +0100 Subject: [PATCH] random ball linear velocity --- scenes/playground/Playground.gd | 21 --------------------- scenes/playground/Playground.tscn | 7 ++----- scenes/playground/ball/Ball.gd | 20 ++++++++++++-------- scenes/playground/ball/Ball.tscn | 4 ++++ scenes/playground/racket/Racket.gd | 2 +- 5 files changed, 19 insertions(+), 35 deletions(-) diff --git a/scenes/playground/Playground.gd b/scenes/playground/Playground.gd index 5af76b0..16b2224 100644 --- a/scenes/playground/Playground.gd +++ b/scenes/playground/Playground.gd @@ -44,24 +44,3 @@ func _on_goal_area_right_body_entered(body): if body_is_ball(body): left_point += 1 new_round(body) - - -func ball_to_racket(body): - if body_is_ball(body): - var new_linear_velocity = body.linear_velocity * -1 - - #if new_linear_velocity.y == 300 and new_linear_velocity.x == 300: - # new_linear_velocity.x -= 10 - # new_linear_velocity.y += 10 - - body.linear_velocity = new_linear_velocity - print_debug(new_linear_velocity) - - -func _on_racket_left_body_entered(body): - ball_to_racket(body) - -func _on_racket_right_body_entered(body): - ball_to_racket(body) - - diff --git a/scenes/playground/Playground.tscn b/scenes/playground/Playground.tscn index 0123cd9..8bb59a5 100644 --- a/scenes/playground/Playground.tscn +++ b/scenes/playground/Playground.tscn @@ -40,7 +40,7 @@ shape = SubResource("RectangleShape2D_u7af8") position = Vector2(128, 224) collision_layer = 255 collision_mask = 255 -custom_integrator = true +freeze = true continuous_cd = 2 max_contacts_reported = 1 contact_monitor = true @@ -52,8 +52,7 @@ position = Vector2(1032, 224) collision_layer = 255 collision_mask = 255 freeze = true -custom_integrator = true -continuous_cd = 1 +continuous_cd = 2 max_contacts_reported = 1 contact_monitor = true metadata/UP_KEY = "right_up" @@ -71,5 +70,3 @@ vertical_alignment = 1 [connection signal="body_entered" from="GoalAreaLeft" to="." method="_on_goal_area_left_body_entered"] [connection signal="body_entered" from="GoalAreaRight" to="." method="_on_goal_area_right_body_entered"] -[connection signal="body_entered" from="RacketLeft" to="." method="_on_racket_left_body_entered"] -[connection signal="body_entered" from="RacketRight" to="." method="_on_racket_right_body_entered"] diff --git a/scenes/playground/ball/Ball.gd b/scenes/playground/ball/Ball.gd index ce8cbe6..19be2c2 100644 --- a/scenes/playground/ball/Ball.gd +++ b/scenes/playground/ball/Ball.gd @@ -1,21 +1,25 @@ extends RigidBody2D -const SPEED = 300.00 +const SPEED = 500.00 -func _ready(): +func get_random_linear_velocity(): randomize() - - global_position = Vector2(640, 360) - + var x_direction = pow(-1, randi() % 2) var y_direction = pow(-1, randi() % 2) - - await get_tree().create_timer(1.0).timeout - linear_velocity = Vector2(x_direction, y_direction) * SPEED + return Vector2(x_direction, y_direction) * SPEED + +func _ready(): + global_position = Vector2(640, 360) + await get_tree().create_timer(1.0).timeout + linear_velocity = get_random_linear_velocity() func _process(delta): pass + +func _on_body_entered(body): + linear_velocity = get_random_linear_velocity() diff --git a/scenes/playground/ball/Ball.tscn b/scenes/playground/ball/Ball.tscn index 132ee40..5e77535 100644 --- a/scenes/playground/ball/Ball.tscn +++ b/scenes/playground/ball/Ball.tscn @@ -15,6 +15,8 @@ gravity_scale = 1.66533e-16 can_sleep = false lock_rotation = true freeze_mode = 1 +max_contacts_reported = 10 +contact_monitor = true linear_damp_mode = 1 script = ExtResource("1_vsr7q") @@ -25,3 +27,5 @@ offset_bottom = 40.0 [node name="CollisionShape2D" type="CollisionShape2D" parent="."] position = Vector2(20, 20) shape = SubResource("RectangleShape2D_bdxrg") + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/scenes/playground/racket/Racket.gd b/scenes/playground/racket/Racket.gd index d2f65b3..06af9e5 100644 --- a/scenes/playground/racket/Racket.gd +++ b/scenes/playground/racket/Racket.gd @@ -1,7 +1,7 @@ extends RigidBody2D -const SPEED = 300.0 +const SPEED = 500.0 func _physics_process(delta): if Input.is_action_pressed(get_meta("UP_KEY")):