From ed3b37f6fa7b37429f79a7d229e1134ae316ed7e Mon Sep 17 00:00:00 2001 From: cflip Date: Fri, 23 Sep 2022 17:23:06 -0600 Subject: Replace the basketball sprite object with a RigidBody2D This way we don't have to implement all of the physics ourselves. --- Basketball.gd | 20 ++++++++++++++++++++ Sprite.gd | 26 -------------------------- basketball.tscn | 21 ++++++++++++++++----- project.godot | 1 + 4 files changed, 37 insertions(+), 31 deletions(-) create mode 100644 Basketball.gd delete mode 100644 Sprite.gd diff --git a/Basketball.gd b/Basketball.gd new file mode 100644 index 0000000..31642ae --- /dev/null +++ b/Basketball.gd @@ -0,0 +1,20 @@ +extends RigidBody2D + +var angular_speed = PI +var start_position + +# Called when the node enters the scene tree for the first time. +func _ready(): + start_position = position + apply_central_impulse(Vector2(500, -800)) + pass # Replace with function body. + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + rotation += angular_speed * delta + +func _input(event): + if event.is_action_pressed("shoot"): + position = start_position + rotation = 0 + apply_central_impulse(Vector2(500, -800)) diff --git a/Sprite.gd b/Sprite.gd deleted file mode 100644 index ce3a8de..0000000 --- a/Sprite.gd +++ /dev/null @@ -1,26 +0,0 @@ -extends Sprite - -export var gravity = 15 - -var angular_speed = PI -var velocity = Vector2(500, -800) - -var start_position - -# Called when the node enters the scene tree for the first time. -func _ready(): - start_position = position - pass # Replace with function body. - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - position += velocity * delta - rotation += angular_speed * delta - - velocity.y += gravity - -func _input(event): - if event.is_action_pressed("shoot"): - position = start_position - rotation = 0 - velocity = Vector2(500, -800) diff --git a/basketball.tscn b/basketball.tscn index 2c8edf4..c7203d2 100644 --- a/basketball.tscn +++ b/basketball.tscn @@ -1,10 +1,21 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://ballforbasketing.png" type="Texture" id=1] -[ext_resource path="res://Sprite.gd" type="Script" id=2] +[ext_resource path="res://Basketball.gd" type="Script" id=2] -[node name="Sprite" type="Sprite"] -position = Vector2( 114, 478 ) +[sub_resource type="CircleShape2D" id=1] +radius = 40.0 + +[node name="Basketball" type="RigidBody2D"] +position = Vector2( 71, 541 ) +script = ExtResource( 2 ) +__meta__ = { +"_edit_group_": true +} + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] scale = Vector2( 5, 5 ) texture = ExtResource( 1 ) -script = ExtResource( 2 ) diff --git a/project.godot b/project.godot index 56cf31e..b05b1de 100644 --- a/project.godot +++ b/project.godot @@ -29,6 +29,7 @@ shoot={ [physics] common/enable_pause_aware_picking=true +2d/default_gravity=981 [rendering] -- cgit v1.2.3