How can I convert a Vector3 target to a orientation on two seperate axes?

I’ve been stuck for a minute on trying to make a cannon rotate to a targeted point. The challenge of what I’m trying to accomplish is that instead of just simply pointing the entire model to a target, I have separate sets of parts that need to be rotated depending on the axis that is being rotated on.

image

UpPivot (as marked) should turn up and down based on it’s location in order to target upwards or downwards, while the green area (the base of the cannon) should rotate side to side towards the target. I haven’t been able to figure out how to do this math. My desires are to have support for each main control scheme (mouse/keyboard, controller thumbstick, and mobile onscreen tap,) preferably through the world Vector3 that is targeted.

Additionally, my solution has to be aware of changing orientation of the cannon itself, as the cannon may be attached to a ship or other object which is moving separate from the cannon.

Adding to this, I have extents based on degrees for the orientation, which we can’t turn a total amount over from the starting position (after movement from any connected objects)

Some extra things to note (though I can change this if the solution requires it:) As of now, my cannon is based upon a metatable, and the cannon has a core part to reference for the total orientation.

TL:DR

How can I rotate a model based on orientation on separate axes to face a target (as shown in the picture). I suck at the math of this.

1 Like

Sounds like you need to do some vector maths to obtain the angle of rotations needed similar to this question for a SAM turret:

Once you get the angles you can do some CFrame orientation, or use servo motors to orient the cannon, up to you.

1 Like

My problem, however, is that I am not understanding how to find the relative angle between the two objects. Your solution just answers how to get the magnitude from one vector3 to the other, which you can just get by doing (base.Position - target.Position).magnitude . You then just say to “do some CFrame orientation,” but clearly I can’t figure how how to just “do” that.

Sorry about this my answer to that thread is incomplete, what I meant was that you can obtain the angles through using the tangent formula on the vector component.

like you can get the x angle by doing:

local baseToTargetVector = (base.Position - target.Position)
local xAngle = math.atan(baseToTargetVector.Z/baseToTargetVector.X)

This is what SwagMasterAndrew was trying to help with also in that thread:

Or there’s even the possibility of getting the horizontal angle (SidePivot) by using the look vector of the base of your cannon (front of the cannon) and the horizontal vector of the cannon to the target position to find the current rotation needed to rotate the base towards the target position.

This horizontal vector you can obtain by obtaining the cannon position to target position vector then equating the Y component to zero.

…NVM that’s bit confusing onwards to the MS paint diagram

What I tried to explain is how to obtain the blue cyan horizontal angle.

Sorry, about this yeah CFrame’s are pretty difficult I spent an entire two months making projects related to CFrame manipulation by experimenting with the different constructors.

There are two ways to do it:

  1. Use CFrame.Angles() like in this tutorial to rotate the cannon along the y-axis similar to the tutorial for the door in the dev API reference, similar you can control the up and down using CFrame.Angles in a similar manner.

  1. Use CFrame.fromAxisAngles() this one you need to define the axis of rotation yourself which you can do using cross product. I highly suggest experimenting with it yourself but you can ask me later (I gotta sleep)
2 Likes