~manuq/+junk/manuqs_interactives

« back to all changes in this revision

Viewing changes to casa_tomada_2/cocos/actions/camera_actions.py

  • Committer: Manuel Quiñones
  • Date: 2008-07-07 06:48:54 UTC
  • Revision ID: manuel.por.aca@gmail.com-20080707064854-0dpwpz7at6atdne3
Import of casa_tomada_2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ----------------------------------------------------------------------------
 
2
# cocos2d
 
3
# Copyright (c) 2008 Daniel Moisset, Ricardo Quesada, Rayentray Tappa, Lucio Torre
 
4
# All rights reserved.
 
5
#
 
6
# Redistribution and use in source and binary forms, with or without
 
7
# modification, are permitted provided that the following conditions are met:
 
8
#
 
9
#   * Redistributions of source code must retain the above copyright
 
10
#     notice, this list of conditions and the following disclaimer.
 
11
#   * Redistributions in binary form must reproduce the above copyright 
 
12
#     notice, this list of conditions and the following disclaimer in
 
13
#     the documentation and/or other materials provided with the
 
14
#     distribution.
 
15
#   * Neither the name of cocos2d nor the names of its
 
16
#     contributors may be used to endorse or promote products
 
17
#     derived from this software without specific prior written
 
18
#     permission.
 
19
#
 
20
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
22
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 
23
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 
24
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 
25
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
26
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
27
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
28
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
29
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 
30
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
31
# POSSIBILITY OF SUCH DAMAGE.
 
32
# ----------------------------------------------------------------------------
 
33
'''Camera Actions
 
34
 
 
35
Actions that moves the OpenGL camera.
 
36
'''
 
37
 
 
38
__docformat__ = 'restructuredtext'
 
39
 
 
40
from cocos.director import director
 
41
from cocos.euclid import *
 
42
from base_actions import *
 
43
 
 
44
import math
 
45
 
 
46
__all__ = [ 'CameraException',            # Camera Exceptions
 
47
           
 
48
            'Camera3DAction',
 
49
            'OrbitCamera',
 
50
            ]
 
51
 
 
52
class CameraException( Exception ):
 
53
    pass
 
54
 
 
55
class Camera3DAction( IntervalAction ):
 
56
    def init( self,  duration=2):
 
57
        """Initialize the Camera Action
 
58
 
 
59
        :Parameters:
 
60
            `duration` : int 
 
61
                Number of seconds that the action will last
 
62
        """
 
63
        self.duration = duration
 
64
 
 
65
    def start( self ):        
 
66
        super(Camera3DAction, self).start()
 
67
 
 
68
        self.camera_eye_orig =  self.target.camera.eye
 
69
        self.camera_center_orig = self.target.camera.center
 
70
        self.camera_up_orig = self.target.camera.up_vector
 
71
 
 
72
    def stop( self ):
 
73
        super(Camera3DAction, self).stop()
 
74
        self.target.camera.eye = self.camera_eye_orig
 
75
        self.target.camera.center = self.camera_center_orig
 
76
        self.target.camera.up_vector = self.camera_up_orig
 
77
 
 
78
    def __reversed__( self ):
 
79
        return _ReverseTime( self )
 
80
 
 
81
 
 
82
class OrbitCamera( Camera3DAction ):
 
83
    '''Orbits the camera around the center of the screen using spherical coordinates
 
84
    '''
 
85
    def init( self, radius=1, delta_radius=0, angle_z=0, delta_z=0, angle_x=0, delta_x=0, *args, **kw ):
 
86
        '''Initialize the camera with spherical coordinates
 
87
 
 
88
        :Parameters:
 
89
            `radius` : float
 
90
                Radius of the orbit. Default: best distance for the current fov
 
91
            `delta_radius` : float
 
92
                Radius of the orbit. Default: best distance for the current fov
 
93
            `angle_z` : float
 
94
                The zenith angle of the spherical coordinate in degrees. Default: 0
 
95
            `delta_z` : float
 
96
                Relative movement of the zenith angle. Default: 0
 
97
            `angle_x` : float
 
98
                The azimuth angle of the spherical coordinate in degrees. Default: 0
 
99
            `delta_x` : float
 
100
                Relative movement of the azimuth angle. Default: 0
 
101
                
 
102
 
 
103
        For more information regarding spherical coordinates, read this:
 
104
            http://en.wikipedia.org/wiki/Spherical_coordinates
 
105
 
 
106
        '''
 
107
        super( OrbitCamera, self ).init( *args, **kw )
 
108
 
 
109
        width, height = director.get_window_size()
 
110
 
 
111
        self.radius = radius
 
112
        self.delta_radius = delta_radius
 
113
        self.rad_x = math.radians( angle_x )
 
114
        self.rad_z = math.radians( angle_z )
 
115
        self.rad_delta_x= math.radians( delta_x )
 
116
        self.rad_delta_z = math.radians( delta_z )
 
117
 
 
118
    def update( self, t ):
 
119
        r = (self.radius + self.delta_radius * t) * self.target.camera.get_z_eye()
 
120
        z_angle = self.rad_z + self.rad_delta_z * t
 
121
        x_angle = self.rad_x + self.rad_delta_x * t
 
122
 
 
123
        # using spherical coordinates
 
124
        p = Point3( math.sin(z_angle) * math.cos(x_angle),
 
125
                    math.sin(z_angle) * math.sin(x_angle),
 
126
                    math.cos(z_angle) )
 
127
 
 
128
        p = p * r
 
129
        d = p + self.camera_center_orig
 
130
        self.target.camera.eye = d