CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
This program was modified from code originally created by Larry O'Brien, and is based on the "Boids" program created by Craig Reynolds in 1986 to demonstrate an aspect of complexity theory called "emergence."
The goal here is to produce a reasonably lifelike reproduction of flocking or herding behavior in animals by establishing a small set of simple rules for each animal. Each animal can look at the entire scene and all the other animals in the scene, but it reacts only to a set of nearby "flockmates." The animal moves according to three simple steering behaviors:
Separation: Avoid crowding local flockmates.
Alignment: Follow the average heading of local flockmates.
Cohesion: Move toward the center of the group of local flockmates.
More elaborate models can include obstacles and the ability for the animals to predict collisions and avoid them, so the animals can flow around fixed objects in the environment. In addition, the animals might also be given a goal, which can cause the herd to follow a desired path. For simplicity, obstacle avoidance and goal-seeking is not included in the model presented here.
Emergence means that, despite the limited nature of computers and the simplicity of the steering rules, the result seems realistic. That is, remarkably lifelike behavior "emerges" from this simple model.
The program is presented as a combined application/applet:
//: FieldOBeasts.java
// Demonstration of complexity theory; simulates
// herding behavior in animals. Adapted from
// a program by Larry O'Brien lobrien@msn.com
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
class Beast
public void step()
}
// Rule 1: Match average speed of those
// in the list:
currentSpeed = totalSpeed / seen.size();
// Rule 2: Move towards the perceived
// center of gravity of the herd:
currentDirection =
totalBearing / seen.size();
// Rule 3: Maintain a minimum distance
// from those around you:
if(distanceToNearest <=
field.minimumDistance)
}
}
else
// Make the beast move:
x += (int)(Math.cos(currentDirection)
* currentSpeed);
y += (int)(Math.sin(currentDirection)
* currentSpeed);
x %= field.xExtent;
y %= field.yExtent;
if(x < 0)
x += field.xExtent;
if(y < 0)
y += field.yExtent;
}
public float bearingFromPointAlongAxis (
int originX, int originY, float axis)
else
}
// Just subtract the axis (in radians):
return (float) (axis - bearingInRadians);
} catch(ArithmeticException aE)
else
return (float) Math.PI;
}
}
public float distanceFromPoint(int x1, int y1)
public Point position()
// Beasts know how to draw themselves:
public void draw(Graphics g)
}
public class FieldOBeasts extends Applet
implements Runnable
beasts =
makeBeastVector(numBeasts, uniqueColors);
// Now start the beasts a-rovin':
thisThread = new Thread(this);
thisThread.start();
}
public void run()
try catch(InterruptedException ex)
repaint(); // Otherwise it won't update
}
}
Vector makeBeastVector(
int quantity, boolean uniqueColors)
}
}
newBeasts.addElement(
new Beast(this, x, y, direction, speed,
new Color(r,g,b)));
}
return newBeasts;
}
public Vector beastListInSector(Beast viewer)
}
return output;
}
public void paint(Graphics g)
}
public static void main(String[] args)
});
frame.add(field, BorderLayout.CENTER);
frame.setSize(640,480);
field.init();
field.start();
frame.setVisible(true);
}
} ///:~
Although this isn't a perfect reproduction of the behavior in Craig Reynold's "Boids" example, it exhibits its own fascinating characteristics, which you can modify by adjusting the numbers. You can find out more about the modeling of flocking behavior and see a spectacular 3-D version of Boids at Craig Reynold's page https://www.hmt.com/cwr/boids.html.
To run this program as an applet, put the following applet tag in an HTML file:
<applet
code=FieldOBeasts
width=640
height=480>
<param name=xExtent value = '640'>
<param name=yExtent value = '480'>
</applet>
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 667
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved