How's to walk forward after turning?

I made a spin off of code I found here and I am trying to make it 1st person but, I can’t figure out how to move forward after turning, it always bases it on the 0.00 turn axis
Any help would be appreciated

-- Use this function to perform your initial setup
displayMode(FULLSCREEN)
function setup()
    stick = Stick()
    parameter.number("look",-360,360,0, function() AdjustCamLook(look) end)
    pylon = Wall("Cargo Bot:Crate Red 2")
    wall = Wall("Cargo Bot:Crate Yellow 2")
    floor = Floor("Cargo Bot:Crate Green 2")

    world = World()
    hero = Hero(3,3)

    
end
function AdjustCamLook(r)
    r=math.rad(r)
    camLook=vec3(math.sin(r),0,math.cos(r))
end
-- This function gets called once every frame
function draw()
    background(0)
    local TO_DEG = TO_DEG
    local hero = hero
    perspective()
    camera(hero.x, 0, hero.z, hero.x+(camLook.x), 0, hero.z+camLook.z, 0, 1, 0)
-- Draw world
    pushMatrix()
    world:draw()
    popMatrix()

-- Draw hero

-- Restore orthographic projection
    ortho()
    viewMatrix(matrix())
    resetMatrix()

    -- fade out overlay
    sprite("Cargo Bot:Background Fade", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)

    if stick.active then
        local ceil = math.ceil
        stick:draw()
        local mvtx = (-math.cos(stick.direction))/50*stick.dist
        local mvtz = (math.sin(stick.direction))/50*stick.dist
        hero.x = hero.x + mvtx
        hero.z = hero.z + mvtz

        -- convert to table coordinates
        hero.px = ceil(hero.x - .5)
        hero.py = ceil(hero.z - .5)

        -- lazy collision check
        if world.data[hero.py][hero.px] ~= 0 then
            hero.x = hero.x - mvtx
            hero.z = hero.z - mvtz
            hero.px = ceil(hero.x - .5)
            hero.py = ceil(hero.z - .5)
        end
    end
end

function touched(touch)
    stick:touched(touch)
end

--# World
World = class()

function World:init()

    -- define the world
    self.data =
    {
        {1, 1, 1, 1, 1, 1, 1, 1},
        {1, 2, 0, 0, 0, 0, 2, 1},
        {1, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 1},
        {1, 2, 0, 0, 0, 0, 2, 1},
        {1, 1, 1, 1, 1, 1, 1, 1}
    }
end

function World:draw()
    local floor, wall, pylon = floor, wall, pylon
    local offSet = 5
    local px, py = hero.px, hero.py

    -- look around the hero to draw whatever is around him
    translate(px - offSet, 0, py - offSet)
    for y = py - offSet, py + offSet do
        for x = px - offSet, px + offSet do
            if self.data[y] then
                local val = self.data[y][x]
                if val == 0 then
                    floor:draw()
                elseif val == 1 then
                    wall:draw()
                elseif val == 2 then
                    pylon:draw()
                end
            end
            translate(1,0,0)
        end
        translate(-(1 + 2 * offSet), 0, 1)
    end
end


--# Hero
Hero = class()

function Hero:init(x, z)
    self.x, self.y, self.z = x,0,z
    self.px, self.py = math.ceil(.5+x), math.ceil(.5+z)
end



--# Wall
Wall = class()


function Wall:init(tex)
    -- all the unique vertices that make up a cube
    local vertices =
    {
        vec3(-0.5, -0.5,  0.5), -- Left  bottom front
        vec3( 0.5, -0.5,  0.5), -- Right bottom front
        vec3( 0.5,  0.5,  0.5), -- Right top    front
        vec3(-0.5,  0.5,  0.5), -- Left  top    front
        vec3(-0.5, -0.5, -0.5), -- Left  bottom back
        vec3( 0.5, -0.5, -0.5), -- Right bottom back
        vec3( 0.5,  0.5, -0.5), -- Right top    back
        vec3(-0.5,  0.5, -0.5), -- Left  top    back
    }

    -- now construct a cube out of the vertices above
    local verts =
    {
        -- Front
        vertices[1], vertices[2], vertices[3],
        vertices[1], vertices[3], vertices[4],
        -- Right
        vertices[2], vertices[6], vertices[7],
        vertices[2], vertices[7], vertices[3],
        -- Back
        vertices[6], vertices[5], vertices[8],
        vertices[6], vertices[8], vertices[7],
        -- Left
        vertices[5], vertices[1], vertices[4],
        vertices[5], vertices[4], vertices[8],
        -- Top
        vertices[4], vertices[3], vertices[7],
        vertices[4], vertices[7], vertices[8],
       -- Bottom
        vertices[5], vertices[6], vertices[2],
        vertices[5], vertices[2], vertices[1],
    }

    -- all the unique texture positions needed
    local texvertices =
    {
        vec2(0,0),
        vec2(1,0),
        vec2(0,1),
        vec2(1,1)
    }

    -- apply the texture coordinates to each triangle
    local texCoords =
    {
        -- Front
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
        -- Right
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
        -- Back
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
        -- Left
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
        -- Top
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
        -- Bottom
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
    }

    self.model = mesh()
    self.model.vertices = verts
    self.model.texture = tex
    self.model.texCoords = texCoords
    self.model:setColors(255,255,255,255)
