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 7412405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:32:04+00:00 2026-05-29T06:32:04+00:00

I recently started game programming on the iPhone using Cocos2d and Box2d. So here’s

  • 0

I recently started game programming on the iPhone using Cocos2d and Box2d. So here’s my problem:

I’ve got a Player class which inherits from CCSprite, and within that class, there’s this method:

-(void) createBox2dObject:(Player *)sender 
             forWorld:(b2World*)world {

b2BodyDef playerBodyDef;
playerBodyDef.type = b2_dynamicBody;
playerBodyDef.position.Set(sender.position.x/PTM_RATIO, sender.position.y/PTM_RATIO);
playerBodyDef.userData = sender;
body = world->CreateBody(&playerBodyDef);

b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(sender.contentSize.width/PTM_RATIO,
                    sender.contentSize.height/PTM_RATIO);
b2FixtureDef polygonShapeDef;
polygonShapeDef.shape = &dynamicBox;
polygonShapeDef.density = 1.0f;
polygonShapeDef.friction = 1.0f;
polygonShapeDef.restitution = 0;
body->CreateFixture(&polygonShapeDef);
}

and here’s how I call this:

    self.player = [Player spriteWithSpriteFrameName:@"runningrupol-1.png"];        
    _player.position = ccp(_player.boundingBox.size.width/2 + 32, _player.boundingBox.size.height/2 + 32);
    self.walkAction = [CCRepeatForever actionWithAction:
                       [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
    [_player runAction:_walkAction];
    [spriteSheet addChild:_player];
    [_player createBox2dObject:_player forWorld:_world];

Obviously, I’m using a spritesheet which is animated.

Here’s how I update the world:

- (void)tick:(ccTime) dt {

    _world->Step(dt, 8, 10);

    for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {    
        if (b->GetUserData() != NULL) {
            CCSprite *playerData = (CCSprite *)b->GetUserData();
            playerData.position = ccp(b->GetPosition().x * PTM_RATIO,
                                    b->GetPosition().y * PTM_RATIO);
            playerData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
        }        
    }

}

And here’s how I call it in the init method:

[self schedule:@selector(tick:)];

This is what I see:

enter image description here

Please help. And if you need additional info, just tell me.

  • 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-05-29T06:32:06+00:00Added an answer on May 29, 2026 at 6:32 am

    SetAsBox() uses half-height and half-width (quirky I know), so divide the parameters by 2:

    dynamicBox.SetAsBox(sender.contentSize.width/PTM_RATIO/2,
                    sender.contentSize.height/PTM_RATIO/2);
    

    When you try this, leave the anchor point as is (the default value if you don’t explicitly set it should be ccp(0.5,0.5), the center of your sprite, and you want that).

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

Sidebar

Related Questions

I recently started on creating a 'game' using PHP and a mysql database. In
I've recently started learning programming to make my own 3D OpenGL game on the
I recently started work on a personal coding project using C++ and KDevelop. Although
I recently started using Eclipse at work for my Java servlet projects. I've been
We recently started using maven for dependency management. Our team uses eclipse as it's
I recently started using scons to build several small cross-platform projects. One of these
I recently started using Git as my version control system for some Cocoa projects
I recently started using Linux as my primary OS. What are the tools that
I recently started diving into low level OS programming. I am (very slowly) currently
Recently started programming Android Java (Eclipse), Im trying to make a simple reader app

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.