Rotating a vector

I have a velocity vector and a mouseposition vector I want to rotate the velocity vector towards the mouseposition vector a set amount of degrees (3D)

(green=velocity,red=mouseposition)
image
what I esstinally want
image

The usecase for this is that I have a projectile that shoots forward and then the player is able to control the projectile with there mouse, which I am using the mouseposition for

I know how to get an angle between 2 vectors using dot product however I am a bit lost on what to do with that information since I need to rotate 2 axis and dot product only gives me a scalar number

5 Likes

So why don’t you just update the velocity with the new direction of the mouse.

Because then it be instantaneous I need it to rotate towards the direction with a set amount of degrees, so if the angle between the 2 vectors is 180 it doesn’t just snap

Since the position is not changing (no need to slerp), consider using a Tween

The position is changing its velocity

Do you have a need to slerp? Why would you avoid the Tween system?

I don’t understand your question I don’t think I ever said I needed to lerp, I just have a projectile that shoots forward and Im representing that movement by “velocity”

So why don’t just change it by a fraction, are using a RunService event to update it?

Why not use body movers to smoothly move your projectile? You could use BodyPosition or RocketPropulsion.

I don’t think you understand what you need. Use TweenService to tween (smoothly interpolate) the projectile to be in the direction of the mouse. That is what that service is for.

Edit: Otherwise you need to use a rotation matrix - you can’t rotate a 3D vector to another 3D vector by itself without a matrix. This is accomplished with CFrame.new(pos, targetPos)

Although using runservice is very fast its not a reliable method since there no gunatureed method it will clamp between desiredangle,maxangle… as for the fraction there be wasted movement since if you can imaigne 2 vectors being very close to each together (small angle) then it will have a hard time reaching to that point since it would then be a small number

Alright Ill try the matrix method and get back to you on that then

Yes, but don’t ignore TweenService. You need to use it so it doesn’t “snap” to the new position. There’s a reason I keep telling you about it - I’m not just randomly throwing it out there.

Use a tween with CFrame.new(pos, targetPos) to tween the projectile to face the new mouse direction (rotate the vector).

local projectile = ? -- Your rocket or whatever
local ts = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1) -- 1 is the time of interpolation for the tween
local tween = ts:Create(projectile, tweenInfo, {
	CFrame = CFrame.new(projectile.Position, mouse.Hit.p)
})

tween:Play() -- Run the tween

Does tweenservice have any issues with the physics engine, the projectile im working with is being moved by bodyvelocity so it is therefor unanchored

Ah, I see. Then you need to use BodyGyro and set the CFrame of that instead of TweenService.

1 Like

Dot Product is telling you how close one vector is to pointing towards the other. If the Dot Product is 1 then both vectors are in the same direction. -1 means they are both facing opposite sides. 0 or -0 means they are perpendicular to each other.

A little confused by the question can you further elaborate on what you mean by controlling the projectile with the player mouse. Specifically how that relates to needing to rotate a vector?

2 Likes

As an addition to @EgoMoose’s question, could you also explain your diagrams as its difficult to see what your end goal is when you then proceed to talk about the mouse controlling the projectile (the diagrams don’t relate)?

I’m assuming you want a projectile which you can fire, and then your mouse can influence how it moves through the air?

1 Like

Yes the projectile velocity changes based on the mouse

I want my projectile to steer towards my mouse instead of instant