Class: Bullet3::Vector3
- Inherits:
-
Object
- Object
- Bullet3::Vector3
- 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
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Class Method Summary collapse
Instance Method Summary collapse
- #*(scalar) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(scalar) ⇒ Object
- #==(other) ⇒ Object
- #[](index) ⇒ Object
- #[]=(index, value) ⇒ Object
- #absolute ⇒ Object
- #angle(other) ⇒ Object
- #cross(other) ⇒ Object
- #distance(other) ⇒ Object
- #distance2(other) ⇒ Object
- #dot(other) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(x = 0.0, y = 0.0, z = 0.0) ⇒ Vector3
constructor
A new instance of Vector3.
- #inspect ⇒ Object
- #length ⇒ Object
- #length2 ⇒ Object
- #lerp(other, t) ⇒ Object
- #normalize ⇒ Object (also: #normalize!)
- #normalized ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
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
#x ⇒ Object
Returns the value of attribute x.
10 11 12 |
# File 'lib/bullet3/linear_math/vector3.rb', line 10 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
10 11 12 |
# File 'lib/bullet3/linear_math/vector3.rb', line 10 def y @y end |
#z ⇒ Object
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
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 |
.zero ⇒ Object
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
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 |
#absolute ⇒ Object
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 |
#inspect ⇒ Object
139 140 141 |
# File 'lib/bullet3/linear_math/vector3.rb', line 139 def inspect "#<Bullet3::Vector3 (#{x}, #{y}, #{z})>" end |
#length ⇒ Object
66 67 68 |
# File 'lib/bullet3/linear_math/vector3.rb', line 66 def length Math.sqrt(length2) end |
#length2 ⇒ Object
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 |
#normalize ⇒ Object 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 |
#normalized ⇒ Object
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_a ⇒ Object
131 132 133 |
# File 'lib/bullet3/linear_math/vector3.rb', line 131 def to_a [x, y, z] end |
#to_s ⇒ Object
135 136 137 |
# File 'lib/bullet3/linear_math/vector3.rb', line 135 def to_s "(#{x}, #{y}, #{z})" end |