Class: SFML::Vector3

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/system/vector3.rb

Overview

3D vector with float components. Mirrors Vector2’s surface.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Vector3.



9
10
11
12
13
14
# File 'lib/sfml/system/vector3.rb', line 9

def initialize(x = 0, y = 0, z = 0)
  @x = x
  @y = y
  @z = z
  freeze
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



4
5
6
# File 'lib/sfml/system/vector3.rb', line 4

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



4
5
6
# File 'lib/sfml/system/vector3.rb', line 4

def y
  @y
end

#zObject (readonly)

Returns the value of attribute z.



4
5
6
# File 'lib/sfml/system/vector3.rb', line 4

def z
  @z
end

Class Method Details

.[](x, y, z) ⇒ Object



6
# File 'lib/sfml/system/vector3.rb', line 6

def self.[](x, y, z) = new(x, y, z)

.from_native(struct) ⇒ Object

:nodoc:



55
56
57
# File 'lib/sfml/system/vector3.rb', line 55

def self.from_native(struct) # :nodoc:
  new(struct[:x], struct[:y], struct[:z])
end

.zeroObject



7
# File 'lib/sfml/system/vector3.rb', line 7

def self.zero        = new(0, 0, 0)

Instance Method Details

#*(scalar) ⇒ Object



18
# File 'lib/sfml/system/vector3.rb', line 18

def *(scalar) = Vector3.new(@x * scalar, @y * scalar, @z * scalar)

#+(other) ⇒ Object



16
# File 'lib/sfml/system/vector3.rb', line 16

def +(other) = Vector3.new(@x + other.x, @y + other.y, @z + other.z)

#-(other) ⇒ Object



17
# File 'lib/sfml/system/vector3.rb', line 17

def -(other) = Vector3.new(@x - other.x, @y - other.y, @z - other.z)

#-@Object



20
# File 'lib/sfml/system/vector3.rb', line 20

def -@ = Vector3.new(-@x, -@y, -@z)

#/(scalar) ⇒ Object



19
# File 'lib/sfml/system/vector3.rb', line 19

def /(scalar) = Vector3.new(@x / scalar.to_f, @y / scalar.to_f, @z / scalar.to_f)

#==(other) ⇒ Object Also known as: eql?



22
23
24
# File 'lib/sfml/system/vector3.rb', line 22

def ==(other)
  other.is_a?(Vector3) && @x == other.x && @y == other.y && @z == other.z
end

#cross(other) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/sfml/system/vector3.rb', line 39

def cross(other)
  Vector3.new(
    (@y * other.z) - (@z * other.y),
    (@z * other.x) - (@x * other.z),
    (@x * other.y) - (@y * other.x)
  )
end

#deconstructObject



49
# File 'lib/sfml/system/vector3.rb', line 49

def deconstruct = [@x, @y, @z]

#deconstruct_keys(_keys) ⇒ Object



50
# File 'lib/sfml/system/vector3.rb', line 50

def deconstruct_keys(_keys) = { x: @x, y: @y, z: @z }

#dot(other) ⇒ Object



37
# File 'lib/sfml/system/vector3.rb', line 37

def dot(other) = (@x * other.x) + (@y * other.y) + (@z * other.z)

#hashObject



26
# File 'lib/sfml/system/vector3.rb', line 26

def hash = [@x, @y, @z].hash

#lengthObject



28
# File 'lib/sfml/system/vector3.rb', line 28

def length    = Math.sqrt(length_sq)

#length_sqObject



29
# File 'lib/sfml/system/vector3.rb', line 29

def length_sq = (@x * @x) + (@y * @y) + (@z * @z)

#normalizeObject



31
32
33
34
35
# File 'lib/sfml/system/vector3.rb', line 31

def normalize
  len = length
  return Vector3.zero if len.zero?
  self / len
end

#to_aObject



47
# File 'lib/sfml/system/vector3.rb', line 47

def to_a = [@x, @y, @z]

#to_hObject



48
# File 'lib/sfml/system/vector3.rb', line 48

def to_h = { x: @x, y: @y, z: @z }

#to_native_fObject

:nodoc:



59
60
61
# File 'lib/sfml/system/vector3.rb', line 59

def to_native_f # :nodoc:
  C::System::Vector3f.new.tap { |v| v[:x] = @x; v[:y] = @y; v[:z] = @z }
end

#to_sObject Also known as: inspect



52
# File 'lib/sfml/system/vector3.rb', line 52

def to_s = "Vector3(#{@x}, #{@y}, #{@z})"