Class: GameMachine::Vector
- Inherits:
-
Object
- Object
- GameMachine::Vector
- Defined in:
- lib/game_machine/vector.rb
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
- #-(v1) ⇒ Object
- #==(v) ⇒ Object
- #distance(v) ⇒ Object
- #distance_squared(v) ⇒ Object
-
#initialize(x = 0.0, y = 0.0, z = 0.0) ⇒ Vector
constructor
A new instance of Vector.
- #inspect ⇒ Object
- #interpolate(vector, change_amount) ⇒ Object
- #lerp(vector, percent) ⇒ Object
- #lerp_float(start_loc, end_loc, percent) ⇒ Object
- #normalize ⇒ Object
- #set(new_x, new_y, new_z) ⇒ Object
- #zero ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(x = 0.0, y = 0.0, z = 0.0) ⇒ Vector
Returns a new instance of Vector.
14 15 16 17 18 |
# File 'lib/game_machine/vector.rb', line 14 def initialize(x=0.0,y=0.0,z=0.0) @x = x @y = y @z = z end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
4 5 6 |
# File 'lib/game_machine/vector.rb', line 4 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
4 5 6 |
# File 'lib/game_machine/vector.rb', line 4 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
4 5 6 |
# File 'lib/game_machine/vector.rb', line 4 def z @z end |
Class Method Details
.from(v) ⇒ Object
10 11 12 |
# File 'lib/game_machine/vector.rb', line 10 def self.from(v) new(v.x,v.y,v.z) end |
.from_array(ar) ⇒ Object
6 7 8 |
# File 'lib/game_machine/vector.rb', line 6 def self.from_array(ar) new(ar[0],ar[1],ar[2]) end |
.norm(x, y) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/game_machine/vector.rb', line 72 def self.norm(x,y) dx = 0.0 dy = 0.0 length = Math.sqrt(x*x + y*y) if length != 0 dx = x * (1/length) dy = y * (1/length) end [dx,dy] end |
Instance Method Details
#-(v1) ⇒ Object
68 69 70 |
# File 'lib/game_machine/vector.rb', line 68 def -(v1) self.class.new( @x - v1.x, @y - v1.y ) end |
#==(v) ⇒ Object
20 21 22 |
# File 'lib/game_machine/vector.rb', line 20 def==(v) @x == v.x && @y = v.y && @z = v.z end |
#distance(v) ⇒ Object
49 50 51 |
# File 'lib/game_machine/vector.rb', line 49 def distance(v) Math.sqrt(distance_squared(v)) end |
#distance_squared(v) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/game_machine/vector.rb', line 41 def distance_squared(v) dx = @x - v.x dy = @y - v.y #dz = @z - v.z #(dx * dx + dy * dy + dz * dz); (dx * dx + dy * dy); end |
#inspect ⇒ Object
93 94 95 |
# File 'lib/game_machine/vector.rb', line 93 def inspect "#{x} #{y} #{z}" end |
#interpolate(vector, change_amount) ⇒ Object
62 63 64 65 66 |
# File 'lib/game_machine/vector.rb', line 62 def interpolate(vector,change_amount) @x=(1-change_amount)*@x + change_amount*vector.x @y=(1-change_amount)*@y + change_amount*vector.y #@z=(1-change_amount)*@z + change_amount*vector.z end |
#lerp(vector, percent) ⇒ Object
53 54 55 56 |
# File 'lib/game_machine/vector.rb', line 53 def lerp(vector,percent) @x = lerp_float(x,vector.x,percent) @y = lerp_float(y,vector.y,percent) end |
#lerp_float(start_loc, end_loc, percent) ⇒ Object
58 59 60 |
# File 'lib/game_machine/vector.rb', line 58 def lerp_float(start_loc,end_loc,percent) start_loc + (percent * (end_loc - start_loc)) end |
#normalize ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/game_machine/vector.rb', line 83 def normalize vector = self.class.new length = Math.sqrt(@x*@x + @y*@y) if length != 0 vector.x = @x * (1/length) vector.y = @y * (1/length) end vector end |
#set(new_x, new_y, new_z) ⇒ Object
35 36 37 38 39 |
# File 'lib/game_machine/vector.rb', line 35 def set(new_x,new_y,new_z) @x = new_x @y = new_y @z = new_z end |
#zero ⇒ Object
29 30 31 32 33 |
# File 'lib/game_machine/vector.rb', line 29 def zero @x = 0.0 @y = 0.0 @z = 0.0 end |
#zero? ⇒ Boolean
25 26 27 |
# File 'lib/game_machine/vector.rb', line 25 def zero? @x == 0 && @y == 0 && z == 0 end |