mirror of
https://github.com/bloeys/gglm.git
synced 2025-12-29 05:28:20 +00:00
Angle and axis methods for quat
This commit is contained in:
@ -4,4 +4,7 @@ const (
|
||||
Pi float32 = 3.14159265359
|
||||
Deg2Rad float32 = Pi / 180
|
||||
Rad2Deg float32 = 180 / Pi
|
||||
|
||||
//CosHalf is Cos32(0.5)
|
||||
CosHalf float32 = 0.87758256189
|
||||
)
|
||||
|
||||
31
gglm/quat.go
31
gglm/quat.go
@ -16,6 +16,37 @@ func (q *Quat) Eq(q2 *Quat) bool {
|
||||
return q.Data == q2.Data
|
||||
}
|
||||
|
||||
//Angle returns the angle represented by this quaternion
|
||||
func (q *Quat) Angle() float32 {
|
||||
|
||||
if Abs32(q.Data[3]) > CosHalf {
|
||||
|
||||
a := Asin32(Sqrt32(q.Data[0]*q.Data[0]+q.Data[1]*q.Data[1]+q.Data[2]*q.Data[2])) * 2
|
||||
if q.Data[3] < 0 {
|
||||
return Pi*2 - a
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
return Acos32(q.Data[3]) * 2
|
||||
}
|
||||
|
||||
//Axis returns the rotation axis represented by this quaternion
|
||||
func (q *Quat) Axis() *Vec3 {
|
||||
|
||||
var t float32 = 1 - q.Data[3]*q.Data[3]
|
||||
if t <= 0 {
|
||||
return &Vec3{Data: [3]float32{0, 0, 1}}
|
||||
}
|
||||
|
||||
t = 1 / Sqrt32(t)
|
||||
return &Vec3{Data: [3]float32{
|
||||
q.Data[0] * t,
|
||||
q.Data[1] * t,
|
||||
q.Data[2] * t,
|
||||
}}
|
||||
}
|
||||
|
||||
//Euler takes rotations in radians and produces a rotation that
|
||||
//rotates around the z-axis, y-axis and lastly x-axis.
|
||||
func NewQuatEuler(v *Vec3) *Quat {
|
||||
|
||||
@ -35,3 +35,11 @@ func Sincos32(x float32) (sinx, cosx float32) {
|
||||
a, b := math.Sincos(float64(x))
|
||||
return float32(a), float32(b)
|
||||
}
|
||||
|
||||
func Abs32(x float32) float32 {
|
||||
return float32(math.Abs(float64(x)))
|
||||
}
|
||||
|
||||
func Sqrt32(x float32) float32 {
|
||||
return float32(math.Sqrt(float64(x)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user