Class: BulldogPhysics::Quaternion

Inherits:
Object
  • Object
show all
Defined in:
lib/quaternion.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r = 1.0, i = 0, j = 0, k = 0) ⇒ Quaternion

Returns a new instance of Quaternion.



7
8
9
# File 'lib/quaternion.rb', line 7

def initialize(r = 1.0, i =0, j = 0, k = 0)
	@r, @i, @j, @k = r, i, j, k
end

Instance Attribute Details

#iObject

attr :data



5
6
7
# File 'lib/quaternion.rb', line 5

def i
  @i
end

#jObject

attr :data



5
6
7
# File 'lib/quaternion.rb', line 5

def j
  @j
end

#kObject

attr :data



5
6
7
# File 'lib/quaternion.rb', line 5

def k
  @k
end

#rObject

attr :data



5
6
7
# File 'lib/quaternion.rb', line 5

def r
  @r
end

Instance Method Details

#*(multiplier) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/quaternion.rb', line 29

def *(multiplier)
	q = self
	r = q.r*multiplier.r - q.i*multiplier.i -
			q.j*multiplier.j - q.k*multiplier.k
	i = q.r*multiplier.i + q.i*multiplier.r + 
			q.j*multiplier.k - q.k*multiplier.j
	j = q.r*multiplier.j + q.j*multiplier.r +
			q.k*multiplier.i - q.i*multiplier.k;
	k = q.r*multiplier.k + q.k*multiplier.r +
			q.i*multiplier.j - q.j*multiplier.i;
	Quaternion.new( r, i, j, k )
end

#addScaledVector(vector, scale) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/quaternion.rb', line 61

def addScaledVector(vector, scale)
	
	q = Quaternion.new(0,
			vector.x * scale,
			vector.y * scale,
			vector.z * scale)
			
	q *= self;
	@r += q.r * 0.5
	@i += q.i * 0.5
	@j += q.j * 0.5
	@k += q.k * 0.5
end

#dataObject



25
26
27
# File 'lib/quaternion.rb', line 25

def data
	[@r, @i, @j, @k]
end

#data=(data) ⇒ Object



18
19
20
21
22
23
# File 'lib/quaternion.rb', line 18

def data=(data)
	@r = data[0]
	@i = data[1]
	@j = data[2]
	@k = data[3]
end

#intialize(r, i, j, k) ⇒ Object



11
12
13
14
15
16
# File 'lib/quaternion.rb', line 11

def intialize(r, i, j, k)
	@r = r
	@i = i
	@j = j
	@k = k
end

#normalizeObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/quaternion.rb', line 42

def normalize()
	d = (@r*@r) + (@i*@i) + (@j*@j) + (@k*@k)
	
	if( d == 0)
		@r = 1
		return
	end
	#return if d.nan?
	begin
	d = (1.0 / Math.sqrt(d))
rescue 
	puts "FUCK D IS #{d}"
end
	@r *= d
	@i *= d
	@j *= d
	@k *= d
end

#rotateByVector(vector) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/quaternion.rb', line 75

def rotateByVector(vector)
	q = Quaternion.new( 0, vector.x, vector.y, vector.z)
	new_q = self * q
	@r = q.r
	@i = q.i
	@j = q.j
	@k = q.k
end