Skip to content

Commit

Permalink
Updated construct and paintable_construct to use matrices for space c…
Browse files Browse the repository at this point in the history
…oords.
  • Loading branch information
wkiri committed Nov 29, 2011
1 parent 27c542a commit a4aa5ab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
13 changes: 9 additions & 4 deletions construct.lua
Expand Up @@ -2,6 +2,7 @@ require "middleclass" -- definition of useful class construction methods
require 'constants'
require 'spob'
require 'segment'
local matrix = require 'lua-matrix/lua/matrix'

-- Construct class: Spob composed of multiple Segments

Expand Down Expand Up @@ -31,10 +32,14 @@ function Construct:initialize(host, n_segments, radius, height, thickness, perio
stop = { x = math.cos(s * angle_subtended) * self.radius,
y = math.sin(s * angle_subtended) * self.radius }
-- Walk out the points of the face in clockwise fashion
p1 = { x = start.x, y = start.y, z = half_thickness }
p2 = { x = stop.x, y = stop.y, z = half_thickness }
p3 = { x = stop.x, y = stop.y, z = -half_thickness }
p4 = { x = start.x, y = start.y, z = -half_thickness }
-- p1 = { x = start.x, y = start.y, z = half_thickness }
-- p2 = { x = stop.x, y = stop.y, z = half_thickness }
-- p3 = { x = stop.x, y = stop.y, z = -half_thickness }
-- p4 = { x = start.x, y = start.y, z = -half_thickness }
p1 = matrix{ start.x, start.y, half_thickness }
p2 = matrix{ stop.x, stop.y, half_thickness }
p3 = matrix{ stop.x, stop.y, -half_thickness }
p4 = matrix{ start.x, start.y, -half_thickness }
faces = { { p1, p2, p3, p4 } }
table.insert(self.segments, Segment:new(self, faces))

Expand Down
25 changes: 16 additions & 9 deletions paintable_construct.lua
@@ -1,5 +1,6 @@
require "middleclass" -- definition of useful class construction methods
require "paintable"
local matrix = require 'lua-matrix/lua/matrix'

PaintableConstruct = Paintable:subclass('PaintableConstruct')

Expand All @@ -9,22 +10,28 @@ function PaintableConstruct:initialize(spob, color)
end

function PaintableConstruct:draw()
segments = self.spob.segments
local segments = self.spob.segments

love.graphics.setColor(self.color.R, self.color.G, self.color.B)

construct_location = self.spob:getLocation()
local construct_location = self.spob:getLocation()
for seg_i, segment in pairs(self.spob.segments) do
for face_i, face in pairs(segment.faces) do
polygon = {}
for _, point in pairs(face) do
sin_theta = math.sin(self.spob.rotation_angle)
cos_theta = math.cos(self.spob.rotation_angle)
local rot_p = { x = point.x * cos_theta - point.y * sin_theta,
y = point.x * sin_theta + point.y * cos_theta }
local p = { x = rot_p.x + construct_location.x,
y = rot_p.y + construct_location.y }
screen_x, screen_y = scale:screenCoords(p)
local theta = self.spob.rotation_angle
local sin_theta = math.sin(theta)
local cos_theta = math.cos(theta)
--local rot_p = { x = point.x * cos_theta - point.y * sin_theta,
-- y = point.x * sin_theta + point.y * cos_theta }
-- This is a 3D rotation matrix around the z axis
local rot_p = matrix{{cos_theta,-sin_theta,0},
{sin_theta, cos_theta,0},
{ 0, 0,1}} * point
--local p = { x = rot_p.x + construct_location.x,
-- y = rot_p.y + construct_location.y }
local p = rot_p + construct_location -- yummy, I'm a matrix
local screen_x, screen_y = scale:screenCoords(p)
table.insert(polygon, screen_x)
table.insert(polygon, screen_y)
end
Expand Down

0 comments on commit a4aa5ab

Please sign in to comment.