generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Dynamically finding an angle

Previous Thread :: Next Thread 
iondriver is not online. 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
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
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
AbstractMadness is not online. AbstractMadness
Joined: 22 Dec 2014
Total Posts: 20425
27 Feb 2015 12:18 PM
:Dot() ?
Report Abuse
iondriver is not online. 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
xSoulStealerx is not online. xSoulStealerx
Joined: 07 May 2008
Total Posts: 1624
27 Feb 2015 01:06 PM
part.CFrame = CFrame.new(part.Position, mouse.Hit.p)

points part to mouse position
Report Abuse
iondriver is not online. 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 is not online. iondriver
Joined: 18 Nov 2012
Total Posts: 757
27 Feb 2015 02:09 PM
Bump
Report Abuse
CrescentJade is not online. CrescentJade
Joined: 07 Jul 2010
Total Posts: 5494
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 is not online. 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
CoolioTree is not online. CoolioTree
Joined: 26 Dec 2012
Total Posts: 2509
27 Feb 2015 02:33 PM
^ oh my god, trigonometric functions would be the most simplest answer to his problem.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
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
CoolioTree is not online. CoolioTree
Joined: 26 Dec 2012
Total Posts: 2509
27 Feb 2015 02:37 PM
what does the :Dot() function do?
Report Abuse
iondriver is not online. 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
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
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 is not online. 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
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image