@yojimbo2000 Here’s some code I have, maybe you could use something like this.
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
-- non moving polygon
verts1={vec2(500,100),vec2(800,400),vec2(300,600),
vec2(100,400)}
-- moving polygon or point
verts2={vec2(700,200),vec2(720,260),vec2(750,260),vec2(800,230)} -- polygon
-- verts2={vec2(700,200)} -- point
tx=0
ty=0
end
function draw()
background(40, 40, 50)
stroke(255,0,0)
strokeWidth(4)
fill(255,0,0)
text("fan area",400,400)
local j=#verts1 -- draw 1st polygon at a fixed location
for z=1,#verts1 do
line(verts1[z].x,verts1[z].y,verts1[j].x,verts1[j].y)
j=z
end
stroke(255)
local j=#verts2 -- draw 2nd polygon moving with touch x,y
for z=1,#verts2 do
line(verts2[z].x+tx,verts2[z].y+ty,verts2[j].x+tx,verts2[j].y+ty)
j=z
end
check2() -- check if polygons are colliding
fill(255)
text(str,WIDTH/2,HEIGHT/2)
text("drag you finger around to move the smaller object",WIDTH/2,HEIGHT-100)
end
function check2() -- calculate x,y points for collision
local j=#verts2
if j==1 then
str=""
if check(tx+verts2[j].x,ty+verts2[j].y) then
str="collision"
end
return
end
for z=1,#verts2 do
local m=(verts2[j].y-verts2[z].y)/(verts2[j].x-verts2[z].x)
local b=verts2[z].y-m*verts2[z].x
local dir=1
if verts2[j].x>verts2[z].x then
dir=-1
end
str=""
for x=verts2[j].x,verts2[z].x,dir do
local y=m*x+b
if check(tx+x,ty+y) then
str="collision"
return
end
end
j=z
end
end
function check(px,py) -- check if points px,py are within polygon
local j=#verts1
local c=-1
for i=1,#verts1 do
local a1=false
if verts1[i].y>py then
a1=true
end
local a2=false
if verts1[j].y>py then
a2=true
end
local b1=verts1[j].x-verts1[i].x
local b2=py-verts1[i].y
local b3=(verts1[j].y-verts1[i].y)
if a1~=a2 and px<(b1*b2/b3+verts1[i].x) then
c = c *-1
end
j=i
end
if c==1 then
return true
else
return false
end
end
function touched(t) -- used just to move 2nd polygon around
tx=tx+t.deltaX
ty=ty+t.deltaY
end