How to make a firefly fly randomly within restricted area

Started by tor.brandt, Thu 08/12/2016 11:55:47

Previous topic - Next topic

tor.brandt

I want to make a firefly object that flies around randomly within a restricted area.
I'd like the movement to be 'soft' and 'rounded', such that it doesn't just fly from one random x,y coordinate to the next in a straight line, but makes more of a half-circle kind of motion between the points, to imitate the way insects move.

In principle I COULD find the needed commands in the manual myself, but I'm really not very good at understanding mathematical expressions (and I take it I will need some of those), which is why I'd like to hear if someone has an idea for how I could write such a function?

Thanks :-D

Retro Wolf

Very quickly (not in real ags code).

Make two variables xspeed, and yspeed. When a timer reaches 0 make the xspeed = (-1 + random(2)) and yspeed = (-1 + random(2))
In repeatedly execute have the object.x += xspeed and the object.y += yspeed.
If the x and y leave a specific area force the timer to 0 so it fires.
eg: if (x < 0) || (x > 100) then timer = 0. (I think setting timer to 0 might disable it so you might have to set it to 1)

Every time the timer resets you could make it a random length.

I've used similar code to make a firefly in a game and found it satisfying.


tor.brandt

Thanks for your replies!

@Retro Wolf:
Right now I can't figure out how your suggestion is supposed to work (like in: I'm not doubting its functionality, but I just can't wrap my head around it at the moment), but when I try to apply it, it just makes the object move diagonally in one direction.
I'm probably doing it wrong :-D

@Crimson Wizard:
Thanks for the link!
I've looked at the functions in the module, but I can't figure out which one I should use for curved line movement?

Retro Wolf

Code: ags

int xSpeed;
int ySpeed;

function firefly_move() {  

  if (IsTimerExpired(1)) {
    xSpeed = (-2 + Random(3));
    ySpeed = (-2 + Random(3));
    
    int rand = Random(3);
    if (rand == 0) {
      xSpeed = 0;
      ySpeed = 0;
    }
    
    SetTimer(1, 10 + Random(40));
  }
  
  if (object[0].X < 10) {
    xSpeed = 2;
  }
  else if (object[0].X > 310) {
    xSpeed = -2;
  }
  
  if (object[0].Y < 10) {
    ySpeed = 2;
  }
  else if (object[0].Y > 190) {
    ySpeed = -2;
  }
  
  object[0].X += xSpeed;
  object[0].Y += ySpeed;
}


I know that it mostly doesn't make curvy moves, but it kind of works like a fly darting in random and sometimes lazy directions.
Don't forget to set the timer somewhere, or it will never start.

Crimson Wizard

#5
Sorry, I think I could make mistake about what Tween does. I will try it myself first to be sure.

EDIT: Ok, try this for example:
Code: ags

function on_mouse_click(MouseButton btn)
{
    oFireFly.TweenX(1.0, mouse.x, eEaseInCircTween);
    oFireFly.TweenY(1.0, mouse.y, eEaseLinearTween);
}


This should change Y coordinate at linear speed, and X coordinate in "circular" speed, which will result in curvy move.

BTW, I found this demo which may be used to visualize tween styles: http://gizma.com/easing

Khris

I wrote a module, give it a try: click

Room script example code:
Code: ags
DrawingSurface *roomBG, ds;

function room_Load()
{
  Firefly.SetSpriteSlot(31);
  Firefly.SetArea(100, 100, 110, 110); // x1, y1, x2, y2
  for (int i = 0; i < 15; i++) Firefly.Add();

  ds = Room.GetDrawingSurfaceForBackground();
  roomBG = ds.CreateCopy();
  Firefly.DrawFlies(ds);
  ds.Release();
}

function room_RepExec()
{
  ds = Room.GetDrawingSurfaceForBackground();
  ds.DrawSurface(roomBG);
  // other draw commands
  Firefly.DrawFlies(ds);
  ds.Release();
}


The flies don't strictly stay within the stated area, but as soon as they leave it, they will fly back towards it.
Just set the area occupied by the light source like its a rectangular hotspot and they will circle it.

[embed=620,520]http://youtu.be/_ZaXTvI1oak[/embed]

(It's not choppy like that in-game! :))

tor.brandt

Thanks for your further replies, and sorry for not responding!
I've been busy, but I'm looking forward to trying out your suggestions :-D

tor.brandt

Quote from: Crimson Wizard on Thu 08/12/2016 16:01:36
EDIT: Ok, try this for example:
Code: ags

function on_mouse_click(MouseButton btn)
{
    oFireFly.TweenX(1.0, mouse.x, eEaseInCircTween);
    oFireFly.TweenY(1.0, mouse.y, eEaseLinearTween);
}


This should change Y coordinate at linear speed, and X coordinate in "circular" speed, which will result in curvy move.

BTW, I found this demo which may be used to visualize tween styles: http://gizma.com/easing

Thank you for the clarification!
And the demo is really helpful for me, since I'm having a hard time visualizing the mathematical functions :-D

tor.brandt

Quote from: Khris on Thu 08/12/2016 18:08:29
I wrote a module, give it a try: click

The flies don't strictly stay within the stated area, but as soon as they leave it, they will fly back towards it.
Just set the area occupied by the light source like its a rectangular hotspot and they will circle it.

Those fireflies look really nice! :-D
Thank you for sharing that! However, I'd like to draw my own flies and have them as objects, so I think I'll have to go with the tween module...

SMF spam blocked by CleanTalk