Question for people good at math. Velocity in a direction

The blue is a spaceship.

Let’s say for example it’s velocity is moving in the exact direction the RED arrow is pointing at.
How can I calculate what it’s current velocity is towards the direction of the green arrow?

P.S. I placed the red arrow in the left upper corner just for example. It can be moving in any direction, I want to be able to calculate the above problem in all cases.

First, we need to split the velocity components into two segments: the north segment and the west segment. To do this, we need to get its direction as if it were on a plane and we were using bearing calculations.
Using this formula for the north velocity:

Vn = V x cos(θ)

And this formula for the west velocity:

Vw = V x sin(θ)

where V is the velocity in the direction its travelling and θ is the angle in radians using the bearings.
Let’s assume that the bearing of travel is 315 and the bearing northwards is 0. Let’s also assume that its travelling at 15 studs per second in the bearing of travel.
Plugging it into the formula above, we get:
Vn = 10.61, Ve = 10.61. This is because if the bearing is 315, the westward velocity must be equal to the northward velocity because 270 + 45 = 315, 360 - 45 = 315.
Therefore, the Velocity northwards is 10.61 roughly for this case.
I am rusty on my vector math, so if anyone spots a mistake, please correct me.

1 Like

you can write:

local VelocityRelativeToWorldPosition = spaceship.CFrame.LookVector * speed

2 Likes

In this formula the “speed” is the velocity vector3 right?

yea why?? Do you need another formula for a different thing?

No, I think this will work. I will try later and mark as a solution if it does. Thanks!

Btw make sure to but it in a client script in a loop as you got to constantly update the direction of the force

Nope, does not work.

As you can see in the later part of the video, the spaceship is moving to a radically different direction than it’s facing, but both velocities print equal.

The script

local curVelocity = root.AssemblyLinearVelocity.Magnitude
local VelocityRelativeToWorldPosition = (root.CFrame.LookVector * curVelocity).Magnitude
print("Normal: "..math.round(curVelocity))
print("Relative: "..math.round(VelocityRelativeToWorldPosition))
print("")

My bad, I was using the magnitude for curVelocity when I shouldn’t have. It works now.

There is another issue with this.

Your formula returns a correct vector3 value, which is technically what I was asking for.
However what I really need, is a number value, because I want to calculate what percentage is current velocity to max speed. And since Max Speed is a number, not a vector 3, I need the relative speed to also be a number.
Using .Magnitude with your provided formula brings incorrect results, because Magnitude is always a positive number, so for example if the spaceship is moving backwards, the relativevelocity magnitude will be equal to normalvelocity magnitude.

Would be much appreciated if you could help with this.

Take the dot product of the look vector and velocity, it should give you the number between -MaxSpeed and MaxSpeed depending on the direction you are moving at.

local function get_diff_between_vectors(look_vec : Vector3, velocity : Vector3) : number
     return look_vec:Dot(velocity);
end

you can set a variable called max speed which is the max speed the space craft can go

local MaxSpeed = 69
local percentageOfCurrentSpeed = currentspeed/69 -- return a decimal eg 10% = .1

if the current speed is negative , it will return a negative percentage