Math for Mouse Randomness to follow an Infinity Symbol's path **SOLVED**

Started by Knox, Wed 08/12/2010 07:27:38

Previous topic - Next topic

Knox

Hi!

Ive got a weird one...right now Im using the following script to make the mouse cursor move/jitter randomly around:

Code: ags

void mouseDrunk(int iPixelsInitial, int iCounterInitial)
{
  iPixels = iPixelsInitial;
  iCounter = iCounterInitial;
  int iRand_a = Random(iPixels);
  int iRand_b = Random (1); //pos or neg
  int iCurrentPosX = (mouse.x);
  int iCurrentPosY = (mouse.y);
  int iDrunkPosX;
  int iDrunkPosY;
  if (drunkMouseCounter >= iCounter) //every 5 game loops
  {
    //substract random num
    //DisplayACL("%d", iRand_b);
    if (iRand_b == 0)
    {
      iDrunkPosX = (iCurrentPosX + iRand_a);
      iDrunkPosY = (iCurrentPosY + iRand_a);
    }
    else if (iRand_b == 1)
    {
      iDrunkPosX = (iCurrentPosX - iRand_a);
      iDrunkPosY = (iCurrentPosY - iRand_a);      
    }
    drunkMouseCounter = 0;
    mouse.SetPosition(iDrunkPosX, iDrunkPosY);
  }
  else drunkMouseCounter++;
}


The result is pretty good, but it seems too "linear", not "swooshy" enough. Do any of you geniuses know how I could have the mouse cursor follow an infinity symbol (horizontal figure-8), clock-wise and counter-clockwise? The cursor would move along this path randomly, but not too much so its choppy/jumps and also so that the infinity symbol is kinda evident (but not too much, ehehe)...more of a smooth swooshing with slight random jitter...

This is too tough for me!
--All that is necessary for evil to triumph is for good men to do nothing.

Calin Leafshade

#1
an infinity symbol is essentially two sine waves with the horizontal one having twice the frequency of the vertical one.

no wait, the frequencies are the same but they are out of phase.

if you want the movement to feel smooth then a sinewave is definitely the way to go.

Knox

Ill take a look in your UtilityModule 0.1 to see if I can extract what I need...thanks for the heads up!

**EDIT**

Id like the mouse to follow this path (lemniscate path):
http://curvebank.calstatela.edu/lemniscate/lemniscate.htm#
http://mathworld.wolfram.com/Lemniscate.html
--All that is necessary for evil to triumph is for good men to do nothing.

Knox

Hi guys,

This is a pretty old thread, however I was starting to work on my game after a pretty big break of not doing much, and I must admit: Im not getting very far in this area! I can get my mouse to move randomly, but without being inside a "set path" that follows a lemniscate so it mimicks a "drunken mouse".

I guess I will wave my white flag and surrender.  :'(

Any help would be more than appreciated!
--All that is necessary for evil to triumph is for good men to do nothing.

Khris

Try this:

Code: ags
float timer;
int slow;

void repeatedly_execute() {
  
  slow++;
  if (slow < 2) return;
  slow = 0;
  
  #define bound Maths.Pi*2.0
  
  timer += 0.1;
  if (timer > bound/2.0) timer -= bound;
  
  float t = timer;
  
  #define x Maths.Sin(t)
  #define y Maths.Sin(t*2.0)*0.5
  
  float x1 = x;
  float y1 = y;

  #define f 2.0
  #define s 0.4
  
  t = t + s;
  if (t > bound/2.0) t -= bound;
  
  float x2 = x;
  float y2 = y;
  int vx = FloatToInt(f*(x2-x1), eRoundNearest) + Random(2)-1;
  int vy = FloatToInt(f*(y2-y1), eRoundNearest) + Random(2)-1;
  
  mouse.SetPosition(mouse.x + vx, mouse.y + vy);
}

Knox

lol! It works so nice! Haha, man, as soon as I tested it out, it does EXACTLY what I was looking for!

Sigh, I wish I had your skills...

Thnx Khris!
;D
--All that is necessary for evil to triumph is for good men to do nothing.

Snarky

When I did something similar (though with much messier code), I found that calling mouse.Update() just before mouse.SetPosition() really helped the responsiveness.

Also, I've found that setting the mouse coordinates directly is problematic for running the game in a window, since it means you can never move the cursor outside of the window. (And this tends to be extremely irritating to players.) My workaround was to disable the mouse moving code when the cursor is up against one of the edges of the screen.

Knox

Hey Snarky,

Those are good ideas, I added a check if the mouse exits the window from the top right corner...that way players dont need to struggle to exit the window + close it while "drunk".
--All that is necessary for evil to triumph is for good men to do nothing.

SMF spam blocked by CleanTalk