class SK::Matrix

Attributes

values[RW]

Public Class Methods

create_rotation(angle) click to toggle source
# File lib/shirokuro/math/matrix.rb, line 19
def self.create_rotation angle
        c = Math::cos(angle)
        s = Math::sin(angle)

        result = Matrix.new([
                +c, +s, 0.0, 0.0,
        -s, +c, 0.0, 0.0,
        0.0,  0.0,  1.0, 0.0,
        0.0,  0.0,  0.0, 1.0
        ])
end
create_scale(x, y) click to toggle source
# File lib/shirokuro/math/matrix.rb, line 40
def self.create_scale x, y
        Matrix.new([
        x, 0, 0, 0,
        0, y, 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1
        ])
end
create_translation(x, y) click to toggle source
# File lib/shirokuro/math/matrix.rb, line 31
def self.create_translation(x, y)
        Matrix.new([
                1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,
        x, y, 0, 1
    ])
end
identity() click to toggle source
# File lib/shirokuro/math/matrix.rb, line 10
def self.identity
        Matrix.new([
                1.0, 0, 0, 0,
                0, 1.0, 0, 0,
                0, 0, 0, 0,
                0, 0, 0, 0
        ])
end
new(values = []) click to toggle source
# File lib/shirokuro/math/matrix.rb, line 6
def initialize values = []
        @values = values
end

Public Instance Methods

*(right) click to toggle source
# File lib/shirokuro/math/matrix.rb, line 61
def * (right)
        result = []
        16.times do |i|
                result[i] = 0;
                4.times do |j|
              result[i] += @values[i / 4 * 4 + j] * right.values[i % 4 + j * 4]
        end
        end
    return Matrix.new(result)
end
decompose() click to toggle source
# File lib/shirokuro/math/matrix.rb, line 49
def decompose
        position = Vec2.new(@values[12], @values[13])
        # tan^-1(2/3) = tan^-1(-1/0)
        rotation = Math::tan(-1.0 * (@values[0] / @values[1]))
        scale = Vec2.new(@values[0], @values[5])
        transform = Transform.new
        transform.position.x = position.x
        transform.position.y = position.y
        transform.rotation = rotation
        transform
end