| 1 | package SDL::Tutorial::3DWorld::Camera::God; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Camera::God - A "God Mode" flying person camera | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | A flying-mode camera is a camera in which the "move" direction occurs in | ||
| 12 | three dimensions rather than two dimensions. | ||
| 13 | |||
| 14 | In addition, a different set of movement modifiers apply to a flying | ||
| 15 | camera than apply to a plain or walking camera. | ||
| 16 | |||
| 17 | =head1 METHODS | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | use 5.008; | ||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | use OpenGL; | ||
| 25 | use SDL::Mouse; | ||
| 26 | use SDL::Constants (); | ||
| 27 | use SDL::Tutorial::3DWorld::Camera (); | ||
| 28 | |||
| 29 | our $VERSION = '0.33'; | ||
| 30 | our @ISA = 'SDL::Tutorial::3DWorld::Camera'; | ||
| 31 | |||
| 32 | use constant D2R => CORE::atan2(1,1) / 45; | ||
| 33 | |||
| 34 | sub new { | ||
| 35 | my $self = shift->SUPER::new(@_); | ||
| 36 | my $down = $self->{down}; | ||
| 37 | |||
| 38 | # Store the original speed for later | ||
| 39 | $self->{speed_original} = $self->{speed}; | ||
| 40 | |||
| 41 | # Move camera forwards and backwards | ||
| 42 | $down->{SDL::Constants::SDLK_w} = 0; | ||
| 43 | $down->{SDL::Constants::SDLK_s} = 0; | ||
| 44 | |||
| 45 | # Strafe camera left and right | ||
| 46 | $down->{SDL::Constants::SDLK_a} = 0; | ||
| 47 | $down->{SDL::Constants::SDLK_d} = 0; | ||
| 48 | |||
| 49 | # Lift up and down | ||
| 50 | $down->{SDL::Constants::SDLK_SPACE} = 0; | ||
| 51 | $down->{SDL::Constants::SDLK_LCTRL} = 0; | ||
| 52 | |||
| 53 | # Shift makes us run | ||
| 54 | $down->{SDL::Constants::SDLK_LSHIFT} = 0; | ||
| 55 | |||
| 56 | return $self; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | ###################################################################### | ||
| 64 | # Engine Interface | ||
| 65 | |||
| 66 | sub move { | ||
| 67 | my $self = shift; | ||
| 68 | my $step = shift; | ||
| 69 | my $down = $self->{down}; | ||
| 70 | |||
| 71 | # The shift key will allow continuous exponential | ||
| 72 | # acceleration of around 5% per second. | ||
| 73 | if ( $down->{SDL::Constants::SDLK_LSHIFT} ) { | ||
| 74 | $self->{speed} += $self->{speed} * 0.05 * $step; | ||
| 75 | } else { | ||
| 76 | $self->{speed} = $self->{speed_original}; | ||
| 77 | } | ||
| 78 | |||
| 79 | # To prevent angle-running and other tricks we need to find and | ||
| 80 | # normalise the direction of movement before applying our speed. | ||
| 81 | my $move = $down->{SDL::Constants::SDLK_w} | ||
| 82 | - $down->{SDL::Constants::SDLK_s}; | ||
| 83 | my $strafe = $down->{SDL::Constants::SDLK_d} | ||
| 84 | - $down->{SDL::Constants::SDLK_a}; | ||
| 85 | my $lift = $down->{SDL::Constants::SDLK_SPACE} | ||
| 86 | - $down->{SDL::Constants::SDLK_LCTRL}; | ||
| 87 | |||
| 88 | # Apply this movement in the direction of the camera. | ||
| 89 | # Math applied unoptimised and longhand for greater readability. | ||
| 90 | my $angle = $self->{angle} * D2R; | ||
| 91 | my $elevation = $self->{elevation} * D2R; | ||
| 92 | my $VX = 0; | ||
| 93 | my $VY = 0; | ||
| 94 | my $VZ = 0; | ||
| 95 | $VX += $move * sin($angle) * cos($elevation); | ||
| 96 | $VY += $move * sin($elevation); | ||
| 97 | $VZ += $move * -cos($angle) * cos($elevation); | ||
| 98 | $VX += $strafe * cos($angle); | ||
| 99 | $VY += $strafe * 0; | ||
| 100 | $VZ += $strafe * sin($angle); | ||
| 101 | $VX += $lift * sin($elevation) * -sin($angle); | ||
| 102 | $VY += $lift * cos($elevation); | ||
| 103 | $VZ += $lift * sin($elevation) * cos($angle); | ||
| 104 | |||
| 105 | # Normalise the velocity and apply to the position delta | ||
| 106 | my $VL = sqrt( $VX ** 2 + $VY ** 2 + $VZ ** 2 ) || 1; | ||
| 107 | my $VS = $self->{speed} * $step / $VL; | ||
| 108 | |||
| 109 | # Apply the final velocity to the position | ||
| 110 | $self->{X} += $VX * $VS; | ||
| 111 | $self->{Y} += $VY * $VS; | ||
| 112 | $self->{Z} += $VZ * $VS; | ||
| 113 | |||
| 114 | # Clip to the zero plain (plus our "height") | ||
| 115 | $self->{Y} = 1.5 if $self->{Y} < 1.5; | ||
| 116 | |||
| 117 | return; | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; | ||
| 121 | |||
| 122 | =pod | ||
| 123 | |||
| 124 | =head1 SUPPORT | ||
| 125 | |||
| 126 | Bugs should be reported via the CPAN bug tracker at | ||
| 127 | |||
| 128 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 129 | |||
| 130 | =head1 AUTHOR | ||
| 131 | |||
| 132 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 133 | |||
| 134 | =head1 SEE ALSO | ||
| 135 | |||
| 136 | L<SDL>, L<OpenGL> | ||
| 137 | |||
| 138 | =head1 COPYRIGHT | ||
| 139 | |||
| 140 | Copyright 2010 Adam Kennedy. | ||
| 141 | |||
| 142 | This program is free software; you can redistribute | ||
| 143 | it and/or modify it under the same terms as Perl itself. | ||
| 144 | |||
| 145 | The full text of the license can be found in the | ||
| 146 | LICENSE file included with this module. | ||
| 147 | |||
| 148 | =cut |