forums.evilmana.com
Welcome, Guest. Please login or register.

Login with username, password and session length
September 08, 2010, 08:25:26 AM
.
News: 06/10/2009 - New Features For Forum.
Friendly URLs, twitter for profile, and video site URLs will auto-display the video.
Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read February 22, 2008, 02:09:12 PM #0
PL3X

Moving Within a 3D plane

Huro, I've been doing heaps of 3D stuff and came up with some interesting stuff and wondered if it was any good. Basically It will allow you to move in any direction through a 3d plane. Theres a small mistake put in so that people do actually have to know about 3d  Tongue

First off here are the variables and functions. all variables are named of what they are like screen.Depth odly enough is the screen depth value and all of these are called later between   Gu.start3d() and   Gu.end3d()

Code:
--starting off with some variables...

screen.Depth = 80

nearClip = 0.5
farClip = 1000

translateX = 0.8
translateY = -2
translateZ = -8

rotateX = 0
rotateY = 0
rotateZ = 0

-- and now the function
--this first one is very a very simple zoom in or out bassed on your curent Gum rotation location and the translate location. I don't think I need to bother to explain how it works.

zoom = function()
pad = Controls.read()

if pad:cross() and screen.Depth >= 8 and not pad:r() then
screen.Depth = screen.Depth - 1
end

if pad:circle() and screen.Depth < 80 and not pad:r() then
screen.Depth = screen.Depth + 1
end
end

--Now onto the more complex one

move = function()

pad = Controls.read()

if pad:right() and not pad:l() and not pad:r() then
  translateX = translateX - 0.2
end

if pad:left() and not pad:l() and not pad:r() then
  translateX = translateX + 0.2
end

if pad:up() and not pad:l() and not pad:r() then
  translateY = translateY - 0.2
end

if pad:down() and not pad:l() and not pad:r() then
  translateY = translateY + 0.2
end

if pad:l() and pad:up() then
  rotateX = rotateX - 0.05
end

if pad:l() and pad:down() then
  rotateX = rotateX + 0.05
end

if pad:l() and pad:left() then
  rotateZ = rotateZ + 0.05
end

if pad:l() and pad:right() then
  rotateZ = rotateZ - 0.05
end

if pad:l() and pad:circle() then
  rotateY = rotateY + 0.05
end

if pad:l() and pad:square() then
  rotateY = rotateY - 0.05
end
end

all of these variables are called in this way.

Code:
  Gu.start3d()

  Gu.clearColor (black)
  Gu.clearDepth(255);
  Gu.clear(Gu.COLOR_BUFFER_BIT+Gu.DEPTH_BUFFER_BIT)

  Gum.matrixMode(Gu.PROJECTION)
  Gum.loadIdentity()
  Gum.perspective(75, 16/9, 0.5, 1000)
  Gum.matrixMode(Gu.VIEW)
  Gum.loadIdentity()

  Gum.matrixMode(Gu.MODEL)
  Gum.loadIdentity()
  Gum.translate(0, 0, -7);
  Gu.disable(Gu.TEXTURE_2D)
  Gum.drawArray(Gu.VERTICIE TYPE, Gu.COLOR_8888+Gu.VERTEX_32BITF+Gu.TRANSFORM_3D, verticieTable)
 
  Gum.matrixMode(Gu.PROJECTION)
  Gum.loadIdentity()
  Gum.perspective(screen.Depth, resx/resy, nearClip, farClip)
  Gum.matrixMode(Gu.VIEW)
  Gum.loadIdentity()

  Gum.matrixMode(Gu.MODEL)
  Gum.loadIdentity()

  Gum.translate(translateX, translateY, translateZ)
  Gum.rotateXYZ(rotateX,rotateY,rotateZ)

  Gu.disable(Gu.TEXTURE_2D)

  Gum.drawArray(Gu.VERTICIE TYPE, Gu.COLOR_8888+Gu.VERTEX_32BITF+Gu.TRANSFORM_3D, verticieTable)

  Gu.end3d()

Now heres a small Exmaple of the actuall thing working.

Code:
red = color.new(255,255,255)
green = colo.new(0,255,255)r
yello = colo.new(255,255,0)
blue = color.new(0,0,255)

screen.Depth = 80

translateX = 2
translateY = -3.8
translateZ = -8

