I want to make a driving game where you can do tricks with your mouse when you hold down LMB. Ex. if you move it up in a straight line really fast (your cursor goes from the bottom to the top of the screen in, say, 0.1s), your bike will do a flip. If you do a quick circular movement, your bike will spin. Essentially, drawing a shape onto the screen = performing a move.

How would this be sensed? Currently, I can only think of having shapes defined by coordinates relative to some starting position (it would be some function taking in your mouse down coords), and within a certain time frame, every update loop will check to see if the current mouse coordinate pair is between the current preset one and the next preset one (and within a margin of error). To know which shape to use, you'd loop through the shape list, see whether or not the mouse coord is invalid for the current shape, and knock it off the possibilities if true.

Example circle path:

  • X = Coordinate
  • Red arrows = Expected mouse movement path
  • Green = Margin of Error


Problems I can see with this algorithm right now:

  • If I have a lot of moves, then there are a lot of shapes to loop through every frame before the deduction process kicks in
  • It's not an optimal solution for doing consecutive moves in a short timespan, because the player would have to release LMB before pressing it down again for the system to sense that a new shape is starting
  • Tiny circles vs big circles, how wide the margin of error would need to be to allow for both, and how that would affect calculations (clashing with other shapes that have similar valid spaces)
  • What if you draw circles like flattened, diagonally skewed ovals, even when you're not trying to do it fast? It is indeed a circular motion, but maybe diagonally skewed ovals are for another move. Is it okay to just shun your artistic ability?
  • How much time will it take before the system recognizes that you're doing a specific move? Must a full circle be completed before any bike spinning occurs?

What would be a better algorithm?


1 Answer 1


I once created a system like this as an experiment. The way I did it was to abstract the pixel-perfect path of the mouse away into a series of quantized directions of travel (rounded to 45° for my purposes). My primitive matching algorithm just tried to fit a pattern to the series when the user released the mouse button.

If I were to do it again I would add in these features to make it more robust:

  • Understand that a curve could be sampled as both 45° and 90° turns
  • Be able to start circles at any point
  • Do early elimination of patterns by guessing when the user has started a gesture (so they don't have to use the mouse button)
  • Sample only every few frames and spread pattern matching over the whole interval

Actually, your proposal could be viable if you took that last point into consideration. Although I barely dabble in either field, I'm sure some research into signal processing and machine learning could benefit the construction of such a system.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .