Skip to content

Commit

Permalink
refactored position updates for construct segments, so the position u…
Browse files Browse the repository at this point in the history
…pdate is done by construct:updateCoords instead of in the draw routine.
  • Loading branch information
IdahoEv committed Nov 29, 2011
1 parent 76ce83a commit 3c3236e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 36 deletions.
6 changes: 4 additions & 2 deletions construct.lua
Expand Up @@ -73,5 +73,7 @@ function Construct:updateCoords(time)

-- Update rotation angle
self.rotation_angle = time / self.rotational_period

end
for seg_i, segment in pairs(self.segments) do
segment:updateFaces(self.rotation_angle)
end
end
16 changes: 2 additions & 14 deletions paintable_construct.lua
Expand Up @@ -16,22 +16,10 @@ function PaintableConstruct:draw()

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
for face_i, face in pairs(segment.current_faces) do
polygon = {}
for _, point in pairs(face) do
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)
local screen_x, screen_y = scale:screenCoords(point)
table.insert(polygon, screen_x)
table.insert(polygon, screen_y)
end
Expand Down
32 changes: 24 additions & 8 deletions segment.lua
Expand Up @@ -8,20 +8,36 @@ Segment = class('Segment')

function Segment:initialize(construct, faces)
self.construct = construct
self.faces = faces
self.initial_faces = faces
self.current_faces = {}
self:updateFaces(0)
end

-- Return the (absolute) x,y coordinates of this Segment
function Segment:getLocation()
local construct_x, construct_y, construct_z

construct_loc = self.construct:getLocation()

-- print(self.name, construct_loc.x, construct_loc.y)
--return { x = self.location.x + construct_loc.x,
--y = self.location.y + construct_loc.y,
--z = self.location.z + construct_loc.z }

return self.location + contruct_loc
end

-- figure current location of faces, rotated by theta from original location
function Segment:updateFaces(theta)
self.current_faces = {}
for face_i, face in pairs(self.initial_faces) do
local rotated_face = {}
for point_i, point in pairs(face) do
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 }
rotated_face[point_i] = rot_p
end
self.current_faces[face_i] = rotated_face
end
end
12 changes: 0 additions & 12 deletions spob.lua
Expand Up @@ -71,11 +71,7 @@ function Spob:getLocation()
-- recurse!
host_loc = self.host:getLocation()
end
-- print(self.name, host_loc.x, host_loc.y)
return self.location + host_loc
--{ x = self.location.x + host_loc.x,
--y = self.location.y + host_loc.y,
--z = self.location.z + host_loc.z }
end

function Spob:drawOrbit(host_loc)
Expand All @@ -90,11 +86,6 @@ end

function Spob:distanceFromParent()
return matrix.len(self.location)
--return math.sqrt(
--(self.location[1][1] * self.location[1][1]) +
--(self.location[2][1] * self.location[2][1])
---- + (self.location.z * self.location.z)
--)
end

function Spob:distanceFromPoint(point)
Expand All @@ -111,10 +102,7 @@ function Spob:updateCoords(time)
math.cos(theta) * self.orbital_radius, -- Y coord
0
}
--self.location[1][1] = math.sin(theta) * self.orbital_radius -- X coord
--self.location[2][1] = math.cos(theta) * self.orbital_radius -- Y coord
end
-- self.location.z is unchanged... since we don't (yet) have
-- orbital inclination
if #(self.satellites) > 0 then
for _, spob in ipairs(self.satellites) do spob:updateCoords(time) end
Expand Down

0 comments on commit 3c3236e

Please sign in to comment.