0 votes

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.

in Engine by (18 points)

1 Answer

0 votes

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
by (2,430 points)

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
are you meaning
or is "vec" just a variable that you created?

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

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.

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.