Example #1
0
File: mat4.go Project: ungerik/go3d
// AssignEulerRotation assigns Euler angle rotations to the rotation part of the matrix and sets the remaining elements to their ident value.
func (mat *T) AssignEulerRotation(yHead, xPitch, zRoll float32) *T {
	sinH := math.Sin(yHead)
	cosH := math.Cos(yHead)
	sinP := math.Sin(xPitch)
	cosP := math.Cos(xPitch)
	sinR := math.Sin(zRoll)
	cosR := math.Cos(zRoll)

	mat[0][0] = cosR*cosH - sinR*sinP*sinH
	mat[1][0] = -sinR * cosP
	mat[2][0] = cosR*sinH + sinR*sinP*cosH
	mat[3][0] = 0

	mat[0][1] = sinR*cosH + cosR*sinP*sinH
	mat[1][1] = cosR * cosP
	mat[2][1] = sinR*sinH - cosR*sinP*cosH
	mat[3][1] = 0

	mat[0][2] = -cosP * sinH
	mat[1][2] = sinP
	mat[2][2] = cosP * cosH
	mat[3][2] = 0

	mat[0][3] = 0
	mat[1][3] = 0
	mat[2][3] = 0
	mat[3][3] = 1

	return mat
}
Example #2
0
File: mat4.go Project: ungerik/go3d
// AssignZRotation assigns a rotation around the z axis to the rotation part of the matrix and sets the remaining elements to their ident value.
func (mat *T) AssignZRotation(angle float32) *T {
	cosine := math.Cos(angle)
	sine := math.Sin(angle)

	mat[0][0] = cosine
	mat[1][0] = -sine
	mat[2][0] = 0
	mat[3][0] = 0

	mat[0][1] = sine
	mat[1][1] = cosine
	mat[2][1] = 0
	mat[3][1] = 0

	mat[0][2] = 0
	mat[1][2] = 0
	mat[2][2] = 1
	mat[3][2] = 0

	mat[0][3] = 0
	mat[1][3] = 0
	mat[2][3] = 0
	mat[3][3] = 1

	return mat
}
Example #3
0
// Slerp returns the spherical linear interpolation quaternion between a and b at t (0,1).
// See http://en.wikipedia.org/wiki/Slerp
func Slerp(a, b *T, t float32) T {
	d := math.Acos(a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3])
	ooSinD := 1 / math.Sin(d)

	t1 := math.Sin(d*(1-t)) * ooSinD
	t2 := math.Sin(d*t) * ooSinD

	q := T{
		a[0]*t1 + b[0]*t2,
		a[1]*t1 + b[1]*t2,
		a[2]*t1 + b[2]*t2,
		a[3]*t1 + b[3]*t2,
	}

	return q.Normalized()
}
Example #4
0
// Rotated returns a counter-clockwise rotated copy of the vector.
func (vec *T) Rotated(angle float32) T {
	sinus := math.Sin(angle)
	cosinus := math.Cos(angle)
	return T{
		vec[0]*cosinus - vec[1]*sinus,
		vec[0]*sinus + vec[1]*cosinus,
	}
}
Example #5
0
// FromZAxisAngle returns a quaternion representing a rotation around the z axis.
func FromZAxisAngle(angle float32) T {
	angle *= 0.5
	return T{0, 0, math.Sin(angle), math.Cos(angle)}
}
Example #6
0
// FromAxisAngle returns a quaternion representing a rotation around and axis.
func FromAxisAngle(axis *vec3.T, angle float32) T {
	angle *= 0.5
	sin := math.Sin(angle)
	q := T{axis[0] * sin, axis[1] * sin, axis[2] * sin, math.Cos(angle)}
	return q.Normalized()
}