Author Topic: [LuaZM] Rotating an object  (Read 3512 times)

0 Members and 1 Guest are viewing this topic.

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
[LuaZM] Rotating an object
« on: November 28, 2012, 08:19:17 am »
OK, I am trying to learn how to do 3D using this guide, and  have gotten to the part for rotating around a point.

The problem is, all it seems to be doing is skewing my cube.

Here is my code:

Code: [Select]
-- base functions
local clear = zmg.clear
local drawCircle = zmg.drawCircle
local drawLine = zmg.drawLine
local drawPoint = zmg.drawPoint
local drawRectFill = zmg.drawRectFill
local drawText = zmg.drawText
local fastCopy = zmg.fastCopy
local keyMenuFast = zmg.keyMenuFast
local keyDirectPoll = zmg.keyDirectPoll
local keyDirect = zmg.keyDirect
local makeColor = zmg.makeColor
local floor = math.floor
local sin = math.sin
local cos = math.cos
local pi = math.pi

-- major vars
local key = {f1=79, f2=69, f3=59, f4=49, f5=39, f6=29, alpha=77, exit=47,
optn=68, up=28, down=37, left=38, right=27, exe=31, shift=78}
local color = {bg=makeColor("white"), fg=makeColor("gray")}
local exit=0

-- 3d vars
local move = {x=50,y=50}
local piv = {x=0, y=0}
local angle = {z=0}
local roffset = {x=0, y=0}
local x = 0
local y = 0
local xd = 0
local yd = 0
local scale = 1

-- 3d arrays
local cube = {x,y}
cube.x = {-20, 20, -20, 20}
cube.y = {-20, -20, 20, 20}

-- functions
local function d2r(angle)
return angle*(pi/180)
end

local function drawCrosshair(x, y, size, color)
drawLine(x-size, y, x+size, y, color)
drawLine(x, y-size, x, y+size, color)
end

-- 3d
while exit~=1 do
keyDirectPoll()
clear()

-- SPLAT BEGIN
for i=1, 4, 1 do
xd = cube.x[i]-piv.x
yd = cube.y[i]-piv.y
roffset.x = xd*cos(d2r(angle.z)) - yd*sin(d2r(angle.z)) - xd
roffset.y = xd*sin(d2r(angle.z)) - yd*cos(d2r(angle.z)) - yd

x = (cube.x[i] + roffset.x) / scale + move.x
y = (cube.y[i] + roffset.y) / scale + move.y

drawCrosshair(x, y, 5/scale, color.fg)
end

if keyDirect(key.left)>0 then move.x = move.x-2 end
if keyDirect(key.right)>0 then move.x = move.x+2 end
if keyDirect(key.up)>0 then move.y = move.y-2 end
if keyDirect(key.down)>0 then move.y = move.y+2 end
if keyDirect(key.f1)>0 then scale = scale*1.02 end
if keyDirect(key.f2)>0 then scale = scale/1.02 end
if keyDirect(key.f3)>0 then angle.z = angle.z-1 end
if keyDirect(key.f4)>0 then angle.z = angle.z+1 end
if keyDirect(key.exit)>0 then exit=1 end
-- SPLAT END

drawText(1, 1, angle.z, color.fg, color.bg)

fastCopy()
end



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0