2D portals in Godot

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By doinsdf

I’ve been experimenting with 2D portals in my game. I have a simple script that queue_frees a ball once it enters one portal, and spawns one in in the other.

I want to preserve the velocity of the ball, so that it doesn’t just have zero velocity when it comes out of the portal. For this, I used the linear_velocity of the preexisting ball before it gets queue_freed, and apply that to the new ball once it comes out of the portal.

However, this does not align with the rotation of the portal, and the ball always continues to go in whatever direction it was going in. I want to be able to have the same velocity and momentum, but come out in the direction the portal’s facing.

Does anyone know a variable I could use to modify this? How can I make the 2D portals realistic?

When the velocity is a Vector2 just use Vector2.rotated().
velocity = velocity.rotated(portal.rotation)

RoniPerson | 2021-01-02 20:36

Thank you, that works perfectly!

doinsdf | 2021-01-02 20:45

I will add it as answer. So that the question is not showen in the unanswered tab.

RoniPerson | 2021-01-03 09:17

:bust_in_silhouette: Reply From: RoniPerson

Use the Vector2.rotated() methode like this:

velocity = velocity.rotated(portal.rotation)