end

function Wall:draw()
    self.model:draw()
end
--# Floor
Floor = class()

function Floor:init(tex)
    -- all the unique vertices that make up a cube
    local vertices =
    {
        vec3( 0.5,  -0.5,  0.5), -- Right top    front
        vec3(-0.5,  -0.5,  0.5), -- Left  top    front
        vec3( 0.5,  -0.5, -0.5), -- Right top    back
        vec3(-0.5,  -0.5, -0.5), -- Left  top    back
    }


    -- now construct a cube out of the vertices above
    local verts =
    {
        -- Bottom
        vertices[3], vertices[4], vertices[2],
        vertices[3], vertices[2], vertices[1],
    }

    -- all the unique texture positions needed
    local texvertices =
    {
        vec2(0,0),
        vec2(1,0),
        vec2(0,1),
        vec2(1,1)
    }

    -- apply the texture coordinates to each triangle
    local texCoords =
    {
        -- Bottom
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
    }

    self.model = mesh()
    self.model.vertices = verts
    self.model.texture = tex
    self.model.texCoords = texCoords
    self.model:setColors(255,255,255,255)
end

function Floor:draw()
    self.model:draw()
end

--# Stick
Stick = class()

function Stick:init()
    self.direction = 120
    self.dist = 0

    self.active = false
    self.origin = vec2(150, 150)
    self.center = self.origin
    self.pos = self.origin

    self.stick_bg = readImage("Space Art:Eclipse")
    self.stick = readImage("Space Art:UFO")

end

function Stick:draw()
    sprite(self.stick_bg, self.center.x, self.center.y)
    sprite(self.stick, self.pos.x, self.pos.y)
end

function Stick:touched(touch)
    if touch.state == BEGAN then
        self.center = vec2(touch.x, touch.y)
        self.active = true
    end

    self.pos = vec2(touch.x+camLook.x, touch.y)
    self.direction = math.atan2((self.pos.y- self.center.y), (self.pos.x - self.center.x))
    self.dist = math.min(2, self.pos:dist(self.center)/32)

    if touch.state == ENDED then
        self.center = self.origin
        self.pos = self.center
        self.active = false
    end


end

@dmoeller Whenever you post code, put ~~~ on a line before and after your code so it formats properly. I added them to your code above. As for moving forward, I’ll look at it later when I have more time.

Thank you, this is my first post

@dmoeller Instead of my changing your code, here’s some stripped down code that I have that moves you around in 3D space. You can look thru this and see how you can modify your code. This code creates 500 cubes in a -400 to 400 cubed area. Holding your finger or thumb on the screen above the white line allows you to move in the direction you’re looking. Lifting your finger stops the movement. Move your other finger in the screen area below the white line to change the direction you’re looking/moving. The red cube is the center of all the cubes. The text at the top of the screen shows your x,y,z position and the horizontal and vertical angle that your moving. Since your code is on a surface, the vertical angle will always be 0.

Removed code. Updated code is below.

But how do you make it so the tapping the right goes forward backwards and side to side while, tapping the left makes you look around

Sorry for so many questions

@dmoeller I modified the above code. See the new comments.

Thank you, I’m editing it right now, will come back if I have more questions

@dmoeller I modified the code again. See the new comments above.

This is kind of what I want to do, but I can’t walk backwards or look up

displayMode(FULLSCREEN)   

function setup()
    cameraX,cameraZ=0,0       -- camera position
    lookX,lookZ=0,2000        -- look at position
    angH=0                    -- horizontal angle
    speed=.1                 -- speed
    x,z=0,0                   -- amount of movement in x,z
    Cubes=Cube()
    xstop = 20
    zstop = 20 
    look = false   
