I’ve done some slight revisions to the 3D FPS Movement Demo project I previously posted. Instead of using the OrbitViewer class from @John ’s “Cameras” Codea dependency, I switched to the FirstPersonViewer class in the same dependency. I the touch screen camera rotation in the FirstPersonViewer class combined with a simple cluster of keyboard inputs to move the camera in 3D space accomplishes more of what I was looking for in terms of 3D First Person Shooter type movement in my Codea projects.
I’ve posted the revised code below and have additionally attached a Zip file of the project’s code and attached the project’s texture assets. As I mentioned in my original Simple 3D FPS Movement Demo post, this forum does not allow me to attach the .obj model so you will either need to download the free model from:
https://www.turbosquid.com/3d-models/old-house-max-free/1117878 (choose .obj format to download) or substitute your own model as an asset (if you do so, remember to change asset.documents statements in the code to point to your assets).
-- 3D FPS demoR (Revised)
function setup()
viewer.mode = FULLSCREEN
-- need to enable keyboard with below command to get input (even if use external keyboard)
showKeyboard()
-- Create a new craft scene
scene = craft.scene()
-- skybox (developers special function for-- really a box around scene painted inside with a cross map texture)
-- *Note: grimmnight texture free download (per download site to be credited to "Hipshot" at www.zfight.com)
scene.sky.material.envMap = craft.cubeTexture(asset.documents.grimmnight_large)
-- Create a ground entity from primitives
-- *Note: "forest-floor-terrain" free download from texturemax.com
Ground = scene:entity()
Ground.model = craft.model.cube(vec3(1.2,0.01,1.2))
Ground.material = craft.material(asset.builtin.Materials.Basic)
Ground.material.map = readImage(asset.documents["forest-floor-terrain.jpg"])
Ground.y = 0
-- import 3D model of house(.obj format) from document folder in Codea
-- *Note: "old_house" model and "wood" texture are free downloads from TurboSquid website
House = scene:entity()
House.model = craft.model(asset.documents.old_house)
House.material = craft.material(asset.builtin.Materials.Basic)
House.material.map = readImage(asset.documents.wood_D_png)
-- *Next line rotates the house model 180 degrees around y axis so will later line up nicely with backdrop moon in sky
House.eulerAngles = vec3(0, 180, 0)
House.scale = vec3(0.005,0.005,0.005)
-- Link "Cameras" dependency to this project; dependency contains FirstPersonViewer class that you can add to scene camera to rotate camera view by touching the screen
-- Note: it helps to assign FirstPersonViewer camera to a variable so that can set its properties (e.g. v = scene.camera:add(FirstPersonViewer))
v = scene.camera:add(FirstPersonViewer)
-- *Next line rotates the camera 180 around y axis so when program runs camera points at front of cabin with moon in the sky as a backdrop and at an arbitrary + 4 z axis units in front of the cabin
-- Note that v.ry defines camera rotation view while v.camera.z sets actual z coordinates of the camera in 3D space
v.ry= 180
v.camera.z = 4
function update(dt)
-- Update the scene (physics, transforms etc)
scene:update(dt)
end
function draw()
--update then draw 3D "scene"
update(DeltaTime)
scene:draw()
end
function keyboard(key)
-- this function detects keyboard input and adds to "Cameras" dependency the ability (without rotating the camera's view) to move the first person camera (FirstPersonViewer) stepwise in 3D space (arbitrarily 0.25 units)
-- (i/j/k/l/u/m = fwd (+z)/left (-x)/back (-z)/right (+x)/up (+y)/down (-y))
-- control speed of movement in x,y,z space by changing constant value to add to viewer position (v) when move
-- *Note: use original FirstPersonViewer function of touching the screen to rotate in place the camera's view
if key == "i" then v.camera.z = v.camera.z -0.25
elseif key == "j" then v.camera.x = v.camera.x - .25
elseif key == "k" then v.camera.z = v.camera.z + 0.25
elseif key == "l" then v.camera.x = v.camera.x + .25
elseif key == "u" then v.camera.y = v.camera.y + .25
elseif key == "m" then v.camera.y = v.camera.y - .25
end
end
end
Have fun!