diff options
Diffstat (limited to 'libs/cocos2d/CCActionCamera.m')
| -rwxr-xr-x | libs/cocos2d/CCActionCamera.m | 147 |
1 files changed, 147 insertions, 0 deletions
| diff --git a/libs/cocos2d/CCActionCamera.m b/libs/cocos2d/CCActionCamera.m new file mode 100755 index 0000000..4dafc4e --- /dev/null +++ b/libs/cocos2d/CCActionCamera.m | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | /* | ||
| 2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
| 3 | * | ||
| 4 | * Copyright (c) 2008-2010 Ricardo Quesada | ||
| 5 | * Copyright (c) 2011 Zynga Inc. | ||
| 6 | * | ||
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 8 | * of this software and associated documentation files (the "Software"), to deal | ||
| 9 | * in the Software without restriction, including without limitation the rights | ||
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 11 | * copies of the Software, and to permit persons to whom the Software is | ||
| 12 | * furnished to do so, subject to the following conditions: | ||
| 13 | * | ||
| 14 | * The above copyright notice and this permission notice shall be included in | ||
| 15 | * all copies or substantial portions of the Software. | ||
| 16 | * | ||
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 23 | * THE SOFTWARE. | ||
| 24 | * | ||
| 25 | */ | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | #import "CCActionCamera.h" | ||
| 30 | #import "CCNode.h" | ||
| 31 | #import "CCCamera.h" | ||
| 32 | #import "ccMacros.h" | ||
| 33 | |||
| 34 | // | ||
| 35 | // CameraAction | ||
| 36 | // | ||
| 37 | @implementation CCActionCamera | ||
| 38 | -(void) startWithTarget:(id)aTarget | ||
| 39 | { | ||
| 40 | [super startWithTarget:aTarget]; | ||
| 41 | CCCamera *camera = [target_ camera]; | ||
| 42 | [camera centerX:¢erXOrig_ centerY:¢erYOrig_ centerZ:¢erZOrig_]; | ||
| 43 | [camera eyeX:&eyeXOrig_ eyeY:&eyeYOrig_ eyeZ:&eyeZOrig_]; | ||
| 44 | [camera upX:&upXOrig_ upY:&upYOrig_ upZ: &upZOrig_]; | ||
| 45 | } | ||
| 46 | |||
| 47 | -(id) reverse | ||
| 48 | { | ||
| 49 | return [CCReverseTime actionWithAction:self]; | ||
| 50 | } | ||
| 51 | @end | ||
| 52 | |||
| 53 | @implementation CCOrbitCamera | ||
| 54 | +(id) actionWithDuration:(float)t radius:(float)r deltaRadius:(float) dr angleZ:(float)z deltaAngleZ:(float)dz angleX:(float)x deltaAngleX:(float)dx | ||
| 55 | { | ||
| 56 | return [[[self alloc] initWithDuration:t radius:r deltaRadius:dr angleZ:z deltaAngleZ:dz angleX:x deltaAngleX:dx] autorelease]; | ||
| 57 | } | ||
| 58 | |||
| 59 | -(id) copyWithZone: (NSZone*) zone | ||
| 60 | { | ||
| 61 | return [[[self class] allocWithZone: zone] initWithDuration:duration_ radius:radius_ deltaRadius:deltaRadius_ angleZ:angleZ_ deltaAngleZ:deltaAngleZ_ angleX:angleX_ deltaAngleX:deltaAngleX_]; | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | -(id) initWithDuration:(float)t radius:(float)r deltaRadius:(float) dr angleZ:(float)z deltaAngleZ:(float)dz angleX:(float)x deltaAngleX:(float)dx | ||
| 66 | { | ||
| 67 | if((self=[super initWithDuration:t]) ) { | ||
| 68 | |||
| 69 | radius_ = r; | ||
| 70 | deltaRadius_ = dr; | ||
| 71 | angleZ_ = z; | ||
| 72 | deltaAngleZ_ = dz; | ||
| 73 | angleX_ = x; | ||
| 74 | deltaAngleX_ = dx; | ||
| 75 | |||
| 76 | radDeltaZ_ = (CGFloat)CC_DEGREES_TO_RADIANS(dz); | ||
| 77 | radDeltaX_ = (CGFloat)CC_DEGREES_TO_RADIANS(dx); | ||
| 78 | } | ||
| 79 | |||
| 80 | return self; | ||
| 81 | } | ||
| 82 | |||
| 83 | -(void) startWithTarget:(id)aTarget | ||
| 84 | { | ||
| 85 | [super startWithTarget:aTarget]; | ||
| 86 | float r, zenith, azimuth; | ||
| 87 | |||
| 88 | [self sphericalRadius: &r zenith:&zenith azimuth:&azimuth]; | ||
| 89 | |||
| 90 | #if 0 // isnan() is not supported on the simulator, and isnan() always returns false. | ||
| 91 | if( isnan(radius_) ) | ||
| 92 | radius_ = r; | ||
| 93 | |||
| 94 | if( isnan( angleZ_) ) | ||
| 95 | angleZ_ = (CGFloat)CC_RADIANS_TO_DEGREES(zenith); | ||
| 96 | |||
| 97 | if( isnan( angleX_ ) ) | ||
| 98 | angleX_ = (CGFloat)CC_RADIANS_TO_DEGREES(azimuth); | ||
| 99 | #endif | ||
| 100 | |||
| 101 | radZ_ = (CGFloat)CC_DEGREES_TO_RADIANS(angleZ_); | ||
| 102 | radX_ = (CGFloat)CC_DEGREES_TO_RADIANS(angleX_); | ||
| 103 | } | ||
| 104 | |||
| 105 | -(void) update: (ccTime) dt | ||
| 106 | { | ||
| 107 | float r = (radius_ + deltaRadius_ * dt) *[CCCamera getZEye]; | ||
| 108 | float za = radZ_ + radDeltaZ_ * dt; | ||
| 109 | float xa = radX_ + radDeltaX_ * dt; | ||
| 110 | |||
| 111 | float i = sinf(za) * cosf(xa) * r + centerXOrig_; | ||
| 112 | float j = sinf(za) * sinf(xa) * r + centerYOrig_; | ||
| 113 | float k = cosf(za) * r + centerZOrig_; | ||
| 114 | |||
| 115 | [[target_ camera] setEyeX:i eyeY:j eyeZ:k]; | ||
| 116 | } | ||
| 117 | |||
| 118 | -(void) sphericalRadius:(float*) newRadius zenith:(float*) zenith azimuth:(float*) azimuth | ||
| 119 | { | ||
| 120 | float ex, ey, ez, cx, cy, cz, x, y, z; | ||
| 121 | float r; // radius | ||
| 122 | float s; | ||
| 123 | |||
| 124 | CCCamera *camera = [target_ camera]; | ||
| 125 | [camera eyeX:&ex eyeY:&ey eyeZ:&ez]; | ||
| 126 | [camera centerX:&cx centerY:&cy centerZ:&cz]; | ||
| 127 | |||
| 128 | x = ex-cx; | ||
| 129 | y = ey-cy; | ||
| 130 | z = ez-cz; | ||
| 131 | |||
| 132 | r = sqrtf( x*x + y*y + z*z); | ||
| 133 | s = sqrtf( x*x + y*y); | ||
| 134 | if(s==0.0f) | ||
| 135 | s = FLT_EPSILON; | ||
| 136 | if(r==0.0f) | ||
| 137 | r = FLT_EPSILON; | ||
| 138 | |||
| 139 | *zenith = acosf( z/r); | ||
| 140 | if( x < 0 ) | ||
| 141 | *azimuth = (float)M_PI - asinf(y/s); | ||
| 142 | else | ||
| 143 | *azimuth = asinf(y/s); | ||
| 144 | |||
| 145 | *newRadius = r / [CCCamera getZEye]; | ||
| 146 | } | ||
| 147 | @end | ||
