-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoids.m
77 lines (64 loc) · 2.11 KB
/
Boids.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
% Project - Boid Simulation
% Author: Jin Woo Ahn
clear all;
close all;
numBoids = 70; % Number of boids to be simulated.
numPreds = 0; % Number of predators to be simulated.
height = 300; % Height of the world.
width = 300; % Width of the world.
iteration = 1000; % Number of simulation times.
b_max_speed = 2; % Maximum speed of boids.
p_max_speed = 1.8; % Maximum speed of predators.
% Create a World object.
world = World(height, width);
% Create an array of Boid objects.
boids(numBoids) = Boid;
% Create an array of Predator objects.
if numPreds == 0
preds = 0;
else
preds(numPreds) = Predator;
end
% Initialize the boids with random color, coordinate, and velocity.
for i = 1 : numBoids
boids(i).color_r = rand;
boids(i).color_g = rand;
boids(i).color_b = rand;
boids(i).velocity = [rand * (b_max_speed * 2) - (b_max_speed / 2), rand * (b_max_speed * 2) - (b_max_speed / 2)];
boids(i).coord = [(rand * height - 1) + 1, (rand * width - 1) + 1];
boids(i).set_height_and_width(height, width);
boids(i).set_max_speed(b_max_speed);
end
% Initialize the preds with random coordinate and velocity.
if numPreds ~= 0
for i = 1 : numel(preds)
preds(i).coord = [(rand * height - 1) + 1, (rand * width - 1) + 1];
preds(i).set_max_speed(b_max_speed);
preds(i).set_height_and_width(height, width);
preds(i).velocity = [rand * (p_max_speed * 2) - (p_max_speed / 2), rand * (p_max_speed * 2) - (p_max_speed / 2)];
end
end
% Create video writer object.
video = VideoWriter('Boids.avi', 'Motion JPEG AVI');
% Set the frame rate.
video.FrameRate = 20;
% Open the video file.
open(video);
% Start the simulation.
for t = 1 : iteration
% Simulation of boids.
for i = 1 : numBoids
boids(i).move(boids, preds);
world.draw_boids(boids(i));
end
% Simulation of predators.
if numPreds ~= 0
for j = 1 : numel(preds)
preds(j).move(boids);
world.draw_predator(preds(j));
end
end
writeVideo(video, imresize(world.outputWorld, [height, width]));
end
% Close video.
close(video);