Finding the Target Angle between two vectors

Not at all, the height (y) of the target part varies, depending on where the ray hits.

Ok that’s probably fine. Try something like this:

local a=baseplate.CFrame.lookVector*-1
a=Vector2.new(a.X,a.Z)
local b=(Vector3.new(target.Position.X,baseplate.Position.Y,target.Position.Z)-baseplate.Position).Unit
b=Vector2.new(b.X,b.Z)

local cost=(a:Dot(b))/(a.Magnitude*b.Magnitude)
local t=math.acos(cost)
print(t)

I can walk you through the math later if you want. But I really want to see how this does first. :slightly_smiling_face::

1 Like

local cost=(a:Dot(b))/(a.Magnitude*b.Magnitude)

Is there an extra parenthesis here?

A lone parenthesis? I don’t see one. Is your HingeConstraint’s ActuatorType set to Servo?

Yeah, it is. :eyes: :thinking: :smile:

Oh ok. That might mean we will need to add our angle to the current TargetAngle. :thinking:

It will be returned in Radians correct? Not Degrees?

It will return as degrees not radians.

Update: Actually, let me check.

I was wrong. It returns in radians.

1 Like

Okay I was wrong as well… the front vector is actually facing towards the right of the mortar… Which means the baseplate’s front vector is actually negative RightVector

1 Like

Just to update this so far:

local a=baseplate.CFrame.RightVector*-1
a=Vector2.new(a.X,a.Z)
local b=(Vector3.new(target.Position.X,baseplate.Position.Y,target.Position.Z)-baseplate.Position).Unit
b=Vector2.new(b.X,b.Z)

local cost=(a:Dot(b))/(a.Magnitude*b.Magnitude)
local t=math.acos(cost)
local angle=math.deg(t)
print(angle)

It’s partially working, it won’t rotate left at all.

Output of T:

  62.015907206196
  72.197744334062
  80.393969837658 (x2)
  80.545715200787 (x6)
  90.42974063954
  115.49450465057
  118.32042315943 (x2)
  113.23179367071
  111.91522450319 (x3)
local A = Baseplate.CFrame.RightVector * -1
A = Vector2.new(A.X, A.Z)
	
local B = ( Vector3.new(Hit.X, Baseplate.CFrame.Y, Hit.Z) - Baseplate.CFrame.p ).Unit
B = Vector2.new(B.X, B.Z)
	
local Cost = ( A:Dot(B) ) / (A.Magnitude * B.Magnitude)
local T = math.acos(Cost)
	
print(math.deg(T))

I use a bunch of capitals… haha a bit non conventional.

2 Likes

That’s normal. I actually expected that to happen. (just hadn’t mentioned it yet) :stuck_out_tongue:
We need a way to figure out when our angle should be subtracted. Give me a minute to try and figure something out. :thinking:

2 Likes

Will every mortar’s base part always have the save orientation? (I hope so :stuck_out_tongue:)

Lol, nope. The way it spawns is it’s placed directly in front of your character on the ground. So it’s facing the same direction as the player.

This actually turned out to be a lot more difficult than I expected, Actually the parabola seemed easier. But that’s probably because I looked up the formula. Haha

Could you try adding this after the angle is calculated:

local x=nozzle.CFrame:PointToObjectSpace(target.Position)
print(x.Z)

After you do that, could you hit play and show me what happens similar to what you posted above?

1 Like

  -4.8550653457642
  -5.9226531982422
  -8.0932426452637
  -9.8448848724365
  -13.027925491333
  -12.001844406128
  31.100881576538
  50.444561004639
  79.524765014648
  80.549499511719 (x51)
  21.992385864258
  17.524366378784

Sorry, could you do that again and sweep between the left side of the nozzle and the right side. I’m trying to figure out if you get a negative output when your mouse is to the left of the nozzle and a positive output if it’s on the right side. Your mouse stayed to the left of the nozzle the entire time. I’m pretty hopful since I saw mostly negative numbers. But try keeping the mouse over on the other side to see if you get positive numbers.

-16.302471160889
  -17.394468307495
  -11.611865997314
  -10.058298110962
  -9.9586334228516
  -9.8046932220459
  -7.5283803939819
  -6.6902565956116
  -7.1277585029602
  -7.531792640686
  -8.0928020477295
  -9.6837329864502
  -9.3881597518921
  -9.4646100997925
  -8.5556716918945
  -9.3337001800537
  -6.2375454902649
  -5.7798442840576
  -6.0139751434326
  -6.1250791549683
  -6.7178807258606
  -7.8094615936279
  -6.7452449798584
  -6.8286004066467
  -7.0145587921143
  -7.3607110977173
  -5.6080870628357
  -5.1491408348083
  -5.4976568222046
  -5.5263962745667 (x38)
  -3.6308784484863
  -1.4806373119354
  -2.232314825058
  -2.6993093490601
  -3.5148768424988
  -4.2912278175354
  -5.004047870636
  -5.6491546630859
  -6.1727752685547
  -6.6697034835815
  -6.9977197647095