iondriver
|
  |
| Joined: 18 Nov 2012 |
| Total Posts: 757 |
|
|
| 27 Feb 2015 12:13 PM |
Here is my function to find the angle between two parts. However, it is not working properly. The function always returns infinity. Where did I go wrong?
function CalculateRotation(P1, P2) local Ax = P2.X local Ay = P2.Y local Az = P1.Z local M1 = (Vector3.new(Ax,Ay,Az)-P1).magnitude print(M1) local M2 = (Vector3.new(Ax,Ay,Az)-P2).magnitude print(M2) local AngRaw = math.acos(M1/M2) if P2.Z > P1.Z then return AngRaw else local Ang = -1*AngRaw return Ang end end |
|
|
| Report Abuse |
|
|
|
| 27 Feb 2015 12:17 PM |
I don't know, but I would just use this.
Part1.CFrame.lookVector:Dot(Part2.CFrame.lookVector) |
|
|
| Report Abuse |
|
|
| |
|
iondriver
|
  |
| Joined: 18 Nov 2012 |
| Total Posts: 757 |
|
|
| 27 Feb 2015 12:29 PM |
| That doesn't work. Thanks though. I am trying to get a part to point to the mouse position. Is there any easier way? |
|
|
| Report Abuse |
|
|
|
| 27 Feb 2015 01:06 PM |
part.CFrame = CFrame.new(part.Position, mouse.Hit.p)
points part to mouse position |
|
|
| Report Abuse |
|
|
iondriver
|
  |
| Joined: 18 Nov 2012 |
| Total Posts: 757 |
|
|
| 27 Feb 2015 01:15 PM |
| Ok, Is there a way I can do this gradually, instead of instantly? Also, Can I turn the part without turning everything it is welded to? |
|
|
| Report Abuse |
|
|
iondriver
|
  |
| Joined: 18 Nov 2012 |
| Total Posts: 757 |
|
| |
|
|
| 27 Feb 2015 02:14 PM |
| Finish this function please v.v I've been trying to do this for a while but I suck at Geometry, being that I didn't even learn it yet. |
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
|
| 27 Feb 2015 02:22 PM |
local CosTheta = a:Dot(b) -- a and b are Vector3s local Theta = math.acos(CosTheta)
|
|
|
| Report Abuse |
|
|
|
| 27 Feb 2015 02:33 PM |
| ^ oh my god, trigonometric functions would be the most simplest answer to his problem. |
|
|
| Report Abuse |
|
|
|
| 27 Feb 2015 02:36 PM |
| He doesn't mean finding an angle between two Vector3s, contrary to what he said in the original post. He should have worded it better. I already gave him a:Dot(b), but he says he doesn't want to find the angle. |
|
|
| Report Abuse |
|
|
|
| 27 Feb 2015 02:37 PM |
| what does the :Dot() function do? |
|
|
| Report Abuse |
|
|
iondriver
|
  |
| Joined: 18 Nov 2012 |
| Total Posts: 757 |
|
|
| 27 Feb 2015 02:59 PM |
Clarification
I am making a Tank. The turret is attached to the body by a motor joint. I need to find the angle between the turret Lookvector and the mouse position. Once I have this, I would set the DesiredAngle of the motor to the returned angle. |
|
|
| Report Abuse |
|
|
|
| 27 Feb 2015 03:12 PM |
Pretty easy. Two things here. You need a 2D angle (not the 3D angle). You also need to normalize it for the DesiredAngle.
function AngleBetween(v1, v2) local d = v1:Dot(v2); local angle = d/(v1.magnitude * v2.magnitude) return math.acos(angle); end
local Omega = (m.hit.p - Turret.Position).unit; local Omega_c = Vector3.new(Omega.X, Omega.Y,0); local Theta = AngleBetween(Vector3.new(0,-1,0), Omega_c); joint.DesiredAngle = Theta; print(math.deg(Theta)); |
|
|
| Report Abuse |
|
|
iondriver
|
  |
| Joined: 18 Nov 2012 |
| Total Posts: 757 |
|
|
| 27 Feb 2015 05:12 PM |
Thanks Doc, you came the closest. The turret almost works now. There is still some odd behavior though. It can bee seen here: http://www.roblox.com/Testing-place?id=218112468
No, I don't want the tix. I just need to get this working, and I cant describe what it is doing. |
|
|
| Report Abuse |
|
|
|
| 27 Feb 2015 11:50 PM |
| That's easy. It's because it's being used on a vertical rotation axis and not horizontal. You just need to flip the Omega-c and change the Vector3.new(0,-1,0) to the correct orientation. |
|
|
| Report Abuse |
|
|