- Joined
- Jul 10, 2007
- Messages
- 6,306
I know I kind of left, but I figured that I'd post something useful
The y and z are swapped (like most game engines).
I'll probably add little demo on how to calculate the interception point between two velocities. It's actually pretty simple.
I'll also probably put up some quaternion functions that you can use with CalculateAngleRotation.
Here's a sweet post on it -> http://forum.onlineconversion.com/showpost.php?p=41014&postcount=2
http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors
I saw that someone posted a resource for 2D. Here's 3D. Enjoy. I made this because I needed it for an assignment and a lot of the online equations are wrong -.-. They are all over the place. These are the working equations. Yes, I took the few minutes to solve them after my laziness failed >.>.
If anyone wants to understand how they work
Keep in mind that Sin and Cos return fractions. You can multiply fractions together to get a fraction of a fraction. How would you get x in that picture given yaw and pitch? x is a fraction of xz, which is a fraction of xzy. xz is the Cos component of xzy. x is the Cos component of xz. xz = xzy*cos(pitch). x = xz*cos(yaw). How would you get y and z?
To understand rotate towards, take a look at a unit circle. Just google trig circle. From there, work it out yourself
. It works : P. Keep in mind that unit vectors are those x,y components, so comparing unit vectors directly will also work. However, you'll still have to return the angle and apply the rotation to the vector based on angles ;(. The extra cosine and sine are overhead, but they make the function more understandable. I guess I could add a between points version of it. See the end of this post to understand how checking the unit vectors would work
. Remember, pitch is composed of two fun vectors, xz and y!
edit
given a unit vector current and a target unit vector target and a max angular rotation Cos(rotation)
current.x - target.x < angular rotation?
0 < current.z - target.z?
pitch would use the magnitude of xz as x and y as z
current.xz - target.xz < angular rotation?
0 < current.y - target.y?
the problem is returning the angle
The y and z are swapped (like most game engines).
I'll probably add little demo on how to calculate the interception point between two velocities. It's actually pretty simple.
I'll also probably put up some quaternion functions that you can use with CalculateAngleRotation.
Here's a sweet post on it -> http://forum.onlineconversion.com/showpost.php?p=41014&postcount=2
http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors
I saw that someone posted a resource for 2D. Here's 3D. Enjoy. I made this because I needed it for an assignment and a lot of the online equations are wrong -.-. They are all over the place. These are the working equations. Yes, I took the few minutes to solve them after my laziness failed >.>.
If anyone wants to understand how they work
Keep in mind that Sin and Cos return fractions. You can multiply fractions together to get a fraction of a fraction. How would you get x in that picture given yaw and pitch? x is a fraction of xz, which is a fraction of xzy. xz is the Cos component of xzy. x is the Cos component of xz. xz = xzy*cos(pitch). x = xz*cos(yaw). How would you get y and z?
To understand rotate towards, take a look at a unit circle. Just google trig circle. From there, work it out yourself
JASS:
function SetUnitYaw takes unit whichUnit, real radians returns nothing
call SetUnitFacing(whichUnit, radians*57.2957795)
endfunction
function GetUnitYaw takes unit whichUnit returns real
return GetUnitFacing(whichUnit)/57.2957795
endfunction
function RotateUnitYaw takes unit whichUnit, real radians returns nothing
call SetUnitYaw(whichUnit, GetUnitYaw(whichUnit) + radians)
endfunction
//to work with pitch, use Vexorian's dummy.mdl
//this is buggy because vexorian's dummy.mdl doesn't go all the way around. It should go from 0-360, not 0-180.
//with the model as it is now, you can't have upside down units
function SetUnitPitch takes unit whichUnit, real radians returns nothing
local integer i = R2I(radians*57.2957795 + 90.5)
if (i > 179) then
set i = 179
elseif (i < 0) then
set i = 0
endif
//should store current pitch into an array
//set unit____currentPitch[GetUnitUserData(whichUnit)] = radians
call SetUnitAnimationByIndex(whichUnit, i)
endfunction
function GetUnitPitch takes unit whichUnit returns real
//return the value in that array
return //unit____currentPitch[GetUnitUserData(whichUnit)]
endfunction
function RotateUnitPitch takes unit whichUnit, real radians returns nothing
call SetUnitPitch(whichUnit, GetUnitPitch(whichUnit) + radians)
endfunction
//the next three functions are used to convert a yaw and pitch to
//a unit vector representing the direction
function CalculateDirectionX takes real yaw, real pitch returns real
return Cos(yaw)*Cos(pitch)
endfunction
function CalculateDirectionY takes real pitch returns real
return Sin(pitch)
endfunction
function CalculateDirectionZ takes real yaw, real pitch returns real
return Sin(yaw)*Cos(pitch)
endfunction
//the next two functions are used to calculate yaw and pitch from a
//vector representing direction (does not have to be a unit vector)
function CalculateVectorYaw takes real x, real z returns real
return Atan2(z, x)
endfunction
function CalculateVectorPitch takes real x, real y, real z returns real
return Atan2(y, SquareRoot(x*x + z*z))
endfunction
function CalculateVectorMagnitude takes real x, real y, real z returns real
return SquareRoot(x*x + y*y + z*z)
endfunction
//howMuchPitchToRotateBy = CalculateAngleRotation(myAngle, myDesiredAngle, myPitchTurnRate)
//HowMuchYawToRotateBy = CalculateAngleRotation(myAngle, myDesiredAngle, myYawTurnRate)
//RotateUnitYaw(myUnit, howMuchYawToRotateBy)
//RotateUnitPitch(myUnit, howMuchPitchToRotateBy)
function CalculateAngleRotation takes real currentAngle, real targetAngle, real turnRate returns real
if (Cos(currentAngle - targetAngle) < Cos(turnRate)) then
if (0 < Sin(currentAngle - targetAngle)) then
return -turnRate
else
return turnRate
endif
endif
return targetAngle - currentAngle
endfunction
edit
given a unit vector current and a target unit vector target and a max angular rotation Cos(rotation)
current.x - target.x < angular rotation?
0 < current.z - target.z?
pitch would use the magnitude of xz as x and y as z
current.xz - target.xz < angular rotation?
0 < current.y - target.y?
the problem is returning the angle
Attachments
Last edited: