O'Reilly Forums: Bee Simulator - Bees Bouncing - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Bee Simulator - Bees Bouncing

#1 User is offline   Ziechman 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 12-November 12

Posted 12 November 2012 - 07:54 PM

The bees in my Bee Simulator are exhibiting an odd behavior. While moving, a large percentage, sometimes all of them, "bounce" either back and forth, or up and down, depending on which direction they are moving. If they're moving horizontally (such as toward the flowers), they bounce up and down. If they're moving vertically (such as to and from the Honey Factory), the bounce back and forth. It doesn't happen with all the bees, all the time, though if they're moving from one exact location to another, which only really happens when they go from the Entrance to the Honey Factory, they will always bounce. If I fiddle around with the coordinates of my Honey Factory and Entrance, I can get them to not bounce during that trip. Since all other coordinates are created randomly, I can't really test them.

Other data points:
The bees don't bounce if I change the MoveRate to 1.
The bees never bounce on the way back from the flowers, no matter what location the hive is at, and what flower they came from.
I'm using the Graphics bees, not the custom control.
This problem also occurs with the solution that I downloaded from the HF C# website.


Possibly unrelated: Recently, since I've been messing with the code trying to fix this, I've had a problem where bees come out of the hive, drop down a little ways, and stay there, not moving, and perpetually "FlyingToFlower" I'm pretty sure I changed all the code back to the way it was before, so possibly this is a bug that just showed up late, as it doesn't happen every time.
0

#2 User is offline   Ziechman 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 12-November 12

Posted 13 November 2012 - 09:44 AM

I figured it out!
because of the way the MoveTowardLocation method works, unless the difference between the two points is divisible by 3 (the MoveRate), the bees will bounce. this is because if say, the destination Y is 266, and the bee is at 265, the next cycle it will jump to 268, and then back to 265, and so on. My solution is as follows:
private bool MoveTowardsLocation(Point destination)
        {
            if (Math.Abs(destination.X - location.X) <= MoveRate) 
            {
                location.X = destination.X;
            } 
            else 
            {
                 if (destination.X > location.X)
                        location.X += MoveRate;
                else if (destination.X < location.X)
                        location.X -= MoveRate;
            }


             if(Math.Abs(destination.Y - location.Y) <= MoveRate) 
             {
                    location.Y = destination.Y;
             } 
             else 
             {
                     if (destination.Y > location.Y)
                        location.Y += MoveRate;
                    else if (destination.Y < location.Y)
                        location.Y -= MoveRate;
              }
             if ((location.X == destination.X) && (location.Y == destination.Y))
                 return true;
             else
                 return false;

        }


Also, because of the way the bee animation is set up, the forms are being repainted unnecessarily every 3rd cycle, which can slow it down. So if running the Bee Simulator is slowing your computer down, you can take the Invalidate calls out of the AnimateBees method, and you'll get get about a 30 percent decrease in CPU usage (and equivalent increase in overall speed) without effecting the behavior of the program.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users