# How do I rotate a Vector2 based off of the stored_rotation and current_rotation of my Player?

Attention 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.

Hi,
i maybe dont understand your concept in total. But i think you should seperate direction and velocity.
To get the velocity out of your vector2 you can use

``````velocity = vec.length()
``````

To get the direction from your vector2 you can use

``````dir = vec.normalized()
``````

To apply a velocity on a vector you can do

``````vec = dir * velocity
``````

so if you want to conserve your speed

``````velocity = vec.length()
dir = vec.normalized()
#modify direction
new_dir = dir.rotated(0.25)
vec = new_dir * velocity
``````

I definitely think this is a much easier solution than the one I was trying to concoct. My code is based off of a tutorial that I added a lot to, and so it is a bit of a mess right now. I’ll probably have to rewrite my code as a lot of the equations start screaming at me when I try to add .length().

Just as clarification, when you say
`vec.length()`
are you meaning
`Vector2.length()`
or is “vec” just a variable that you created?

I’ll work on it tomorrow and I’ll let you know if it works.

cardboard_bones | 2020-09-17 05:06

The vec would be the vector your refeering to as “since Vector2’s are both distance and direction” With the methods i’de say you could split it up into direction and velocity.

klaas | 2020-09-17 06:38

Thanks for the clarification. After trying somethings, my code eventually just broke down and I think Im going to have to scrap it and restart it. Hopefully I’ll be able to implement separating the speed and direction this time!

cardboard_bones | 2020-09-17 14:58