ball now more consistently collides with arbitrary objects and bounces off them appropriately

This commit is contained in:
2024-07-23 20:07:13 +02:00
parent 3fea3c89ed
commit 091d957d26
7 changed files with 147 additions and 102 deletions
+35 -9
View File
@@ -1,17 +1,43 @@
extends CharacterBody2D
@export var speed: float = 5
const _speed_mult: float = 200
var _direction: Vector2 = Vector2(1.0, 0.5)
@export var strength: int = 1
const _speed_mult: float = 20
var _direction: Vector2 = Vector2(-1.0, -0.5)
var _velocity: Vector2
var deg: float
func _ready():
_velocity.x = 200#speed * _speed_mult * _direction.x * delta
_velocity.y = 250#speed * _speed_mult * _direction.y * delta
func _physics_process(delta):
velocity.x = speed * _speed_mult * _direction.x
velocity.y = speed * _speed_mult * _direction.y
move_and_slide()
var collide = move_and_collide(_velocity * delta)
if collide:
deg = rad_to_deg(collide.get_angle())
if (deg >= 45 and deg < 135) or (deg >= 225 and deg < 315):
_invert_x()
if (deg >= 135 and deg < 225) or deg < 45 or deg >= 315:
_invert_y()
if collide.get_collider() is Block:
print(true)
collide.get_collider().take_damage(strength)
func invert_x():
_direction.x *= -1
func _invert_x():
_velocity.x *= -1
func invert_y():
_direction.y *= -1
func _invert_y():
_velocity.y *= -1
func has_collided():
pass
#if $Raycasts/North.is_colliding() or $Raycasts/South.is_colliding():
#_invert_y()
#print("y")
#if $Raycasts/West.is_colliding() or $Raycasts/East.is_colliding():
#_invert_x()
#print("x")
+41 -1
View File
@@ -1,4 +1,4 @@
[gd_scene load_steps=4 format=3 uid="uid://sl3jbnopo0yh"]
[gd_scene load_steps=8 format=3 uid="uid://sl3jbnopo0yh"]
[ext_resource type="PackedScene" uid="uid://d2r0ip6xy3a2" path="res://balls/ball.tscn" id="1_ffml1"]
[ext_resource type="Texture2D" uid="uid://cxi0fqy5sajh5" path="res://balls/basic/basic.webp" id="2_4dnuc"]
@@ -6,10 +6,50 @@
[sub_resource type="RectangleShape2D" id="RectangleShape2D_isusp"]
size = Vector2(50, 50)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_qicwc"]
size = Vector2(46, 2)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ugdte"]
size = Vector2(46, 2)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_2tu06"]
size = Vector2(2, 46)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_0vqpn"]
size = Vector2(2, 46)
[node name="Ball" instance=ExtResource("1_ffml1")]
collision_mask = 14
[node name="CollisionShape2D" type="CollisionShape2D" parent="." index="0"]
shape = SubResource("RectangleShape2D_isusp")
[node name="Sprite2D" type="Sprite2D" parent="." index="1"]
texture = ExtResource("2_4dnuc")
[node name="Raycasts" type="Node2D" parent="." index="2"]
visible = false
[node name="North" type="ShapeCast2D" parent="Raycasts" index="0"]
shape = SubResource("RectangleShape2D_qicwc")
target_position = Vector2(0, -25)
collision_mask = 10
collide_with_areas = true
[node name="South" type="ShapeCast2D" parent="Raycasts" index="1"]
shape = SubResource("RectangleShape2D_ugdte")
target_position = Vector2(0, 25)
collision_mask = 10
collide_with_areas = true
[node name="West" type="ShapeCast2D" parent="Raycasts" index="2"]
shape = SubResource("RectangleShape2D_2tu06")
target_position = Vector2(-25, 0)
collision_mask = 10
collide_with_areas = true
[node name="East" type="ShapeCast2D" parent="Raycasts" index="3"]
shape = SubResource("RectangleShape2D_0vqpn")
target_position = Vector2(25, 0)
collision_mask = 10
collide_with_areas = true