Hi,
im new to this forum, and have played with Codea for only a few days now. Im trying to make a simple touch-controller class to decide the direction of movement in a game.
My approach is to divied the whole screen into four triangles. when the touch position is within the upper triangle, i set a variable “up” to true, similar for left, right and down…
To determine which triangle has been touched i decided to go with the physics api because of the nice collision checks.
However, the check works only when the game is freshly started. After a few seconds the collision checks start failing. Im already printing the triangles to check if they somehow move automagically, but everything seems ok.
It would be nice if someone can give any hint, thanks in advance.
here’s the code of the controller class:
Controller = class()
function Controller:init()
self.ptTopLeft = vec2(0, HEIGHT)
self.ptTopRight = vec2(WIDTH, HEIGHT)
self.ptBtLeft = vec2(0,0)
self.ptBtRight = vec2(WIDTH, 0)
self.ptCenter = vec2(WIDTH/2, HEIGHT/2)
self.areaTop = physics.body(POLYGON, unpack({self.ptTopLeft, self.ptTopRight, self.ptCenter}))
self.areaBottom = physics.body(POLYGON, unpack({self.ptBtLeft, self.ptBtRight, self.ptCenter}))
self.areaLeft = physics.body(POLYGON, unpack({self.ptBtLeft, self.ptTopLeft, self.ptCenter}))
self.areaRight = physics.body(POLYGON, unpack({self.ptBtRight, self.ptTopRight, self.ptCenter}))
self.left = false
self.right = false
self.up = false
self.down = false
--local poly = physics.body(POLYGON, unpack(points))
end
function Controller:touched(touch)
local touchVec
touchVec = vec2(touch.x, touch.y)
print(touch.state)
if(touch.state == ENDED) then
self.up = false
self.down = false
self.left = false
self.right = false
elseif (self.areaTop:testPoint(touchVec) == true) then
self.up = true
elseif (self.areaBottom:testPoint(touchVec) == true) then
self.down = true
elseif (self.areaLeft:testPoint(touchVec) == true) then
self.left = true
elseif (self.areaRight:testPoint(touchVec) == true) then
self.right = true
end
end
function Controller:draw()
drawBody(self.areaRight)
drawBody(self.areaLeft)
end
function drawBody(body)
pushStyle()
stroke(255, 0, 0, 255)
strokeWidth(7)
local points = body.points
for j = 1,#points do
a = points[j]
b = points[(j % #points)+1]
line(a.x, a.y, b.x, b.y)
end
popStyle()
end