Hey guys!
I was recently converting some processing scripts into LUA for Corona and was curious if there would be a way to define a shape with coordinates (and fill it with color). That way it would be easier to make pseudo 3D Cubes and other stuff.
Some stuff for you to look at:
Wavy (original code: http://www.openprocessing.org/visuals/?visualID=5671)
--[[
WAVY
Original Code by BLueThen: http://www.openprocessing.org/visuals/?visualID=5671
Converted for Corona SDK by Christian Wisniewski - xxxfanta@googlemail.com
]]--
display.setStatusBar(display.HiddenStatusBar)
local a = 0
local background = display.newRect(0,0,display.contentWidth,display.contentHeight)
background:setFillColor(220)
local linesGroup = display.newGroup()
local function distance(bx,by,cx,cy)
local a = math.sqrt((math.pow(cx-bx,2)+math.pow(cy-by,2)))
return a
end
local function draw(event)
a = a-0.08
for i=linesGroup.numChildren,1,-1 do
linesGroup[i]:removeSelf()
linesGroup[i] = nil
end
for x = -7,6,1 do
for z = -7,6,1 do
local y = 24\*math.cos(0.55 \* distance(x,z,0,0)+a)
local xm = x\*17 -8.5
local xt = x\*17 +8.5
local zm = z\*17 -8.5
local zt = z\*17 +8.5
local halfw = display.contentWidth/2
local halfh = display.contentHeight/2
-- evtl math.floor
local isox1 = (xm - zm + halfw);
local isoy1 = ((xm + zm) \* 0.5 + halfh);
local isox2 = (xm - zt + halfw);
local isoy2 = ((xm + zt) \* 0.5 + halfh);
local isox3 = (xt - zt + halfw);
local isoy3 = ((xt + zt) \* 0.5 + halfh);
local isox4 = (xt - zm + halfw);
local isoy4 = ((xt + zm) \* 0.5 + halfh);
local meshLeft = display.newLine( isox2,isoy2-y,isox3,isoy3-y)
meshLeft:append(isox3,isoy3+40,isox2,isoy2+40,isox2,isoy2-y)
local meshRight = display.newLine( isox3,isoy3-y,isox4,isoy4-y)
meshRight:append(isox4,isoy4+40,isox3,isoy3+40,isox3,isoy3-y)
local meshTop = display.newLine( isox1,isoy1-y,isox2,isoy2-y)
meshTop:append(isox3,isoy3-y,isox4,isoy4-y,isox1,isoy1-y)
linesGroup:insert(meshLeft)
linesGroup:insert(meshRight)
linesGroup:insert(meshTop)
end
end
end
Runtime:addEventListener("enterFrame",draw)
Rotating 3D Cube
display.setStatusBar(display.HiddenStatusBar)
local background = display.newRect(0,0,display.contentWidth,display.contentHeight)
background:setFillColor(220)
local objectGroup = display.newGroup()
local focalLength = 1000;
local function make3DPoint(x,y,z)
local point = {}
point.x = x;
point.y = y;
point.z = z;
return point;
end
local function make2DPoint(x,y, depth, scaleFactor)
local point = {}
point.x = x;
point.y = y;
point.depth = depth;
point.scaleFactor = scaleFactor;
return point;
end
local function Transform3DPointsTo2DPoints(points, axisRotations)
local TransformedPointsArray = {};
local sx = math.sin(axisRotations.x);
local cx = math.cos(axisRotations.x);
local sy = math.sin(axisRotations.y);
local cy = math.cos(axisRotations.y);
local sz = math.sin(axisRotations.z);
local cz = math.cos(axisRotations.z);
local x,y,z, xy,xz, yx,yz, zx,zy, scaleFactor;
local i = #points;
while (i\>0) do
x = points[i].x;
y = points[i].y;
z = points[i].z;
-- rotation around x
xy = cx\*y - sx\*z;
xz = sx\*y + cx\*z;
-- rotation around y
yz = cy\*xz - sy\*x;
yx = sy\*xz + cy\*x;
-- rotation around z
zx = cz\*yx - sz\*xy;
zy = sz\*yx + cz\*xy;
scaleFactor = focalLength/(focalLength + yz);
x = zx\*scaleFactor;
y = zy\*scaleFactor;
z = yz;
TransformedPointsArray[i] = make2DPoint(x, y, -z, scaleFactor);
i=i-1
end
return TransformedPointsArray;
end
local pointsArray = {
make3DPoint(-100,-100,-100),
make3DPoint(100,-100,-100),
make3DPoint(100,-100,100),
make3DPoint(-100,-100,100),
make3DPoint(-100,100,-100),
make3DPoint(100,100,-100),
make3DPoint(100,100,100),
make3DPoint(-100,100,100)
}
local cubeAxisRotations = make3DPoint(0,0,0);
local rotationSpeedY = -5/100
local rotationSpeedX = 5/1000
local function draw(event)
-- erstmal alles löschen
for i=objectGroup.numChildren,1,-1 do
objectGroup[i]:removeSelf()
objectGroup[i] = nil
end
--objectGroup.x = display.contentWidth/2
objectGroup.y = display.contentHeight/2
--objectGroup.y = math.sin(objectGroup.x)\*display.contentHeight/100+display.contentHeight/2
print(objectGroup.y)
objectGroup.x = (objectGroup.x+5)%(display.contentWidth+100)
cubeAxisRotations.y = cubeAxisRotations.y + rotationSpeedY
cubeAxisRotations.x = cubeAxisRotations.x + rotationSpeedX
local screenPoints = Transform3DPointsTo2DPoints(pointsArray, cubeAxisRotations);
local meshTop = display.newLine( screenPoints[1].x,screenPoints[1].y,screenPoints[2].x,screenPoints[2].y)
meshTop:append(screenPoints[3].x,screenPoints[3].y,screenPoints[4].x,screenPoints[4].y,screenPoints[1].x,screenPoints[1].y)
local meshBottom = display.newLine( screenPoints[5].x,screenPoints[5].y,screenPoints[6].x,screenPoints[6].y)
meshBottom:append(screenPoints[7].x,screenPoints[7].y,screenPoints[8].x,screenPoints[8].y,screenPoints[5].x,screenPoints[5].y)
local connectionA = display.newLine( screenPoints[1].x,screenPoints[1].y,screenPoints[5].x,screenPoints[5].y)
local connectionB = display.newLine( screenPoints[2].x,screenPoints[2].y,screenPoints[6].x,screenPoints[6].y)
local connectionC = display.newLine( screenPoints[3].x,screenPoints[3].y,screenPoints[7].x,screenPoints[7].y)
local connectionD = display.newLine( screenPoints[4].x,screenPoints[4].y,screenPoints[8].x,screenPoints[8].y)
objectGroup:insert(meshTop)
objectGroup:insert(meshBottom)
objectGroup:insert(connectionA)
objectGroup:insert(connectionB)
objectGroup:insert(connectionC)
objectGroup:insert(connectionD)
end
local function rotate(event)
if event.numTaps == 2 then
print("double Tap")
focalLength = (focalLength-100)%1000
else
rotationSpeedY = 1/math.random(5,30)
rotationSpeedX = 1/math.random(5,30)
end
end
background:addEventListener("tap",rotate)
Runtime:addEventListener("enterFrame",draw)
Have fun and let me know if you plan on adding this feature someday! [import]uid: 13097 topic_id: 17800 reply_id: 317800[/import]