MV Detect Drawn Shapes?

Hello there, fellow game devs, I was wondering if there might be a plugin (or a way to code a plugin) for a specific minigame I have in mind.

First off, have any of you played the 2020 Halloween Google Doodle? Or maybe that game in the Google Santa's Workshop where you draw things and a robot guesses what they are? That is kind of what I have in mind for this minigame. You draw a certain shape, and the game tries to guess what it is, and if the game guesses it in time, then you win the minigame.

So, what I need is some sort of plugin (or plugin coding knowledge) to figure out how to have the players draw, and have the game try and recognize their shapes and drawings. I hope that this is actually possible, and that you all can help me with it, like you all have done so many times before. Thank you!
Oh neat! I vaguely remember playing the previous one, this sequel looks fun~ 😂

Oh, uh, yeah, recognising shapes from user input, right. 😅

Implementing shape recognition in general is a Hard Problem (technical term), though someone might happen to know of a public code library (image similarity?) that can do the heavy lifting there.

It becomes significantly easier if you know which shapes to check ahead of time. Let's take the doodle's shapes as an example:
  • Straight lines (—, |)
  • V shapes (V, Λ)
  • Thunderbolt (ϟ)
  • Circle (o)
  • Cardioid (♡)
  • Spiral (couldn't find a unicode spiral 🙁)
The input will be a sample of points taken while the user is drawing. So what needs to happen is:
  1. Store input coordinates while drawing;
  2. Process input and compare to expected shapes;
  3. Return matched shape, if any.
Let's focus on the difficult one, point 2. I'd suggest approximating the input as a set of straight lines and working from there. Consider a set of points starting at (a, b) and ending at (c, d). Each consecutive pair of points can be connected by a straight line with a particular gradient. This data can then be used to determine the kind of shape drawn, e.g.
  • A horizontal straight line should have roughly equal values for b and d (start and end Y coordinates) and approximately-zero gradient (0 or 180 degrees to the +x axis) for all its line segments.

  • A V shape should have a straight-line shape at, say, approx. -60 degrees from +x, after which it should change to a +60 degree straight-line. Maybe the start/end Y coordinates should also be roughly equal, depends on the implementation.

  • A thunderbolt is basically a tilted V shape with another bend in it~

  • A circle should have roughly equal changes in segment gradient throughout, and approximately equal start and end points.
    [Edit @Alador: on reflection, this approach may be better for checking a circle:
    1. Get the minimum bounding rectangle for the input points;
    2. Use that to determine the shape's centre point;
    3. Check each input point's distance from the centre;
    4. All points of a circle should be approximately equally-distant from the centre.
    😮‍💨]


  • Other curved shapes like cardioid and spiral are more tricky, depends what you're going for there.
When I say "roughly" or "approximately" here, that's up to the implementation. Some kind of margin is essential, otherwise players would have to draw perfect straight lines etc, but too much could cause false positives. A minimum width/height (bounding box of all input points) will probably also be necessary to prevent exploits. 😴
@caethyril, thank you so much for that beautiful explanation. But, there is still one thing we have yet to figure out: How does the user draw on the game in order for the game to detect it?
👋 I was thinking you could just check the touch input status each frame:
  • If touch input received, add new input coordinates to an array of drawing data;
  • If touch input not received and there's drawing data to process, then process it.
"Check each frame" typically translates to an update method extension, and the core scripts provide easy handlers for touch input. It might look something like this (untested):
Spoiler: Concept code (map scene)
JavaScript:
(function() {
'use strict';
  let drawPts = [];   // array of logged points

  var logDrawing = function() {
    drawPts.push({ x: TouchInput.x, y: TouchInput.y });
  };

  var endDrawing = function() {
    if (drawPts.length) {
      checkShape();
      drawPts = [];
    }
  };

  var checkShape = function() {
    console.info('TODO: process shape recognition');
  };

  var _Scene_Map_updateMain = Scene_Map.prototype.updateMain;
  Scene_Map.prototype.updateMain = function() {
    _Scene_Map_updateMain.apply(this, arguments);
    if (TouchInput.isPressed()) logDrawing();
    else endDrawing();
  };
})();
If you're talking about rendering the drawing on-screen, one approach would be to just draw dots (or particles?) at each point when it gets logged. Alternatively (or additionally), lines could be drawn connecting each point. I know there are canvas methods for these, e.g. arc -> fill, or lineTo -> stroke, but I haven't really experimented with them personally. Details:
You'd also need to decide where to draw to...probably a good idea to create a new sprite and draw to that. You should be able to add sprite layers by extending a method of the appropriate spriteset, e.g.
Spoiler: Add new sprite to spriteset
JavaScript:
(function(alias) {
  Spriteset_Map.prototype.createUpperLayer = function() {
    alias.apply(this, arguments);
    this._drawingLayer = new Sprite(new Bitmap(Graphics.boxWidth, Graphics.boxHeight));
    this.addChild(this._drawingLayer);
  };
})(Spriteset_Map.prototype.createUpperLayer);
@caethyril, thank you again for your help! I think that emitting particles from the points where the touch input is logged is a great idea, and if we've learned anything from @ImaginaryVillain, it's that particle are capable of doing a heck of a lot of things!

Also, I have looked through your example code, and a lot of it looks like some pretty solid scripting. But, as I am likely to make this a plugin, is there any way we could add some parameters to specify what shapes the plugin is detecting? To make it easier, we could only put limited options in the parameter as to what the user has to draw, so we don't have to take every possible shape that everyone could ever want into account. Anyway, thanks for your amazing assistance.


EDIT: Sorry, @taarna23, I put the wrong user at the top, sorry if you got a notification...
Not entirely sure what you're asking here... 😅

When checking for a shape, I assume you'll be going through each one in turn (—, |, V, etc) seeking a match. Thus, if you want something excluded, you just need to add an extra check (boolean) before processing the relevant code. 🙏

To add parameters to a plugin, write the plugin header in the usual format, e.g.
JavaScript:
/*:
 * @plugindesc Description
 * @author Author
 * @help Line 1
 * Line 2
 *
 * @param Horizontal Line
 * @type boolean
 * @desc If true, check for horizontal lines.
 * @default false
 */
You can fetch the parameters (string format) and parse them in the code, e.g.
JavaScript:
var checkLineH = PluginManager.parameters(pluginFileName)['Horizontal Line'] === 'true';
Then you have a set of boolean (i.e. true|false) values dictacting whether you should check each shape, or not. These can be used to filter which shapes you check, e.g.
JavaScript:
if (checkLineH && isLineH()) {
  // do horizontal line match stuff
} else if (checkLineV && isLineV()) {
  // do vertical line match stuff
// etc


If you were going more for a sort of "specify your own polygons" thing, you'd first need to figure out how you want to represent the shapes. In that case I'd consider a series of value-pairs in [0, 1], representing a unit-scale version of a shape's vertices...or a series of angles and side length? Then you'd be able to perform analysis of one set of points vs another. I imagine this would involve a bit of maths, e.g. scaling, interpolation (for uneven point counts), accuracy (check distance from perfect shape); no idea how strong you are in that area. 😴

Also, one thing about PIXI particles: are they canvas- or WebGL-based? Mentioning this because it's worth considering if you plan to release the final plugin publicly: core RMMV does not strictly require a WebGL-capable system.
@caethyril, I think PIXI particles are WebGL based, as I did a quick Google search and the majority of the results that came up said that they were WebGL based. Also, a "specify your own polygons" thing would be cool if I could get it to work, but I was thinking more along the lines of having limited choices in the parameters and you can select the certain shapes that you want the plugin to recognize.
Most modern systems should support WebGL, just thought I'd mention it. 🙂

And yea, having several distinct on/off parameters for the different shapes would probably be easiest. Sounds like a neat idea, good luck with it! 😂
@caethyril, thank you, both for your help and for the luck!
is there a final plugin in for this? I want to make my gacha more exciting, like when the shape is drawn on screen, the number of points will affect randomness.
Reminds me of the DS game LostMagic where you drew runes on the touchscreen and somehow it knew what the heck you were drawing. I always wondered how that worked.
Reminds me of the DS game LostMagic where you drew runes on the touchscreen and somehow it knew what the heck you were drawing. I always wondered how that worked.
This is what i've been looking for. i forgot where i saw a working onr for MV.

Original thread: https://forums.rpgmakerweb.com/threads/detect-drawn-shapes.129612/