util.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package model
  2. import (
  3. "math"
  4. "roserver/serverproto"
  5. )
  6. const (
  7. Eepsilon = 0.0004
  8. )
  9. type Direction3D struct {
  10. //x=roll,y=pitch,z=yaw
  11. dir serverproto.Vector3
  12. }
  13. func (this *Direction3D) roll() float32 {
  14. return this.dir.X
  15. }
  16. func (this *Direction3D) pitch() float32 {
  17. return this.dir.Y
  18. }
  19. func (this *Direction3D) yaw() float32 {
  20. return this.dir.Z
  21. }
  22. func (this *Direction3D) setRoll(val float32) {
  23. this.dir.X = val
  24. }
  25. func (this *Direction3D) setPitch(val float32) {
  26. this.dir.Y = val
  27. }
  28. func (this *Direction3D) setYaw(val float32) {
  29. this.dir.Z = val
  30. }
  31. func almostEqual(f1, f2 float32) bool {
  32. return math.Abs(float64(f1-f2)) < Eepsilon
  33. }
  34. //v1 - v2
  35. func Vector3Sub(v1, v2 *serverproto.Vector3) *serverproto.Vector3 {
  36. retVec3 := &serverproto.Vector3{}
  37. retVec3.X = v1.X - v2.X
  38. retVec3.Y = v1.Y - v2.Y
  39. retVec3.Z = v1.Z - v2.Z
  40. return retVec3
  41. }
  42. //v1 + v2
  43. func Vector3Add(v1, v2 *serverproto.Vector3) *serverproto.Vector3 {
  44. retVec3 := &serverproto.Vector3{}
  45. retVec3.X = v1.X + v2.X
  46. retVec3.Y = v1.Y + v2.Y
  47. retVec3.Z = v1.Z + v2.Z
  48. return retVec3
  49. }
  50. //vector3 len
  51. func Vector3Len(v *serverproto.Vector3) float32 {
  52. return float32(math.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z)))
  53. }