File Coverage

blib/lib/OpenGL/Earth/Physics.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             #
2             # Acceleration, speed, and rotation of the Earth
3             #
4             # $Id$
5            
6             package OpenGL::Earth::Physics;
7            
8 1     1   1565 use strict;
  1         2  
  1         42  
9 1     1   7 use warnings;
  1         2  
  1         34  
10 1     1   476 use OpenGL;
  0            
  0            
11             use OpenGL::Earth::Wiimote;
12            
13             our $X_ROT = 300.0;
14             our $Y_ROT = 0.0;
15            
16             our $X_SPEED = 0.0;
17             our $Y_SPEED = 0.02;
18            
19             our $Z_OFF = -5.0;
20            
21            
22             sub calculate_falloff_motion {
23            
24             my ($motion) = @_;
25            
26             my $falloff_factor = 0.96;
27             my $acc = abs($motion->{force_x} / 10);
28            
29             my $keys = OpenGL::Earth::Wiimote::get_keys();
30             my $acc_pos = exists $keys->{A};
31             my $home = exists $keys->{home};
32            
33             if ($home) {
34             $X_ROT = 300.0;
35             $Y_ROT = 0.0;
36             $X_SPEED = 0.0;
37             $Y_SPEED = 0.02;
38             return;
39             }
40            
41             if (exists $keys->{up}) {
42             $Z_OFF -= 0.01;
43             }
44             if (exists $keys->{down}) {
45             $Z_OFF += 0.01;
46             }
47            
48             if ($acc_pos) {
49             $X_SPEED += $acc * ($X_SPEED > 0 ? 1 : -1);
50             $Y_SPEED += $acc * ($Y_SPEED > 0 ? 1 : -1);
51             }
52             else {
53             $X_SPEED += $motion->{tilt_z} * $acc;
54             $Y_SPEED += $motion->{tilt_y} * $acc;
55             }
56            
57             if ($X_SPEED > 5) { $X_SPEED = 5 }
58             if ($Y_SPEED > 5) { $Y_SPEED = 5 }
59            
60             $X_ROT += $X_SPEED;
61             $Y_ROT += $Y_SPEED;
62            
63             if ($X_SPEED > 0.02) {
64             $X_SPEED *= $falloff_factor;
65             }
66            
67             if ($Y_SPEED > 0.02) {
68             $Y_SPEED *= $falloff_factor;
69             }
70            
71             return;
72             }
73            
74             {
75             my $prev_tilt_x = 0.0;
76             my $prev_tilt_y = 0.0;
77            
78             sub calculate_static_motion {
79            
80             my ($motion) = @_;
81            
82             # Now let's do the motion calculations.
83             $X_SPEED = ($motion->{tilt_z} - $prev_tilt_x) / 2;
84             $Y_SPEED = ($motion->{tilt_y} - $prev_tilt_y) / 2;
85            
86             $prev_tilt_x = $X_SPEED;
87             $prev_tilt_y = $Y_SPEED;
88            
89             $X_ROT += $X_SPEED;
90             $Y_ROT += $Y_SPEED;
91            
92             return;
93             }
94            
95             }
96            
97             sub move {
98            
99             # Move the object back from the screen.
100             glTranslatef(0.0,0.0,$Z_OFF);
101            
102             # Rotate the calculated amount.
103             glRotatef($X_ROT,1.0,0.0,0.0);
104             glRotatef($Y_ROT,0.0,0.0,1.0);
105            
106             return;
107             }
108            
109             sub reverse_motion {
110             $X_SPEED = -$X_SPEED;
111             $Y_SPEED = -$Y_SPEED;
112             return;
113             }
114            
115             sub stop {
116             $X_SPEED = $Y_SPEED = 0.0;
117             return;
118             }
119            
120             1;