rptwsthi
rptwsthi

Reputation: 10182

Placing camera with animation

I want to know that how to place camera near and far (By increasing and decreasing value of eyeZ self.camera setEyeX:0 eyeY:0 eyeZ:1]; self.camera setEyeX:0 eyeY:0 eyeZ:180];) including animation (for smoothness), as normally it provide jerky zooming.

Upvotes: 3

Views: 497

Answers (2)

sergio
sergio

Reputation: 69047

My suggestion is creating your own subclass of CCActionInterval, say CCCameraZoomAnimation and override its update method. The main advantage of having an action, aside from being able to control the camera movement finely, is also the possibility of using this action through CCEaseOut/CCEaseIn (etc.) to obtain nice graphical effects.

CCCameraZoomAnimation would have the node whose camera you want to modify as a target and another parameter to the constructor specifying the final Z value.

 @interface CCActionEase : CCActionInterval <NSCopying>
 {
       CCActionInterval * other;
 }
 /** creates the action */
 +(id) actionWithDuration:(ccTime)t finalZ:(float)finalZ;
 /** initializes the action */
 -(id) initWithDuration:(ccTime)t finalZ:(float)finalZ;
 @end

The update method is called with an argument dt which represents the elapsed time since the start of the action and would allow you to easily calculate the current Z position:

 -(void) update: (ccTime) t
 {
    // Get the camera's current values.
    float centerX, centerY, centerZ;
    float eyeX, eyeY, eyeZ;
    [_target.camera centerX:&centerX centerY:&centerY centerZ:&centerZ];
    [_target.camera eyeX:&eyeX eyeY:&eyeY eyeZ:&eyeZ];

    eyeZ = _intialZ + _delta * t  //-- just a try at modifying the camera

    // Set values.
    [_target.camera setCenterX:newX centerY:newY centerZ:0];
    [_target.camera setEyeX:newX eyeY:newY eyeZ:eyeZ];
 }

You would also need to implement copyWithZone:

 -(id) copyWithZone: (NSZone*) zone
 {
      CCAction *copy = [[[self class] allocWithZone: zone] initWithDuration: [self duration] finalZ:_finalZ];
      return copy;
  }

and make use of startWithTarget to

  -(void) startWithTarget:(CCNode *)aTarget
  {
      [super startWithTarget:aTarget];
      _initialZ = _target.camera....;  //-- get the current value for eyeZ
      _delta = ccpSub( _finalZ, _initialZ );
  }

Nothing more, nothing less.

Sorry if copy/paste/modify produced some bugs, but I hope that the general idea is clear.

Upvotes: 2

theiOSDude
theiOSDude

Reputation: 1470

if you increase the 'z' by 180 from the off, you are bound to get a jerky animation, try running this in an animation context loop,increasing the value over a period of time will allow you to have a smooth 'zoom'.

Upvotes: 0

Related Questions