Class: Bullet3::Vector3

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bullet3/linear_math/vector3.rb,
lib/bullet3/linear_math/vector3.rb

Constant Summary collapse

EPSILON =
1e-6
COMPONENTS =
{ 0 => :x, 1 => :y, 2 => :z }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0.0, y = 0.0, z = 0.0) ⇒ Vector3

Returns a new instance of Vector3.



16
17
18
19
20
# File 'lib/bullet3/linear_math/vector3.rb', line 16

def initialize(x = 0.0, y = 0.0, z = 0.0)
  @x = Float(x)
  @y = Float(y)
  @z = Float(z)
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



10
11
12
# File 'lib/bullet3/linear_math/vector3.rb', line 10

def x
  @x
end

#yObject

Returns the value of attribute y.



10
11
12
# File 'lib/bullet3/linear_math/vector3.rb', line 10

def y
  @y
end

#zObject

Returns the value of attribute z.



10
11
12
# File 'lib/bullet3/linear_math/vector3.rb', line 10

def z
  @z
end

Class Method Details

.coerce(value) ⇒ Object

Raises:

  • (TypeError)


143
144
145
146
147
148
# File 'lib/bullet3/linear_math/vector3.rb', line 143

def self.coerce(value)
  coerced = try_coerce(value)
  return coerced if coerced

  raise TypeError, "expected Bullet3::Vector3 or a 3-element Array"
end

.try_coerce(value) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/bullet3/linear_math/vector3.rb', line 150

def self.try_coerce(value)
  return value if value.is_a?(self)

  return new(*value) if value.respond_to?(:to_ary) && value.to_ary.length == 3

  nil
end

.zeroObject



12
13
14
# File 'lib/bullet3/linear_math/vector3.rb', line 12

def self.zero
  new
end

Instance Method Details

#*(scalar) ⇒ Object



48
49
50
# File 'lib/bullet3/linear_math/vector3.rb', line 48

def *(scalar)
  self.class.new(x * Float(scalar), y * Float(scalar), z * Float(scalar))
end

#+(other) ⇒ Object



34
35
36
37
# File 'lib/bullet3/linear_math/vector3.rb', line 34

def +(other)
  other = self.class.coerce(other)
  self.class.new(x + other.x, y + other.y, z + other.z)
end

#-(other) ⇒ Object



39
40
41
42
# File 'lib/bullet3/linear_math/vector3.rb', line 39

def -(other)
  other = self.class.coerce(other)
  self.class.new(x - other.x, y - other.y, z - other.z)
end

#-@Object



44
45
46
# File 'lib/bullet3/linear_math/vector3.rb', line 44

def -@
  self.class.new(-x, -y, -z)
end

#/(scalar) ⇒ Object

Raises:

  • (ZeroDivisionError)


52
53
54
55
56
57
# File 'lib/bullet3/linear_math/vector3.rb', line 52

def /(scalar)
  scalar = Float(scalar)
  raise ZeroDivisionError, "divided by 0" if scalar.zero?

  self.class.new(x / scalar, y / scalar, z / scalar)
end

#==(other) ⇒ Object



59
60
61
62
63
64
# File 'lib/bullet3/linear_math/vector3.rb', line 59

def ==(other)
  other = self.class.try_coerce(other)
  return false unless other

  distance2(other) <= EPSILON * EPSILON
end

#[](index) ⇒ Object



22
23
24
# File 'lib/bullet3/linear_math/vector3.rb', line 22

def [](index)
  public_send(component_name(index))
end

#[]=(index, value) ⇒ Object



26
27
28
# File 'lib/bullet3/linear_math/vector3.rb', line 26

def []=(index, value)
  public_send("#{component_name(index)}=", Float(value))
end

#absoluteObject



127
128
129
# File 'lib/bullet3/linear_math/vector3.rb', line 127

def absolute
  self.class.new(x.abs, y.abs, z.abs)
end

#angle(other) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/bullet3/linear_math/vector3.rb', line 118

def angle(other)
  other = self.class.coerce(other)
  denominator = length * other.length
  return 0.0 if denominator <= EPSILON

  cosine = dot(other) / denominator
  Math.acos(cosine.clamp(-1.0, 1.0))
end

#cross(other) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/bullet3/linear_math/vector3.rb', line 96

def cross(other)
  other = self.class.coerce(other)
  self.class.new(
    (y * other.z) - (z * other.y),
    (z * other.x) - (x * other.z),
    (x * other.y) - (y * other.x)
  )
end

#distance(other) ⇒ Object



105
106
107
# File 'lib/bullet3/linear_math/vector3.rb', line 105

def distance(other)
  Math.sqrt(distance2(other))
end

#distance2(other) ⇒ Object



109
110
111
# File 'lib/bullet3/linear_math/vector3.rb', line 109

def distance2(other)
  (self - other).length2
end

#dot(other) ⇒ Object



91
92
93
94
# File 'lib/bullet3/linear_math/vector3.rb', line 91

def dot(other)
  other = self.class.coerce(other)
  (x * other.x) + (y * other.y) + (z * other.z)
end

#each(&block) ⇒ Object



30
31
32
# File 'lib/bullet3/linear_math/vector3.rb', line 30

def each(&block)
  to_a.each(&block)
end

#inspectObject



139
140
141
# File 'lib/bullet3/linear_math/vector3.rb', line 139

def inspect
  "#<Bullet3::Vector3 (#{x}, #{y}, #{z})>"
end

#lengthObject



66
67
68
# File 'lib/bullet3/linear_math/vector3.rb', line 66

def length
  Math.sqrt(length2)
end

#length2Object



70
71
72
# File 'lib/bullet3/linear_math/vector3.rb', line 70

def length2
  dot(self)
end

#lerp(other, t) ⇒ Object



113
114
115
116
# File 'lib/bullet3/linear_math/vector3.rb', line 113

def lerp(other, t)
  other = self.class.coerce(other)
  self + ((other - self) * Float(t))
end

#normalizeObject Also known as: normalize!



74
75
76
77
78
79
80
# File 'lib/bullet3/linear_math/vector3.rb', line 74

def normalize
  normalized = self.normalized
  self.x = normalized.x
  self.y = normalized.y
  self.z = normalized.z
  self
end

#normalizedObject



84
85
86
87
88
89
# File 'lib/bullet3/linear_math/vector3.rb', line 84

def normalized
  magnitude = length
  return self.class.zero if magnitude <= EPSILON

  self / magnitude
end

#to_aObject



131
132
133
# File 'lib/bullet3/linear_math/vector3.rb', line 131

def to_a
  [x, y, z]
end

#to_sObject



135
136
137
# File 'lib/bullet3/linear_math/vector3.rb', line 135

def to_s
  "(#{x}, #{y}, #{z})"
end