end

function draw()
    background(0)  
    fill(255)
    
    text(1/DeltaTime//1,WIDTH/2,HEIGHT-25)
    perspective()
    camera(cameraX,0,cameraZ,lookX,0,lookZ,0,1,0)
    Cubes:draw()     
end

function touched(t)
    if t.state==BEGAN and t.x<WIDTH/2 then
        moving=true -- allow moving
        look= false 
    elseif t.state==MOVING then
        angH=(angH-(t.deltaX))%360
    elseif t.state==ENDED then
        moving=false
        look=true    -- stop moving
    end
end

Cube = class()

function Cube:init()
    local vertices = {vec3(-0.5, -0.5,  0.5),vec3( 0.5, -0.5,  0.5),
        vec3( 0.5,  0.5,  0.5),vec3(-0.5,  0.5,  0.5),vec3(-0.5, -0.5, -0.5),
        vec3( 0.5, -0.5, -0.5),vec3( 0.5,  0.5, -0.5),vec3(-0.5,  0.5, -0.5) }
    local cubeverts = { vertices[1], vertices[2], vertices[3],    
      vertices[1], vertices[3], vertices[4],vertices[2], vertices[6], vertices[7],
      vertices[2], vertices[7], vertices[3],vertices[6], vertices[5], vertices[8],
      vertices[6], vertices[8], vertices[7],vertices[5], vertices[1], vertices[4],
      vertices[5], vertices[4], vertices[8],vertices[4], vertices[3], vertices[7],
      vertices[4], vertices[7], vertices[8],vertices[5], vertices[6], vertices[2],
      vertices[5], vertices[2], vertices[1] }
    local texverts = { vec2(0.03,0.24),vec2(0.97,0.24),
            vec2(0.03,0.69),vec2(0.97,0.69) }                
    local cubetexCoords = {
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3] } 

    self.m=mesh()
    self.m.vertices = cubeverts
    self.m.texture = "Planet Cute:Stone Block"
    self.m.texCoords = cubetexCoords    
    self.cubesTab={}    -- table for cubes
    self.cubesTab1={}   -- table for barrier cubes
    -- create a 40x40 cube floor
    for x=-20,20 do
        for z=-20,20 do
            table.insert(self.cubesTab,vec3(x,-1,z))
        end
    end
    -- create 100 random cubes on the floor
    for a=1,100 do
        x=math.random(-20,20)
        z=math.random(-20,20)
        if x~=0 and y~=0 then
            table.insert(self.cubesTab1,vec3(x,0,z))
        end
    end
end

function Cube:draw()
    x=speed*math.sin(math.rad(angH))
    z=speed*math.cos(math.rad(angH)) 
    if look and CurrentTouch.state == MOVING then
        lookX=cameraX+2000*x
        lookZ=cameraZ+2000*z
        xstop = lookX
        zstop = lookZ
    else
        lookX=cameraX + xstop
        lookZ=cameraZ+ zstop
    end
    if moving then    -- move camera in a certain x,z direction
        hx=cameraX
        hz=cameraZ
        cameraX=cameraX+x
        cameraZ=cameraZ+z 
    end
    for a,b in pairs(self.cubesTab) do
        resetMatrix()
        translate(b.x,b.y,b.z) 
        self.m:draw()             
    end  
    for a,b in pairs(self.cubesTab1) do
        if cameraX>b.x-.6 and cameraX<b.x+.6 and 
                    cameraZ>b.z-.6 and cameraZ<b.z+.6 then
            cameraX=hx
            cameraZ=hz
        end
        resetMatrix()
        translate(b.x,b.y,b.z) 
        self.m:draw()             
    end  
end

Yes, but how would you do this with two gamestick controls?

Are you going to have 2 different objects or one control for movement and another control for direction.

I cant figure out how to do that could you help?

@dmoeller I changed my above code. This might be more usable for you. You should be able to add your WORLD to this code. To move, just touch the screen and move your finger in the direction you want to go.

EDIT: Added code so if your touch starts on the left half of the screen, you turn and move. If your touch starts on the right half of the screen, you just turn. Also added code so you can’t move thru a cube.

EDIT: Changed code again. Movement and rotation is done with one finger. To move forward, touch screen and slide your finger up a little. To move left, slide your finger left a little. To move right, slide your finger right a little. To stop slide your finger down. To just rotate right or left, slide your finger down and right or down and left. The FPS is also faster to allow a larger world and more cubes. If you’re moving and you lift your finger, motion continues.

