Moving in 3d

Hello all, recently I was working in 3d and i whipped together this in a few minutes:

-- Into the Thid Dimension

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    walls=mesh()
    verts={
    vec3(25,0,25),
    vec3(25,0,-25),
    vec3(-25,0,25),
    vec3(-25,0,-25),
    vec3(25,25,25),
    vec3(25,25,-25),
    vec3(-25,25,25),
    vec3(-25,25,-25)
    }
    tex={
    vec2(1,1),
    vec2(0,0),
    vec2(1,0),
    vec2(0,1)
    }
    walls.vertices={
    verts[1],verts[5],verts[3],
    verts[3],verts[7],verts[5],
    
    verts[3],verts[7],verts[4],
    verts[4],verts[8],verts[7],
    
    verts[2],verts[6],verts[4],
    verts[4],verts[8],verts[6],
    
    verts[1],verts[5],verts[2],
    verts[2],verts[6],verts[5] 
    }
    walls.texCoords={
    tex[3],tex[1],tex[2],
    tex[2],tex[4],tex[1],
    
    tex[3],tex[1],tex[2],
    tex[2],tex[4],tex[1],
    
    tex[3],tex[1],tex[2],
    tex[2],tex[4],tex[1],
    
    tex[3],tex[1],tex[2],
    tex[2],tex[4],tex[1]
    }
    walls.texture="Cargo Bot:Game Area"
    walls:setColors(255,255,255)
    floor=mesh()
    floor.vertices={
    verts[1],verts[2],verts[4],
    verts[1],verts[3],verts[4]
    }
    floor.texCoords={
    tex[1],tex[3],tex[2],
    tex[1],tex[4],tex[2]
    }
    floor.texture="Documents:floor"
    look=vec3(0,1,0)
    pos=vec2(0,0) 
    angle=vec2(0,1)
    touches={}
    b=Button(50,50,50,50)
    acc=vec2(0,0)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    b:draw()
    pushMatrix()
    pos=pos-acc
    look.x,look.z=math.sin(math.rad(angle.x))*1,-math.cos(math.rad(angle.x))*1
    look.y=math.sin(math.rad(angle.y))*10
    perspective(45,WIDTH/HEIGHT)
    camera(pos.x,1,pos.y,look.x,look.y,look.z,0,1,0)
    walls:draw()
    floor:draw()
    popMatrix()
    for i,v in pairs(touches) do
        if v.x<b.x or v.x>b.x+b.w or v.x<b.y or v.x>b.y+b.h then
            angle.x=angle.x+v.deltaX/100
            angle.y=angle.y+v.deltaY/100
        else acc=(vec2(pos.x,pos.z)-vec2(look.x,look.z)):normalize()
        end
    end
end

function touched(t)
    if t.state~=ENDED and t.state~=CANCELLED then
        touches[t.id]=t
    else
        touches[t.id]=nil
        acc=vec2(0,0)
    end
end

For some reason, it is not working, for movement seems to move in a random direction and whenever I do move a little bit, the direction one is looking in also gets messed up. Does anyone know how to fix this? Thank you!
-TheSolderKing

impossibleto test your code, some class is missing (Button)

I suggest when you work in 3D, that you start with as little code as possible, then add a few lines at a time, testing as you go.

I would debug by commenting out as much code as possible, to isolate the problem. Right now, there is too much going on.

Sorry @Jmv38, I will fix that.
@Ignatz, I found the problem, still have no idea what happened…but anyways, here is the code afterwards, with button class.


--# Button
Button = class()

function Button:init(x,y,w,h)
    -- you can accept and set parameters here
    self.x = x
    self.y = y
    self.w = w
    self.h = h
end

function Button:draw()
    -- Codea does not automatically call this method
    fill(198, 198, 198, 255)
    rect(self.x, self.y, self.w, self.h)
end

function Button:touched(touch)
    -- Codea does not automatically call this method
end

--# Main
-- Into the Thid Dimension

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    walls=mesh()
    verts={
    vec3(25,0,25),
    vec3(25,0,-25),
    vec3(-25,0,25),
    vec3(-25,0,-25),
    vec3(25,25,25),
    vec3(25,25,-25),
    vec3(-25,25,25),
    vec3(-25,25,-25)
    }
    tex={
    vec2(1,1),
    vec2(0,0),
    vec2(1,0),
    vec2(0,1)
    }
    walls.vertices={
    verts[1],verts[5],verts[3],
    verts[3],verts[7],verts[5],
    
    verts[3],verts[7],verts[4],
    verts[4],verts[8],verts[7],
    
    verts[2],verts[6],verts[4],
    verts[4],verts[8],verts[6],
    
    verts[1],verts[5],verts[2],
    verts[2],verts[6],verts[5] 
    }
    walls.texCoords={
    tex[3],tex[1],tex[2],
    tex[2],tex[4],tex[1],
    
    tex[3],tex[1],tex[2],
    tex[2],tex[4],tex[1],
    
    tex[3],tex[1],tex[2],
    tex[2],tex[4],tex[1],
    
    tex[3],tex[1],tex[2],
    tex[2],tex[4],tex[1]
    }
    walls.texture="Cargo Bot:Game Area"
    walls:setColors(255,255,0)
    floor=mesh()
    floor.vertices={
    verts[1],verts[2],verts[4],
    verts[1],verts[3],verts[4]
    }
    floor.texCoords={
    tex[1],tex[3],tex[2],
    tex[1],tex[4],tex[2]
    }
    floor.texture="Documents:floor"
    look=vec3(0,1,0)
    pos=vec2(0,0) 
    angle=vec2(0,1)
    touches={}
    b=Button(50,50,50,50)
    acc=vec2(0,0)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 0, 0, 255)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    b:draw()
    pushMatrix()
    pos=pos-acc
    look.x,look.z=math.sin(math.rad(angle.x))*1000,-math.cos(math.rad(angle.x))*1000
    look.y=math.sin(math.rad(angle.y))*1000
    perspective(45,WIDTH/HEIGHT)
    camera(pos.x,1,pos.y,look.x,look.y,look.z,0,1,0)
    walls:draw()
    floor:draw()
    popMatrix()
    for i,v in pairs(touches) do
        if v.x>=b.x and v.x<=b.x+b.w and v.y>=b.y and v.y<=b.y+b.h then
            acc=(vec2(pos.x,pos.z)-vec2(look.x,look.z)):normalize()
        else 
            angle.x=angle.x+CurrentTouch.deltaX/5
            angle.y=angle.y+CurrentTouch.deltaY/3
        end
    end
end

function touched(t)
    if t.state~=ENDED and t.state~=CANCELLED then
        touches[t.id]=t
    else
        touches[t.id]=nil
        acc=vec2(0,0)
    end
end

This code seems to work pretty well now?

Yes it works great…strange. I don’t believe I changed anything :stuck_out_tongue: