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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:02:43+00:00 2026-05-26T18:02:43+00:00

I am using Box2D with Cocos2D in my game. I am aware that Cocos2D

  • 0

I am using Box2D with Cocos2D in my game. I am aware that Cocos2D has a animation API named CCMoveTo. I want to do the same thing in Box2D with b2Bodies. I want to be able to set a destination point and a animation time from the point it is initially at. Does anyone know if this is possible or not?
I am just trying to keep this as simple as possible.

I am open to ideas and suggestions!

Thanks!

Edit1: Ok so I want to make my b2Bodys following my CCSprite permanently.
There is a problem with the code you showed me.

In my .h I am doing this:

b2World *world;
b2Body *body;

Then I modified the method a bit and now it looks like this:

for (body = world->GetBodyList(); body != nil; body = body->GetNext())
    {
        CCSprite *bodyNode = body->GetUserData();
        if (bodyNode != nil)
        {
            // this transfers the body's position and rotation to the sprite
            bodyNode.position = [Helper toPixels:body->GetPosition()];
            float angle = body->GetAngle();
            bodyNode.rotation = -(CC_RADIANS_TO_DEGREES(angle));

            // and this would do the exact opposite
            b2Vec2 pos = [Helper toMeters:bodyNode.position];
            body->SetTransform(pos, CC_DEGREES_TO_RADIANS(bodyNode.rotation));
            body->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
            body->SetAngularVelocity(0.0f);
        }
    }

So I used the ivars instead of the instance variable. Is that OK?
Also I get 2 errors:

  1. I get Cannot Initialize variable of type ‘CCSprite’ with an value of type ‘void’ in the body->GetUserData line

  2. I also get: Use of undeclared identifier ‘Helper’. I put those 2 helper methods into my class but I am just not sure what Helper is.

Edit2: How does this look? I decided to keep the methods in my class and not create a new one specifically for it.

    // for each body, get its assigned BodyNode and update the sprite's position
    for (body = world->GetBodyList(); body != nil; body = body->GetNext())
    {
        CCSprite* sprite = (CCSprite*)body->GetUserData();
        if (sprite != nil)
        {
            // this transfers the body's position and rotation to the sprite
            sprite.position = [self toPixels:body->GetPosition()];
            float angle = body->GetAngle();
            sprite.rotation = -(CC_RADIANS_TO_DEGREES(angle));

            // and this would do the exact opposite
            b2Vec2 pos = [self toMeters:sprite.position];
            body->SetTransform(pos, CC_DEGREES_TO_RADIANS(sprite.rotation));
            body->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
            body->SetAngularVelocity(0.0f);
        }
    }
}

// convenience method to convert a CGPoint to a b2Vec2
-(b2Vec2)toMeters:(CGPoint)point
{
    return b2Vec2(point.x / CTM_RATIO, point.y / CTM_RATIO);
}

// convenience method to convert a b2Vec2 to a CGPoint
-(CGPoint)toPixels:(b2Vec2)vec
{
    return ccpMult(CGPointMake(vec.x, vec.y), CTM_RATIO);
}
  • 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-26T18:02:43+00:00Added an answer on May 26, 2026 at 6:02 pm

    You can reverse the relationship of CCSprite and b2Body, either temporarily or permanently. Normally in your update method the CCSprite will be set to the position of the b2Body. If you have a CCSprite that uses CCMoveTo to move to a specific position, you could instead assign the b2Body the position and rotation of the sprite.

    All you need to do is to determine when and which sprite to control the body:

    // for each body, get its assigned BodyNode and update the sprite's position
    for (b2Body* body = world->GetBodyList(); body != nil; body = body->GetNext())
    {
        BodyNode* bodyNode = body->GetUserData();
        if (bodyNode != nil)
        {
            // this transfers the body's position and rotation to the sprite
            bodyNode.position = [Helper toPixels:body->GetPosition()];
            float angle = body->GetAngle();
            bodyNode.rotation = -(CC_RADIANS_TO_DEGREES(angle));
    
            // and this would do the exact opposite
            b2Vec2 pos = [Helper toMeters:bodyNode.position];
            body->SetTransform(pos, CC_DEGREES_TO_RADIANS(bodyNode.rotation));
            body->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
            body->SetAngularVelocity(0.0f);
        }
    }
    

    The Helper methods are defined as follows:

    // convenience method to convert a CGPoint to a b2Vec2
    +(b2Vec2) toMeters:(CGPoint)point
    {
        return b2Vec2(point.x / PTM_RATIO, point.y / PTM_RATIO);
    }
    
    // convenience method to convert a b2Vec2 to a CGPoint
    +(CGPoint) toPixels:(b2Vec2)vec
    {
        return ccpMult(CGPointMake(vec.x, vec.y), PTM_RATIO);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently started game programming on the iPhone using Cocos2d and Box2d. So here's
I'm working on a platformer for the iPhone that is using Box2D and cocos2D.
I am using box2d and cocos2d for my jump based game. I need to
I am using Box2d and Cocos2d to develop an iPhone game. I have a
im using xcode4 and box2d/cocos2d. Im having some strange difficulties with runtime errors that
Hey, Im using cocos2d and box2d and I want to turn off the wireframe
I make simple game using Box2D. I want to add new feature of Black
I am writing an iphone game using cocos2d and box2d engines. I have a
I am currently making a game for the iPad and iPhone using cocos2d, Box2D
I'm using Box2d for a Bike Physics Game, Box2d lets you have fixtures that

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.