EDIT: Changed the code again. You can now backup. I also removed the limits on the speed and rotation. The movement control is a little different because of the reverse direction.

displayMode(FULLSCREEN)   

function setup()
    parameter.integer("rx",-180,180)
    parameter.integer("ry",-180,180,0)
    size=100    -- size of the world (size x size)
    nbrCubes=240
    cameraX,cameraZ=0,0       -- starting camera position  0,0
    lookX,lookZ=0,2000        -- look at position
    angH=0                    -- horizontal angle
    speed=.00001              -- speed
    x,z=0,0                   -- amount of movement in x,z
    Cubes=Cube() 
    m=mesh()
    m:addRect(0,0,size+1,size+1)
    m.texture="Blocks:Redsand"
    ang=0
end

function draw()
    background(0)    
    fill(255)
    text(1/DeltaTime//1,WIDTH/2,HEIGHT-25)
    perspective()
    camera(cameraX,0,cameraZ,lookX,0,lookZ,0,1,0)
    Cubes:draw() 
    translate(0,-.5,0)
    rotate(90,1,0,0)
    m:draw() 
    angH=(angH-ang)%360
end

function touched(t)
    if t.state==BEGAN then 
        sx=t.x 
        sy=t.y
    elseif t.state==MOVING then
        speed=(t.y-sy)/100
        ang=ang+t.deltaX/100
    end
end

Cube = class()

function Cube:init()
    local vertices = {vec3(-0.5, -0.5,  0.5),vec3( 0.5, -0.5,  0.5),
        vec3( 0.5,  0.5,  0.5),vec3(-0.5,  0.5,  0.5),vec3(-0.5, -0.5, -0.5),
        vec3( 0.5, -0.5, -0.5),vec3( 0.5,  0.5, -0.5),vec3(-0.5,  0.5, -0.5) }
    local cubeverts = { vertices[1], vertices[2], vertices[3],    
      vertices[1], vertices[3], vertices[4],vertices[2], vertices[6], vertices[7],
      vertices[2], vertices[7], vertices[3],vertices[6], vertices[5], vertices[8],
      vertices[6], vertices[8], vertices[7],vertices[5], vertices[1], vertices[4],
      vertices[5], vertices[4], vertices[8],vertices[4], vertices[3], vertices[7],
      vertices[4], vertices[7], vertices[8],vertices[5], vertices[6], vertices[2],
      vertices[5], vertices[2], vertices[1] }
    local texverts = { vec2(0.03,0.24),vec2(0.97,0.24),
            vec2(0.03,0.69),vec2(0.97,0.69) }                
    local cubetexCoords = {
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3] } 

    self.m=mesh()
    self.m.vertices = cubeverts
    self.m.texture = "Planet Cute:Stone Block"
    self.m.texCoords = cubetexCoords    
    self.cubesTab={}    -- table for cubes
    
    for a=1,nbrCubes do
        x=math.random(-size//2,size//2)
        z=math.random(-size//2,size//2)
        if x~=0 and z~=0 then   -- no cube at 0,0 
            table.insert(self.cubesTab,vec3(x,0,z))
        end
    end
end

function Cube:draw()
    if speed==0 then
        speed=.0001
    end
    x=speed*math.sin(math.rad(angH))
    z=speed*math.cos(math.rad(angH)) 
    if speed<0 then
        lookX=cameraX+2000*-x
        lookZ=cameraZ+2000*-z
    else
        lookX=cameraX+2000*x
        lookZ=cameraZ+2000*z
    end
    hx=cameraX
    hz=cameraZ
    cameraX=cameraX+x
    cameraZ=cameraZ+z 
    for a,b in pairs(self.cubesTab) do
        if cameraX>b.x-.6 and cameraX<b.x+.6 and 
                    cameraZ>b.z-.6 and cameraZ<b.z+.6 then
            cameraX=hx
            cameraZ=hz
        end
        pushMatrix()
        translate(b.x,b.y,b.z) 
        self.m:draw()  
        popMatrix()           
    end  
end

@dave1707

@dmoeller I change my code above to allow reverse. See the new comment.

Thank you so much this is my code now. I can find out why it glitches when I turn slowly.

function setup()
    cameraX,cameraZ=0,0       -- camera position
    lookX,lookZ=0,2000        -- look at position
    angH=0                   -- horizontal angle
    nbrCubes = 240
    size=100
    speed=.1                 -- speed
    x,z=0,0                   -- amount of movement in x,z
    Cubes=Cube()
    m = mesh()
    m:addRect(0,0,size+1,size+1)
    img=readImage("Blocks:Redsand")
    m.texture=img
    xstop = 20
    zstop = 20 
    look = false   
    backwards = 1
end

function draw()
    background(0)  
    fill(255, 255, 255, 255)
    text(1/DeltaTime//1,WIDTH/2,HEIGHT-25)
    perspective()
    camera(cameraX,0,cameraZ,lookX,0,lookZ,0,1,0)
    Cubes:draw()
    translate(0,-.5,0)
    rotate(90,1,0,0)
    m:draw()    
end

function touched(t)
    if t.state==BEGAN and t.x<WIDTH/2 then
        moving=true -- allow moving
        look= false 
        sy=t.y
    elseif t.state==MOVING then
        angH=angH-(t.deltaX)%360
        speed=(t.y-sy)/500
        if speed >.1 then
            speed = .1
        elseif speed <-.1 then
            speed = -.1
        end
    elseif t.state==ENDED then
        moving=false
        look=true    -- stop moving
    end
end

Cube = class()

function Cube:init()
    local vertices = {vec3(-0.5, -0.5,  0.5),vec3( 0.5, -0.5,  0.5),
        vec3( 0.5,  0.5,  0.5),vec3(-0.5,  0.5,  0.5),vec3(-0.5, -0.5, -0.5),
        vec3( 0.5, -0.5, -0.5),vec3( 0.5,  0.5, -0.5),vec3(-0.5,  0.5, -0.5) }
    local cubeverts = { vertices[1], vertices[2], vertices[3],    
      vertices[1], vertices[3], vertices[4],vertices[2], vertices[6], vertices[7],
      vertices[2], vertices[7], vertices[3],vertices[6], vertices[5], vertices[8],
      vertices[6], vertices[8], vertices[7],vertices[5], vertices[1], vertices[4],
      vertices[5], vertices[4], vertices[8],vertices[4], vertices[3], vertices[7],
      vertices[4], vertices[7], vertices[8],vertices[5], vertices[6], vertices[2],
      vertices[5], vertices[2], vertices[1] }
    local texverts = { vec2(0.03,0.24),vec2(0.97,0.24),
            vec2(0.03,0.69),vec2(0.97,0.69) }                
    local cubetexCoords = {
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3],
      texverts[1], texverts[2], texverts[4],texverts[1], texverts[4], texverts[3] } 

    self.m=mesh()
    self.m.vertices = cubeverts
    self.m.texture = "Planet Cute:Stone Block"
    self.m.texCoords = cubetexCoords    
    self.cubesTab={}    -- table for cubes

    for a=1,nbrCubes do
        x=math.random(-size//2,size//2)
        z=math.random(-size//2,size//2)
        if x~=0 and z~=0 then   -- no cube at 0,0 
            table.insert(self.cubesTab,vec3(x,0,z))
        end
    end
end

function Cube:draw()
    x=speed*math.sin(math.rad(angH))
    z=speed*math.cos(math.rad(angH))
    if look and CurrentTouch.state == MOVING then
        lookX=cameraX+2000*x
        lookZ=cameraZ+2000*z            
        xstop = lookX
        zstop = lookZ
        speed= math.abs(speed)
    else
        hx=cameraX
        hz=cameraZ
        lookX=cameraX + xstop
        lookZ=cameraZ+ zstop
    end
    if moving then  
        if speed < 0 then
            cameraX=cameraX-x
            cameraZ=cameraZ+z
        else
            cameraX=cameraX+x
            cameraZ=cameraZ+z
        end
    end
    for a,b in pairs(self.cubesTab) do
        if cameraX>b.x-.6 and cameraX<b.x+.6 and 
                    cameraZ>b.z-.6 and cameraZ<b.z+.6 then
            cameraX=hx
            cameraZ=hz
        end
        pushMatrix()
        translate(b.x,b.y,b.z) 
        self.m:draw()  
        popMatrix()           
    end              
end

I can’t find out why it glitches when I turn slowly could you help @dave1707 ?

@dmoeller It probably because you’re at a point where you’re looking forward, then backwards, then forwards, etc. Anyways, I’m not going to figure out why because I gave you an updated version above yesterday.