Ftor (“Fake vecTOR”), a vector-like class for 2D position/movement.
(NB: See angle for an important note about why angles appear to be the opposite of what you may expect.)
Ftor is useful for storing 2D coordinates (x,y) as well as vector quantities such as velocity and acceleration (representationally, points and vectors are equivalent.) Although Ftors are always represented internally as Cartesian coordinates (x, y), it is possible to deal with an Ftor as polar coordinates (angle, magnitude) instead. See new_am and set_am!, for example.
Ftor is a “fake” vector because it has certain convenient properties which differ from “true” vectors (i.e. vectors in a strict mathematical sense).
Unlike vectors, Ftors may be multiplied or divided to another Ftor. This is equivalent to multiplying or dividing each component by the corresponding component in the second Ftor. If you like, you can think of this feature as scaling each component of the Ftor by a separate factor:
Ftor(a,b) * Ftor(c,d) = Ftor(a*c, b*d)
Of course, Ftors also have the usual vector behavior for addition/subraction between two Ftors, and multiplication/division of an Ftor by a scalar:
Ftor(a,b) + Ftor(c,d) = Ftor(a+c, b+d) Ftor(a,b) * n = Ftor(a*n, b*n)
Additionally, Ftor contains functions for manipulating itself. You can both get and set such properties as angle, magnitude, unit, and normal, and the Ftor will change in-place as needed. For example, if you set angle=, the vector will change to have the new angle, but keeps the same magnitude as before.
Ftor attempts to save processing time (at the expense of memory) by storing secondary properties (angle, magnitude, etc.) whenever they are calculated,so that they need not be calculated repeatedly. If the vector changes, the properties will be calculated again the next time they are needed. (In future versions, it may be possible to disable this feature for certain Ftors, for example if they will change very often, to save memory.)
Returns a new Ftor which represents the difference in position of two points p1 and p2. (p1 and p2 can be Ftors, size-2 Arrays, or anything else which has two numerical components and responds to #.)
In other words, assuming v is the Ftor returned by this function:
p1 + v = p2
# File lib/rubygame/ftor.rb, line 101 def self.new_from_to(p1,p2) return self.new(p2-p1,p2-p1) end
Perform multiplication of this Ftor by the scalar other, like so:
Ftor(a,b) * n = Ftor(a*n, b*n)
However, if this causes TypeError, attempt to extract indices 0 and 1 with other’s # operator, and multiply them into the corresponding components of this Ftor, like so:
Ftor(a,b) * Ftor(c,d) = Ftor(a*c, b*d) Ftor(a,b) * [c,d] = Ftor(a*c, b*d)
# File lib/rubygame/ftor.rb, line 215 def *(other) return self.class.new(@x*other,@y*other) rescue TypeError return self.class.new(@x*other,@y*other) end
Like #+, but performs subtraction instead of addition.
# File lib/rubygame/ftor.rb, line 203 def -(other) return self.class.new(@x-other,@y-other) end
Like #*, but performs division instead of multiplication.
# File lib/rubygame/ftor.rb, line 222 def /(other) x, y = @x.to_f, @y.to_f return self.class.new(x/other,y/other) rescue TypeError return self.class.new(x/other,y/other) end
# File lib/rubygame/ftor.rb, line 369 def _clear @angle = nil @magnitude = nil @normal = nil @unit = nil return self end
Alias for angle
Alias for angle=
Alias for unit=
Return the angle (radians) this Ftor forms with the positive X axis. This is the same as the Ftor’s angle in a polar coordinate system.
IMPORTANT: Because the positive Y axis on the Rubygame::Screen points downwards, an angle in the range 0..PI will appear to point downwards, rather than upwards! This also means that positive rotation will appear clockwise, and negative rotation will appear counterclockwise! This is the opposite of what you would expect in geometry class!
# File lib/rubygame/ftor.rb, line 238 def angle @angle or @angle = Math.atan2(@y,@x) end
Alias for magnitude
Alias for magnitude=
Alias for normal
Alias for normal=
IMPORTANT: Positive rotation will appear clockwise, and negative rotation will appear counterclockwise! See angle for the reason.
# File lib/rubygame/ftor.rb, line 344 def rotate!(angle) # case(angle) # when HALF_PI, -THREE_HALF_PI # self.set!(@y,-@x) # when THREE_HALF_PI, -HALF_PI # self.set!(-@y,@x) # when PI, -PI # self.set!(@y,-@x) # when 0, TWO_PI, -TWO_PI # self.set!(@y,-@x) # else self.a += angle # end return self end
Returns an Array of this Ftor’s components, [x,y].
# File lib/rubygame/ftor.rb, line 167 def to_a [@x,@y] end
Alias for to_a
Alias for unit
Alias for unit=
# File lib/rubygame/ftor.rb, line 288 def unit m = magnitude().to_f @unit or @unit = Ftor.new(@x/m, @y/m) end
# File lib/rubygame/ftor.rb, line 301 def unit=(other) set!( *(self.class.new(*other).unit() * magnitude()) ) end