Godot Version
v4.3
Question
Hello, I’m new to GDScript and need help with a problem. I’m working on a physics demo, just to test myself, and I’ve enqountered an issue. I have three balls in my main scene, one of them is perfect, bouncing then coming to a rest soon after, the other two are bouncing forever, from the top to the bottom of the window, at maximum speed.
ball scene:
@tool
extends Entity
class_name ball
# exports
@export var diameter : int = 64
@export var color : Color = "ff0000"
# variables
var radius : int = 32
# constants
const GRAVITY : float = 9.8 # m/sec^2\
func _process(delta: float) -> void:
$Sprite2D.modulate = color
$Sprite2D.scale = Vector2(diameter / 64, diameter / 64)
$CollisionShape2D.scale = Vector2(diameter / 64, diameter / 64)
# physics
func _physics_process(delta: float) -> void:
if !Engine.is_editor_hint():
var collision = move_and_collide(velocity + acceleration * delta)
if collision:
var collider = collision.get_collider()
if collider == $"../floor" || collider == $"../ceiling":
velocity.y = -velocity.y
elif collider == $"../left_wall" || collider == $"../right_wall":
velocity.x = -velocity.x
else:
velocity = -velocity
change_positions(delta)
func change_positions(delta: float):
print(str($".") + str(velocity))
acceleration.y += GRAVITY * delta
velocity = Vector2(min(terminal_velocity, velocity.x + acceleration.x * delta), min(terminal_velocity, velocity.y + acceleration.y * delta))
All three of the balls come from the same scene but act differently. I don’t know how to fix it. but I have found out that the two that are bouncing quickly have a super high velocity compared to the other one even when I simply start the scene. All help is appreciated and welcome, thank you in advance!
P.S. if you need more info on other scenes I have (like my entity scene) feel free to ask
Edit:
My entity scene, as it is very small.
extends CharacterBody2D
class_name Entity
# exports
@export var sprite_texture : Sprite2D
@export var mass : float = 1.0
# variables
var acceleration = Vector2(0, 0)
var terminal_velocity : int = 1000