-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcollision
49 lines (38 loc) · 1.36 KB
/
collision
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
#!/usr/bin/env perl
use strict;
use warnings;
# Attempt to simulate a wind-chime
use Collision::2D qw(hash2circle dynamic_collision);
use MIDI::Util qw(setup_score);
my $max = shift || 32; # Number of times to detect collisions
my $bpm = shift || 40; # "Wind speed"
my $duration = shift || 'sn';
my $patch = shift || 14;
my $score = setup_score(lead_in => 0, bpm => $bpm, patch => $patch);
my @chimes = (
{ chime => hash2circle({ x => 0, y => 4, radius => 1 }), note => 'C6' }, # N
{ chime => hash2circle({ x => 4, y => 0, radius => 1 }), note => 'F6' }, # E
{ chime => hash2circle({ x => -4, y => 0, radius => 1 }), note => 'G6' }, # W
{ chime => hash2circle({ x => 0, y => -4, radius => 1 }), note => 'C7' }, # S
);
for my $i (1 .. $max) {
my ($xv, $yv) = (randv(), randv());
printf "%d. xv = %5.2f, yv = %5.2f\n", $i, $xv, $yv;
my $clapper = hash2circle({ x => 0, y => 0, radius => 2, xv => $xv, yv => $yv });
for my $c (@chimes) {
my $collision = dynamic_collision($clapper, $c->{chime});
if ($collision) {
printf "\tCollision with chime: %s\n", $c->{note};
$score->n($duration, $c->{note});
}
else {
$score->r($duration);
}
}
}
$score->write_score("$0.mid");
sub randv {
my $x = rand 2;
my $sign = int rand 2 ? 1 : -1;
return $x * $sign;
}