Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Zepher79 |
So this is a bizarre situation I find myself in. In essence I was messing around creating an asteroids clone and was struggling to figure out wraparound behavior for the ships. Ships are RigidBody2Ds with a sprite and collision object attached and operating using the following code:
extends RigidBody2D
export var maxSpeed: float = 500
export var turnSpeed: float = 0.1
export var foward_friction: float = 0.1
var boostVelocity: float
# Called when the node enters the scene tree for the first time.
func _process(delta):
# Bank left
if Input.is_action_pressed("turn_left"):
angular_velocity = clamp(angular_velocity - turnSpeed, -2, 2)
# Bank right
if Input.is_action_pressed("turn_right"):
angular_velocity = clamp(angular_velocity + turnSpeed, -2, 2)
# Boosting
if Input.is_action_pressed("thrust"):
# Ramp boost up towards maxSpeed
boostVelocity = lerp(boostVelocity, maxSpeed, 0.5 * delta)
else:
# Ramp boost down towards 0
boostVelocity = lerp(boostVelocity, 0, 10 * delta)
# Braking
if Input.is_action_pressed("brake"):
# Severely ramp down boost towards 0
boostVelocity = lerp(boostVelocity, 0, 10 * delta)
# Apply forces
linear_velocity += Vector2(0, -boostVelocity).rotated(rotation)*foward_friction
linear_velocity = linear_velocity.limit_length(maxSpeed)
wraparound()
# Supposedly teleports ship to the opposite side of the screen, ala Asteroids
func wraparound():
if position.y <= 0:
position.y += 600
if position.y >= 600:
position.y -= 600
if position.x <= 0:
position.x += 1024
if position.x >= 1024:
position.x -= 1024
Now I am aware that this is not how physics should be handled and will be going back to do it properly, and this code breaks predictably after the first wraparound in any direction off the screen.
HOWEVER! Included in the ScenesToTest folder is another almost identical scene called VisShip. It uses the exact same code source and the only change is a VisibilityNotifier2D node is added as a child. There are no additional links or signals being emitted, it’s just sitting there.
This second scene “works” perfectly as I had envisioned and I honestly am stumped as to why that would be. If anyone has observations or just gets a kick out of it as well, I’d love to hear your thoughts!