rotateX = 0.25
rotateY = 0.8
rotateZ = 0.1

Lines = {

{red, -1,  1,  1},
{red, -1, -1,  1},

{yellow, -1, -1,  1},
{yellow,  1, -1,  1},

{red,  1, -1,  1},
{red,  1,  1,  1},

{red,  1,  1, -1},
{red,  1, -1, -1},

{blue, -1.4,1,  1},
{blue,  1.4,1,  1},


{red, -1,  6, -3},
{red, -1, -1, -3},

{yellow, -1, -1, -3},
{yellow,  1, -1, -3},


{blue, -1.4,1, -3},
{blue,  1.4,1, -3},

{yellow, -1, -1,  1},
{yellow, -1, -1, -3},

{yellow,  1, -1,  1},
{yellow,  1, -1, -1},

{blue,  1.4,1,  1},
{blue,  1.4,1, -3},

{blue, -1.4,1,  1},
{blue, -1.4,1, -3},

{blue,  1.4,1,  1},
{blue,  0  ,2,  1},

{blue, -1.4,1,  1},
{blue,  0  ,2,  1},

{blue,  1.4,1, -3},
{blue,  0  ,2, -3},

{blue, -1.4,1, -3},
{blue,  0  ,2, -3},


{blue,  0 , 2,  1},
{blue,  0 , 2, -3},

{blue,  1 , 1, -3},
{blue,  4 , 1, -3},

{blue,  1.4,  1, -0.6},
{blue,  4,  1,  -0.6},

{blue,  4,  1,  -0.6},
{blue,  4,  1,  -3.4},

{red,  4,  6,  -3},
{red,  4, -1, -3},

{yellow,  4, -1, -3},
{yellow,  1, -1, -3},

{yellow,  1, -1, -1},
{yellow,  4, -1, -1},

{red,  4, -1, -1},
{red,  4,  1, -1},

{yellow,  4, -1, -1},
{yellow,  4, -1, -3},

{blue,  4,  1, -0.6},
{blue,  4,  2, -2},

{blue,  4,  1, -3.4},
{blue,  4,  2, -2},

{blue,  4,  2, -2},
{blue,  0,  2, -2},

{yellow, 1, -1, -1},
{yellow,-1, -1, -3},

{red,  1, -1, -3},
{red,  1,  6, -3},

{yellow,  -1, 3, -3},
{yellow,   4, 3, -3},

{yellow,  -1, 4, -3},
{yellow,   4, 4, -3},

{yellow,  -1, 5, -3},
{yellow,   4, 5, -3},

{blue,  -1, 6, -3},
{blue,   4, 6, -3},

{yellow,  -1, 0, -3},
{yellow,   4, 0, -3},

{yellow,  -1, -1, -3},
{yellow,  -1, -1, -6},

{red,  -1, -1, -6},
{red,  -1,  6, -6},

{red,  2, -1, -3},
{red,  2, 6, -3},

{red,  1, -1, -3},
{red,  1, 6, -3},

{red,  3, -1, -3},
{red,  3, 6, -3},

{red,  0, -1, -3},
{red,  0, 6, -3},

{blue,  -1,  6, -6},
{blue,  -1,  6, -3},

{yellow,  -1, -0, -3},
{yellow,  -1, -0, -6},

{yellow,  -1,  1, -3},
{yellow,  -1,  1, -6},

{yellow,  -1,  2, -3},
{yellow,  -1,  2, -6},

{yellow,  -1,  3, -3},
{yellow,  -1,  3, -6},

{yellow,  -1,  4, -3},
{yellow,  -1,  4, -6},

{yellow,  -1,  5, -3},
{yellow,  -1,  5, -6},

{yellow,  -1,  2, -3},
{yellow,   4,  2, -3},

{yellow,   4, -1, -3},
{yellow,   4, -1, -6},

{yellow,   4, -1, -6},
{yellow,  -1, -1, -6},

{red,  4, -1, -6},
{red,  4,  6, -6},

{blue,  4,  6, -6},
{blue,  4,  6, -3},

{blue,  4,  6, -6},
{blue, -1,  6, -6},

{yellow,  4,  0, -3},
{yellow,  4,  0, -6},

{yellow,  4,  1, -3},
{yellow,  4,  1, -6},

{yellow,  4,  2, -3},
{yellow,  4,  2, -6},

{yellow,  4,  3, -3},
{yellow,  4,  3, -6},

{yellow,  4,  4, -3},
{yellow,  4,  4, -6},

{yellow,  4,  5, -3},
{yellow,  4,  5, -6},

{blue,  4,  6, -3},
{blue,  4,  6, -6},

{blue,  4,  6, -3},
{blue,  4,  7, -6},

{blue, -1,  6, -3},
{blue, -1,  7, -6},

{blue,  4,  7, -6},
{blue, -1,  7, -6},

{blue,  4,  7, -6},
{blue,  4,  6, -6},

{blue,  -1,  7, -6},
{blue,  -1,  6, -6},

}

