# Rotating An Object

rotate lua 3d

No replies to this topic

### #1 flyingfisch

flyingfisch

Casio Maniac

• Deputy
• 1891 posts
• Gender:Male
• Location:OH,USA
• Interests:Aviation, Skiing, Programming, Mountain Biking.

• Calculators:
fx-9860GII
fx-CG10 PRIZM

Posted 28 November 2012 - 01:18 PM

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:

```-- 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 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
```

### Also tagged with one or more of these keywords: rotate, lua, 3d

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users