I’m wondering how I could go about checking whether touch input continuously follows a defined path or shape, especially a complete circle, in a 2D game. This could either be finger-based on a touch screen device, or a click-hold-drag action of a mouse button.
This action could be used for something like drawing on paper, turning a valve, rotating an object, casting a spell etc.
I understand the basics of touch but don’t know where to begin with something like this.
I have never coded this, but I will in a futur game idea (I want to make a game where Attack is like in Okami, you draw the action pattern on screen) so I want to think about it with you!
We have 2 problems to solve I guess:
Record continuous touch positions made by the user
Determine if this recorded path is similar enough to a predefine shape.
For the first, we can do it like that I guess (i am writing on my phone, just thinking about it, none of it has been tested…)
function init(self)
-- Acquire input focus so the script receives events
msg.post(".", "acquire_input_focus")
self.touch_active = false
self.touch_positions = {}
end
function on_input(self, action_id, action)
if action_id == hash("touch") then
if action.pressed then
self.touch_active = true
self.touch_positions = {} -- reset
table_insert( self.touch_positions, vmath.vector3(action.x, action.y, 0) )
elseif action.released then
self.touch_active = false
test_shape(self.touch_positions) -- where we solve problem 2...
elseif self.touch_active then
-- This captures the continuous movement while holding/dragging
table_insert(self.touch_positions , vmath.vector3(action.x, action.y, 0) )
end
end
end
For the problem to test shape similarity, I have to think about it more, that could be a tough problem to solve
And it will depend if you want one of these sitation:
A. there is a shape already drawn on screen to follow. So positions of shape point are absolute. Here you can compute distances from shape points to user points.
B. there is no drawn shape, user can start it wherever on the screen => so positions of shape point are relative, to (0,0), and we need to bring back the user shape positions to the same reference point (I guess compute the barycenter of the shape, then substract it to each positions). Then we are in situation A again.
I haven’t implemented it my self, but I’d look into Penny Pincher algorithm (and possible improvements upon that).
The Black & White game uses it iirc: Black & White Style Spell Gesture Recognition
EDIT: I also haven’t tried it but this asset mentions gesture support, and lists as supporting all platforms: Defold-Input