File Coverage

blib/lib/Collision/2D/Entity.pm
Criterion Covered Total %
statement 16 17 94.1
branch 2 2 100.0
condition n/a
subroutine 5 6 83.3
pod 2 4 50.0
total 25 29 86.2


line stmt bran cond sub pod time code
1             package Collision::2D::Entity;
2 7     7   45 use strict;
  7         14  
  7         485  
3 7     7   36 use warnings;
  7         13  
  7         6141  
4              
5             require DynaLoader;
6             our @ISA = qw(DynaLoader);
7             bootstrap Collision::2D::Entity;
8              
9 0     0 0 0 sub typename{'entity'}
10              
11              
12             #an actual collision at t=0;
13             sub null_collision{
14 15     15 0 25 my $self = shift;
15 15         16 my $other = shift;
16 15         75 return Collision::2D::Collision->new(
17             time => 0,
18             ent1 => $self,
19             ent2 => $other,
20             );
21             }
22              
23             sub intersect{
24 96     96 1 13812 my ($self, @others) = @_;
25 96         166 for (@others){
26 97 100       253 return 1 if Collision::2D::intersection ($self, $_);
27             }
28 25         129 return 0;
29             }
30              
31             sub collide{
32 12     12 1 24 my ($self, $other, %params) = @_;
33 12         20 $params{keep_order} = 1;
34 12         37 return Collision::2D::dynamic_collision ($self, $other, %params);
35             }
36              
37             sub new{die}
38             1
39              
40             __END__
41             =head1 NAME
42              
43             Collision::2D::Entity - A moving entity. Don't use this directly.
44              
45             =head1 DESCRIPTION
46              
47             =head1 ATTRIBUTES
48              
49             =head2 x,y,xv,yv
50              
51             Absolute position and velocity in space.
52             These are necessary if you want to do collisions through
53             L<dynamic_collision|Collision::2D/dynamic_collision>
54              
55             dynamic_collision($circ1, $circ2);
56              
57             =head2 relative_x, relative_y, relative_xv, relative_yv
58              
59             You shouldn't worry about these. Move along now.
60              
61             Relative position and velocity in space.
62             these are necessary if you want to do collisions directly through entity methods,
63              
64             $circ1->_collide_circle($circ2);
65              
66             In this case, both the absolute and relative position and velocity of $circ2
67             is not used. The relative attributes of $circ1 are assumed to be relative to $circ2.
68              
69              
70             =head1 METHODS
71              
72             =head2 collide
73              
74             my $collision = $self->collide ($other_entity, interval=>4);
75              
76             Detect collision with another entity. $self must be normalized to $other.
77             Takes interval as a parameter. Returns a collision if there is a collision.
78             Returns undef if there is no collision.
79              
80             With the collide method, the entity order is preserved.
81             Consider this example:
82              
83             my $collision1 = $panel->collide($droplet);
84             my $collision2 = $droplet->collide($panel);
85              
86             If these objects collide, then its C<$collision1->ent1> will be C<$panel>, and
87             C<$collision2->ent1> will be C<$droplet>.
88              
89             =head2 intersect
90              
91             my $t_or_f = $self->intersect ($other_entity, interval=>2.5);
92              
93             Detect intersection (overlapping) with another entity.
94             Takes interval as a parameter. Returns a collision if there is a collision.
95             Returns undef if there is no collision.
96              
97             C<interval> is optional. C<interval> is 1 by default.
98              
99             Relative vectors and velocity are not considered for intersection.
100              
101             =head2 normalize
102              
103             You probably shouldn't use this directly. At all.
104             Relative vectors are handled automatically
105             in C<dynamic_collision> and in C<$ent1->collide($ent2)>
106              
107             $self->normalize($other); # $other isa entity
108              
109             This compares the absolute attributes of $self and $other.
110             It only sets the relative attributes of $self.
111             This is necessary to call _collide_*($other) methods on $self.