Hi,

I’ve been using Ignatz’s excellent guides to help me get a basic understanding of 3D graphics, and have made some small progress on my project. One issue among the many that currently perplex me is detecting which object is “under” the touch. I’ve adapted the code from https://coolcodea.wordpress.com/2013/05/30/70a-touching-an-object-on-a-3d-screen/ and https://coolcodea.wordpress.com/2013/05/19/55-3d-creating-4-connected-walls/ in the code below.

The issue I get is that the foremost rectangle in my mesh isn’t always the one returned by the check. It works sometimes, but not others. If I record the video of this code in use, I can see the transparent walls of the cube that the check misses. I suspect I’m making a fundamental mistake, any help would be appreciated in getting me past this problem.

Thanks,

Time Trial.

```
-- 3D Test
-- Use this function to perform your initial setup
function setup()
parameter.integer('cameraY',-1000,1000,0)
parameter.number('Rotation',-2*math.pi,2*math.pi,0)
tapped=false
tapX,tapY=0,0
r,g,b=0,0,0
tapImg=image(WIDTH,HEIGHT)
meshTable={}
AddFourWalls(0,0,0)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
if tapped==true then
setContext(tapImg)
clip(tapX-1,tapY-1,2,2)
end
background(40, 40, 50)
perspective(45,WIDTH/HEIGHT)
cameraX=math.sin(Rotation)*400
cameraZ=(math.cos(Rotation)*400)
camera(cameraX,cameraY,cameraZ,0,0,0,0,1,0)
for i,m in pairs(meshTable) do
pushMatrix()
translate(m.pos.x,m.pos.y,-m.pos.z)
rotate(-m.ang,0,1,0)
m:draw()
popMatrix()
end
if tapped==true then
r,g,b=tapImg:get(tapX,tapY)
print('Image ' .. r/20 .. ' tapped (r=' .. r .. ')')
setContext()
clip()
tapped=false
end
end
function AddFourWalls(x,y,z)
local w,h=100,100
local xx,yy,zz=x-w/2,y-h/2,z-w/2
AddImage(x,y,zz,w,h,CreateImage(w,h,1),0)
AddImage(xx,y,z,w,h,CreateImage(w,h,2),90)
AddImage(x,y,zz+w,w,h,CreateImage(w,h,3),0)
AddImage(xx+w,y,z,w,h,CreateImage(w,h,4),90)
end
function CreateImage(pw,ph,px)
local ci=image(pw,ph)
setContext(ci)
rectMode(CORNER)
fill(px*20,0,0)
rect(-1,-1,pw+2,ph+2)
fill(255)
fontSize(14)
text(px,10,10)
setContext()
return ci
end
function AddImage(x,y,z,w,h,i,a)
local m=mesh()
m.texture=i
local v,t={},{}
local xx,yy,zz=-w/2,-h/2,0
v[#v+1]=vec3(xx,yy,zz) t[#t+1]=vec2(0,0)
v[#v+1]=vec3(xx+w,yy,zz) t[#t+1]=vec2(1,0)
v[#v+1]=vec3(xx+w,yy+h,zz) t[#t+1]=vec2(1,1)
v[#v+1]=vec3(xx+w,yy+h,zz) t[#t+1]=vec2(1,1)
v[#v+1]=vec3(xx,yy+h,zz) t[#t+1]=vec2(0,1)
v[#v+1]=vec3(xx,yy,zz) t[#t+1]=vec2(0,0)
m:setColors(color(255))
m.vertices=v
m.texCoords=t
m.pos=vec3(x,y,z) --store x,y,z
m.ang=a --store angle
table.insert(meshTable,m)
end
function touched(touch)
if touch.tapCount == 2 and touch.state == ENDED then
tapped=true
tapX=CurrentTouch.x
tapY=CurrentTouch.y
else
tapped=false
end
end
```