Rotate vectors relative to another, as well as your mouse movement

I’m looking to solve a unique mathematical and practicality issue, which I hope is a simple solution that 'm overlooking.

The issue that I’m having is that I want to rotate a vector, around another (in a three dimensional space) relative from an offset, that’s based off the mouse movement of a player.

Here is a simple drawing of what I’m looking to achieve.

The red line would be the offset, the blue & purple lines are possible paths that the green dot could follow, ie; if the player moves the mouse left, up, down, etc…

Calculating the offsets that the vector needs to move is simple,

xDiff = (xDiff + Mouse.X * sens - x)
yDiff = (yDiff + Mouse.Y * sens - y)

x, y = Mouse.X * sens, Mouse.Y * sens

That bit works perfectly fine, the issues at hand is applying these mouse offsets to another vector (or CFrame) and moving it accordingly.

Here’s the current code handling this.

local default = CFrame.new(HumanoidRootPart.Position, rightGrapplePosition)
cFrame = default * CFrame.Angles(0, math.rad(xDiff), math.rad(yDiff)) * offset

Where offset is the following code, (position being the position to rotate around)

offset = (HumanoidRootPart.Position - position).Unit
distance = (HumanoidRootPart.Position - position).Magnitude
	
offset = CFrame.new(offset * distance)

Here is a clip of what’s happening as of right now.

If I remove the offset multiplier, this is the result.

My assumption is this is happening due to the differences not taking in the current orientation as a factor, but I’m not entirely sure how to fix this.

Are you looking for a theoretical example that will work anywhere, or a practical example that you can use in video game design?

I’m looking for a solution or steps to reach a solution that works how I intend it to, as seen in the drawing.

Well the problem is that I don’t really know all of the requirements for this. Can it have a maximum elevation of 90 degrees? Are you looking for a fix for your system, or can I recommend a new one? If so, can you set an azimuth and elevation variable and adjust those based on xDiff and yDiff?

CFrame.new(V3_origin) * CFrame.FromAxisAngle(Vector3.new(0, 1, 0), math.rad(degrees_azimuth)) * CFrame.FromAxisAngle(Vector3.new(1, 0, 0), math.rad(degrees_elevation)) * CFrame.new(0, 0, -studs_distance)

1 Like

I’m all ears for suggestions as well as other ways to go about this, ie; different systems completely.
So feel free to suggest away :smiley:

There are no limitations in regards to what angles can be used, etc…

I will take a look at the code sample you sent now, :slight_smile:

@JarodOfOrbiter

So while what you wrote works, the issue at hand still remains; the origin of the CFrame should be based on the original vector given.

In this case the red brick should start at the players root part, and rotate around the point from there.

(Rather then 0, 0, 0 which I believe is the case here)

That will be V3_origin variable, though I’m sure you know that. Set that to HumanoidRootPart.Position.
Are you saying also that you don’t want it to follow the player?

Also, how are you getting the mouse delta? That doesn’t look quite right to me.

xDiff = (xDiff + Mouse.X * sens - x)
yDiff = (yDiff + Mouse.Y * sens - y)

x, y = Mouse.X * sens, Mouse.Y * sens

That’s how mouse information is passed, and yes the V3_Origin variable is set properly,

I don’t want the brick to follow the player, rather start at the players location (I will use body forces later on to move the player to the brick, etc…)

Still haven’t found a viable solution, if anyone has any ideas let me know :slight_smile: