|Topic was automatically imported from the old Question2Answer platform.
Context I am making an arcade racing game and as a part of it, the cars have 2 states: NORMAL, DAMPEN
The DAMPEN state will have multiple uses (mostly how it interacts with the 3 core abilities of the game, but I’m not there yet). One use is how the DAMPEN state effects your velocity upon entering and exit. It first calculates and stores your current velocity, then removes the “boost” increase to your current velocity while in the DAMPEN state (thus slowing you down to your base speed that is unaffected by any “turbo speed”), and then finally applies the stored_velocity upon exiting DAMPEN (returning you to your previous, much faster, speed). To my surprise, I’ve actually got most of it working, except for one terrible bug at the end.
The Problem Since my velocity is a Vector2, when I apply my stored velocity, it pushes the car in the direction I was facing at the time I stored it, not in the direction I am currently facing, which makes sense since Vector2’s are both distance and direction. So if I enter DAMPEN, make a hard 180 turn, then exit DAMPEN, my current velocity and my stored velocity negate each other and my car briefly stops moving before resuming its NORMAL acceleration speed.
What I think I need to do is adjust the stored velocity’s rotation equal to the new rotation of my car when I exit, that way my velocity always pushing my “forward”. But I have no idea how to do this.
Note: the car is a KinematicBody2D
My Thoughts on the Approach Should I create a Position2D Node that rotates itself whenever I turn, and then have my machine remember/store its rotation value upon entering DAMPEN and upon exiting DAMPEN, find the difference between the values, then rotate the stored velocity equal to that difference?
I keep stumbling over “atan2” and Vector2.rotate() as I try to research this problem, but I haven’t figured out how to actually implement these functions.
Any help would be greatly appreciated! If you need me to list code, I’d be happy to, though it’s probably pretty messy, which I apologize about.