3D Cube and Wavy Animation - have fun :)

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! :wink: [import]uid: 13097 topic_id: 17778 reply_id: 317778[/import]

Thanks :slight_smile:
But I was just converting the script :wink:
But it would still be nice to be able to make polygones (i.e. for creating isometric terrain). [import]uid: 13097 topic_id: 17778 reply_id: 67805[/import]

The wavy looks so cool, xxxfanta!

Naomi [import]uid: 67217 topic_id: 17778 reply_id: 67800[/import]

This is awesome! I can’t comment as to what we plan on adding or when (not my area I’m afraid) - but either way, this is great :slight_smile: [import]uid: 52491 topic_id: 17778 reply_id: 67874[/import]

Too bad :wink:
I should have posted this in the Feature Request Forum I guess… :> [import]uid: 13097 topic_id: 17778 reply_id: 67910[/import]

Do you want it moved? :slight_smile: [import]uid: 52491 topic_id: 17778 reply_id: 67919[/import]

If it helps to get it into corona, yes please :smiley:
Otherwise the example codes might get lost there :> [import]uid: 13097 topic_id: 17778 reply_id: 67922[/import]

Grumble. Yeah, the sample may get buried.

If you’d like to make a second thread there you have my word I will not delete it and tell you off for dupes, though :wink:

Peach [import]uid: 52491 topic_id: 17778 reply_id: 67930[/import]

Haha okay I’ll make another thread and rename this one :slight_smile: [import]uid: 13097 topic_id: 17778 reply_id: 67933[/import]

Great :slight_smile: (Nice avatar by the way, did you draw it?) [import]uid: 52491 topic_id: 17778 reply_id: 67945[/import]

:slight_smile:

Haha yes, I did - in diffrent variations:
http://christianwisniewski.com/Char01.jpg

I’m actually splitting those apart and animate them in Corona for my next game where I want the user to be able to change the characters outfit and face (therefore I can’t use premade spritesheets for the animation but rather animate them via code)

Let me know if you want to see it in action :wink: [import]uid: 13097 topic_id: 17778 reply_id: 67946[/import]

very funky!

Im trying to think of how i can include this is future games etc now :smiley:

Thanks for converting.

Ps. out of interest have you run that on device yet? [import]uid: 69826 topic_id: 17778 reply_id: 67947[/import]

Hehe. Actually I was thinking of making some old C64-like Cracktros :smiley:

I haven’t tested it on a normal device yet… Got a new iPhone and now my provisioning profile ain’t working ._.

But tell me the results if you get it running! :slight_smile: [import]uid: 13097 topic_id: 17778 reply_id: 67948[/import]

Awesome; looks very interesting. (I think customization is a great thing to have in a game.)

Would love to see it in action :slight_smile: [import]uid: 52491 topic_id: 17778 reply_id: 67951[/import]