line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright (c) 2018 Timm Murray |
2
|
|
|
|
|
|
|
# All rights reserved. |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Redistribution and use in source and binary forms, with or without |
5
|
|
|
|
|
|
|
# modification, are permitted provided that the following conditions are met: |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# * Redistributions of source code must retain the above copyright notice, |
8
|
|
|
|
|
|
|
# this list of conditions and the following disclaimer. |
9
|
|
|
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright |
10
|
|
|
|
|
|
|
# notice, this list of conditions and the following disclaimer in the |
11
|
|
|
|
|
|
|
# documentation and/or other materials provided with the distribution. |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
14
|
|
|
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15
|
|
|
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
16
|
|
|
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
17
|
|
|
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18
|
|
|
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19
|
|
|
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20
|
|
|
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21
|
|
|
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22
|
|
|
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
23
|
|
|
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE. |
24
|
|
|
|
|
|
|
package Game::Collisions; |
25
|
|
|
|
|
|
|
$Game::Collisions::VERSION = '0.3'; |
26
|
19
|
|
|
19
|
|
1310859
|
use v5.14; |
|
19
|
|
|
|
|
217
|
|
27
|
19
|
|
|
19
|
|
104
|
use warnings; |
|
19
|
|
|
|
|
34
|
|
|
19
|
|
|
|
|
618
|
|
28
|
19
|
|
|
19
|
|
114
|
use List::Util (); |
|
19
|
|
|
|
|
50
|
|
|
19
|
|
|
|
|
450
|
|
29
|
|
|
|
|
|
|
|
30
|
19
|
|
|
19
|
|
7682
|
use Game::Collisions::AABB; |
|
19
|
|
|
|
|
45
|
|
|
19
|
|
|
|
|
12214
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# ABSTRACT: Fast, pure Perl collision 2D detection |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub new |
36
|
|
|
|
|
|
|
{ |
37
|
9
|
|
|
9
|
1
|
789
|
my ($class) = @_; |
38
|
9
|
|
|
|
|
41
|
my $self = { |
39
|
|
|
|
|
|
|
root_aabb => undef, |
40
|
|
|
|
|
|
|
all_aabbs => {}, |
41
|
|
|
|
|
|
|
}; |
42
|
9
|
|
|
|
|
24
|
bless $self => $class; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
9
|
|
|
|
|
26
|
return $self; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub make_aabb |
50
|
|
|
|
|
|
|
{ |
51
|
34
|
|
|
34
|
1
|
1188
|
my ($self, $args) = @_; |
52
|
34
|
|
|
|
|
111
|
my $aabb = Game::Collisions::AABB->new( $args ); |
53
|
34
|
|
|
|
|
81
|
$self->_add_aabb( $aabb ); |
54
|
34
|
|
|
|
|
67
|
return $aabb; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub get_collisions |
58
|
|
|
|
|
|
|
{ |
59
|
5
|
|
|
5
|
1
|
646
|
my ($self) = @_; |
60
|
5
|
|
|
|
|
80
|
my @aabbs_to_check = values %{ $self->{all_aabbs} }; |
|
5
|
|
|
|
|
29
|
|
61
|
5
|
|
|
|
|
17
|
my @collisions; |
62
|
|
|
|
|
|
|
|
63
|
5
|
|
|
|
|
11
|
foreach my $aabb (@aabbs_to_check) { |
64
|
20
|
|
|
|
|
40
|
push @collisions => $self->get_collisions_for_aabb( $aabb ); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
5
|
|
|
|
|
23
|
return @collisions; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub get_collisions_for_aabb |
71
|
|
|
|
|
|
|
{ |
72
|
20
|
|
|
20
|
1
|
29
|
my ($self, $aabb) = @_; |
73
|
20
|
50
|
|
|
|
42
|
return () if ! defined $self->{root_aabb}; |
74
|
20
|
|
|
|
|
22
|
my @collisions; |
75
|
|
|
|
|
|
|
|
76
|
20
|
|
|
|
|
30
|
my @nodes_to_check = ($self->{root_aabb}); |
77
|
20
|
|
|
|
|
36
|
while( @nodes_to_check ) { |
78
|
95
|
|
|
|
|
106
|
my $check_node = shift @nodes_to_check; |
79
|
|
|
|
|
|
|
|
80
|
95
|
100
|
|
|
|
202
|
if( $check_node->is_branch_node ) { |
81
|
63
|
|
|
|
|
96
|
my $left_node = $check_node->left_node; |
82
|
63
|
|
|
|
|
94
|
my $right_node = $check_node->right_node; |
83
|
|
|
|
|
|
|
|
84
|
63
|
100
|
66
|
|
|
135
|
if( defined $left_node && $left_node->does_collide( $aabb ) ) { |
85
|
53
|
|
|
|
|
70
|
push @nodes_to_check, $left_node; |
86
|
|
|
|
|
|
|
} |
87
|
63
|
100
|
100
|
|
|
151
|
if( defined $right_node && $right_node->does_collide( $aabb ) ) { |
88
|
22
|
|
|
|
|
50
|
push @nodes_to_check, $right_node; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
else { |
92
|
|
|
|
|
|
|
# We already know it collided, since it wouldn't be added |
93
|
|
|
|
|
|
|
# to @nodes_to_check otherwise. |
94
|
32
|
|
|
|
|
114
|
push @collisions, [ $aabb, $check_node ]; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
20
|
|
|
|
|
51
|
return @collisions; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub get_collisions_for_aabb_bruteforce |
102
|
|
|
|
|
|
|
{ |
103
|
0
|
|
|
0
|
1
|
0
|
my ($self, $aabb) = @_; |
104
|
0
|
|
|
|
|
0
|
my @aabbs = values %{ $self->{all_aabbs} }; |
|
0
|
|
|
|
|
0
|
|
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
0
|
my @collisions = grep { $_->does_collide( $aabb ) } @aabbs; |
|
0
|
|
|
|
|
0
|
|
107
|
0
|
|
|
|
|
0
|
return @collisions; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub rebalance_tree |
111
|
|
|
|
|
|
|
{ |
112
|
1
|
|
|
1
|
1
|
5
|
my ($self) = @_; |
113
|
1
|
|
|
|
|
34
|
my @aabbs = values %{ $self->{all_aabbs} }; |
|
1
|
|
|
|
|
7
|
|
114
|
1
|
|
|
|
|
4
|
$self->{all_aabbs} = {}; |
115
|
|
|
|
|
|
|
|
116
|
1
|
|
|
|
|
5
|
my $new_root = $self->_new_meta_aabb({ |
117
|
|
|
|
|
|
|
x => 0, |
118
|
|
|
|
|
|
|
y => 0, |
119
|
|
|
|
|
|
|
length => 1, |
120
|
|
|
|
|
|
|
height => 1, |
121
|
|
|
|
|
|
|
}); |
122
|
1
|
|
|
|
|
2
|
$self->{root_aabb} = $new_root; |
123
|
1
|
|
|
|
|
3
|
$self->_add_aabb( $_ ) for @aabbs; |
124
|
|
|
|
|
|
|
|
125
|
1
|
|
|
|
|
4
|
return; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub root |
129
|
|
|
|
|
|
|
{ |
130
|
1
|
|
|
1
|
0
|
4
|
my ($self) = @_; |
131
|
1
|
|
|
|
|
5
|
return $self->{root_aabb}; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub _add_aabb |
136
|
|
|
|
|
|
|
{ |
137
|
38
|
|
|
38
|
|
57
|
my ($self, $new_node) = @_; |
138
|
|
|
|
|
|
|
|
139
|
38
|
100
|
|
|
|
115
|
if(! defined $self->{root_aabb} ) { |
140
|
9
|
|
|
|
|
21
|
$self->{root_aabb} = $new_node; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
else { |
143
|
29
|
|
|
|
|
78
|
my $new_root = $self->{root_aabb}->insert_new_aabb( $new_node ); |
144
|
29
|
100
|
|
|
|
78
|
$self->{root_aabb} = $new_root if defined $new_root; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
38
|
|
|
|
|
142
|
$self->{all_aabbs}{"$new_node"} = $new_node; |
148
|
38
|
|
|
|
|
67
|
return; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub _new_meta_aabb |
152
|
|
|
|
|
|
|
{ |
153
|
1
|
|
|
1
|
|
2
|
my ($self, $args) = @_; |
154
|
1
|
|
|
|
|
3
|
my $aabb = Game::Collisions::AABB->new( $args ); |
155
|
1
|
|
|
|
|
2
|
return $aabb; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
1; |
160
|
|
|
|
|
|
|
__END__ |