Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7786787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:28:46+00:00 2026-06-01T20:28:46+00:00

I am in process of developing a small game where a space-ship travels through

  • 0

I am in process of developing a small game where a space-ship travels through a layer (doh!), in some situations the spaceship comes close to an enemy, and the whole layer is zoomed in on the space-ship with the zoom level being dependent on the distance between the ship and the enemy. All of this works fine.

The main question, however, is how do I keep the zoom being centered on the space-ship?

Currently I control the zooming in the GameLayer object through the update method, here is the code:

-(void) prepareLayerZoomBetweenSpaceship{
    CGPoint mainSpaceShipPosition  = [mainSpaceShip position];
    CGPoint enemySpaceShipPosition = [enemySpaceShip position];

    float distance = powf(mainSpaceShipPosition.x - enemySpaceShipPosition.x, 2) + powf(mainSpaceShipPosition.y - enemySpaceShipPosition.y,2);
    distance = sqrtf(distance);

    /*
        Distance > 250 --> no zoom
        Distance < 100 --> maximum zoom
     */
    float myZoomLevel = 0.5f;
    if(distance < 100){ //maximum zoom in
        myZoomLevel = 1.0f;
    }else if(distance > 250){
        myZoomLevel = 0.5f;
    }else{
        myZoomLevel = 1.0f - (distance-100)*0.0033f;
    }

    [self zoomTo:myZoomLevel];
}

-(void) zoomTo:(float)zoom {
    if(zoom > 1){
        zoom = 1;
    }

    // Set the scale.
    if(self.scale != zoom){
        self.scale = zoom;
    }
}

Basically my question is: How do I zoom the layer and center it exactly between the two ships? I guess this is like a pinch zoom with two fingers!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:28:48+00:00Added an answer on June 1, 2026 at 8:28 pm

    Below is some code that should get it working for you. Basically you want to:

    1. Update your ship positions within the parentNode‘s coordinate system
    2. Figure out which axis these new positions will cause the screen will be bound by.
    3. Scale and re-position the parentNode

    I added some sparse comments, but let me know if you have any more questions/issues. It might be easiest to dump this in a test project first…

    ivars to put in your CCLayer:

    CCNode *parentNode;
    CCSprite *shipA;
    CCSprite *shipB;
    CGPoint destA, deltaA;
    CGPoint destB, deltaB;
    CGPoint halfScreenSize;
    CGPoint fullScreenSize;
    

    init stuff to put in your CCLayer:

    CGSize size = [[CCDirector sharedDirector] winSize];
    fullScreenSize = CGPointMake(size.width, size.height);
    halfScreenSize = ccpMult(fullScreenSize, .5f);
    parentNode = [CCNode node];
    [self addChild:parentNode];
    
    shipA = [CCSprite spriteWithFile:@"Icon-Small.png"]; //or whatever sprite
    [parentNode addChild:shipA];
    
    shipB = [CCSprite spriteWithFile:@"Icon-Small.png"];
    [parentNode addChild:shipB];
    
    //schedules update for every frame... might not run great.
    //[self schedule:@selector(updateShips:)]; 
    
    //schedules update for 25 times a second
    [self schedule:@selector(updateShips:) interval:0.04f]; 
    

    Zoom / Center / Ship update method:

    -(void)updateShips:(ccTime)timeDelta {
        //SHIP POSITION UPDATE STUFF GOES HERE
        ...
    
        //1st: calc aspect ratio formed by ship positions to determine bounding axis
        float shipDeltaX = fabs(shipA.position.x - shipB.position.x);
        float shipDeltaY = fabs(shipA.position.y - shipB.position.y);
        float newAspect = shipDeltaX / shipDeltaY;
    
        //Then: scale based off of bounding axis
    
        //if bound by x-axis OR deltaY is negligible
        if (newAspect > (fullScreenSize.x / fullScreenSize.y) || shipDeltaY < 1.0) { 
            parentNode.scale = fullScreenSize.x / (shipDeltaX + shipA.contentSize.width);
        } 
        else { //else: bound by y-axis or deltaX is negligible
            parentNode.scale = fullScreenSize.y / (shipDeltaY + shipA.contentSize.height);
        }
    
    
        //calculate new midpoint between ships AND apply new scale to it
        CGPoint scaledMidpoint = ccpMult(ccpMidpoint(shipA.position, shipB.position), parentNode.scale);
        //update parent node position (move it into view of screen) to scaledMidpoint
        parentNode.position = ccpSub(halfScreenSize, scaledMidpoint);
    }
    

    Also, I’m not sure how well it’ll perform with a bunch of stuff going on — but thats a separate problem!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am in the process of developing a small game in DirectX 10 and
I'm developing a small TCP server, which will process some TCP packets and behave
I'm currently in the process of developing an app which has some very demanding
I am developing a small game (in Java) for a coursework and the extension
I am currently developing a small form for an order process. The form has
I'm in the process of developing an android game. I have an activity that
In process of developing I often face with the next problem: if some method
we are in process of developing google chrome extension, in which we need to
We are in process of developing an Java based CMS related to travelling domain
I'm in the process of developing a Silverlight custom control that hosts a Flash

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.