ClearSquare = {
  {blue, -20, -11, -7},
  {red, 20, 11, -7},
  {blue, 20, -11, -7},
  {red, 20, 11, -7},
  {blue, -20, -11, -7},
  {red, -20, 11, -7}
}

move = function()

pad = Controls.read()

if pad:right() and not pad:l() and not pad:r() then
  translateX = translateX - 0.2
end

if pad:left() and not pad:l() and not pad:r() then
  translateX = translateX + 0.2
end

if pad:up() and not pad:l() and not pad:r() then
  translateY = translateY - 0.2
end

if pad:down() and not pad:l() and not pad:r() then
  translateY = translateY + 0.2
end

if pad:l() and pad:up() then
  rotateX = rotateX - 0.05
end

if pad:l() and pad:down() then
  rotateX = rotateX + 0.05
end

if pad:l() and pad:left() then
  rotateZ = rotateZ + 0.05
end

if pad:l() and pad:right() then
  rotateZ = rotateZ - 0.05
end

if pad:l() and pad:circle() then
  rotateY = rotateY + 0.05
end

if pad:l() and pad:square() then
  rotateY = rotateY - 0.05
end
end

while true do

  zoom()
  screen:clear()
  Gu.start3d()

  Gu.clearColor (black)
  Gu.clearDepth(255);
  Gu.clear(Gu.COLOR_BUFFER_BIT+Gu.DEPTH_BUFFER_BIT)

  Gu.matrixMode(Gu.PROJECTION)
  Gum.loadIdentity()
  Gum.perspective(75, 16/9, 0.5, 1000)
  Gum.matrixMode(Gu.VIEW)
  Gum.loadIdentity()

  Gum.matrixMode(Gu.MODEL)
  Gum.loadIdentity()
  Gum.translate(0, 0, -7);
  Gu.disable(Gu.TEXTURE_2D)
  Gum.drawArray(Gu.TRIANGLES, Gu.COLOR_8888+Gu.VERTEX_32BITF+Gu.TRANSFORM_3D, ClearSquare)
 
  Gum.matrixMode(Gu.PROJECTION)
  Gum.loadIdentity()
  Gum.perspective(screen.Depth, resx/resy, nearClip, farClip)
  Gum.matrixMode(Gu.VIEW)
  Gum.loadIdentity()

  Gum.matrixMode(Gu.MODEL)
  Gum.loadIdentity()

  Gum.translate(translateX, translateY, translateZ)
  Gum.rotateXYZ(rotateX,rotateY,rotateZ)

  if rotateStatus == 1 then
  Gum.rotateXYZ(val * 0.79 * (Gu.PI/180), val * 0.98 * (Gu.PI/180.0), val * 1.32 * (Gu.PI/180.0))
  end

  Gu.disable(Gu.TEXTURE_2D)

  Gum.drawArray(Gu.LINES, Gu.COLOR_8888+Gu.VERTEX_32BITF+Gu.TRANSFORM_3D, Lines)

  Gu.end3d()
  move()

screen.flip()
oldpad = pad

end
Offline  
Read February 22, 2008, 02:51:16 PM #1
dan369

Re: Moving Within a 3D plane

nice one!!!:) always wanted to have an example of a 3d lua code apart from the rotating cube that just confuses me Huh?


Offline  
Read February 22, 2008, 02:54:00 PM #2
PL3X

Re: Moving Within a 3D plane

Thanks, rotating cube is actually surprisingly simple, people just get confused with the verticies and gu/gum function.
Offline  
Pages: [1]   Go Up
Jump to:  

Theme Update by Runic Warrior Originally created by m3talc0re