使用 Box2D 进行角色跳跃

发布于 2024-11-18 06:54:06 字数 91 浏览 3 评论 0原文

我正在使用 box2d 和 cocos2d 来玩我的跳跃游戏。我需要在平台到来时将角色跳跃到平台上。

关于如何使用 box2d 做到这一点有什么想法吗?

I am using box2d and cocos2d for my jump based game. I need to jump the character on platforms as they come.

Any ideas of how to do it using box2d ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠意 2024-11-25 06:54:07

你试试这个:

//OBSTACLE_1:
    {
        CCSprite *sprite = [CCSprite spriteWithFile:@"hungObstacle1_C2H18.png"];
        sprite.position = ccp(1568,704);
        sprite.tag = 4;
        [self addChild:sprite];

        //SHAPE :
        {
            b2BodyDef bodyDefinition;
            bodyDefinition.type = b2_dynamicBody;
            bodyDefinition.position.Set(1568/PTM_RATIO, 704/PTM_RATIO);
            bodyDefinition.userData = sprite;

            body = world->CreateBody(&bodyDefinition);//body is a b2Body

            // Create body shape
            b2PolygonShape bodyShape;
        //row 1, col 1
        int num = 8;
        b2Vec2 verts[] = {
            b2Vec2(14.5f / PTM_RATIO, -27.0f / PTM_RATIO),
            b2Vec2(19.5f / PTM_RATIO, -11.0f / PTM_RATIO),
            b2Vec2(19.5f / PTM_RATIO, 11.0f / PTM_RATIO),
            b2Vec2(16.5f / PTM_RATIO, 24.0f / PTM_RATIO),
            b2Vec2(-14.5f / PTM_RATIO, 24.0f / PTM_RATIO),
            b2Vec2(-19.5f / PTM_RATIO, 16.0f / PTM_RATIO),
            b2Vec2(-19.5f / PTM_RATIO, -13.0f / PTM_RATIO),
            b2Vec2(-12.5f / PTM_RATIO, -26.0f / PTM_RATIO)
        };
        bodyShape.Set(verts, num);

            // Create shape definition and add to body
            b2FixtureDef bodyFixtureDefinition;
            bodyFixtureDefinition.shape = &bodyShape;
            bodyFixtureDefinition.density = 5000.0f;
            bodyFixtureDefinition.friction = 1.0f;
            bodyFixtureDefinition.restitution = 1.0f;//*        
            fixture=body->CreateFixture(&bodyFixtureDefinition);            
        }           
    }

*注意:通过将恢复设置为1,身体将一直保持跳跃,只需要很少的力量

You try this:

//OBSTACLE_1:
    {
        CCSprite *sprite = [CCSprite spriteWithFile:@"hungObstacle1_C2H18.png"];
        sprite.position = ccp(1568,704);
        sprite.tag = 4;
        [self addChild:sprite];

        //SHAPE :
        {
            b2BodyDef bodyDefinition;
            bodyDefinition.type = b2_dynamicBody;
            bodyDefinition.position.Set(1568/PTM_RATIO, 704/PTM_RATIO);
            bodyDefinition.userData = sprite;

            body = world->CreateBody(&bodyDefinition);//body is a b2Body

            // Create body shape
            b2PolygonShape bodyShape;
        //row 1, col 1
        int num = 8;
        b2Vec2 verts[] = {
            b2Vec2(14.5f / PTM_RATIO, -27.0f / PTM_RATIO),
            b2Vec2(19.5f / PTM_RATIO, -11.0f / PTM_RATIO),
            b2Vec2(19.5f / PTM_RATIO, 11.0f / PTM_RATIO),
            b2Vec2(16.5f / PTM_RATIO, 24.0f / PTM_RATIO),
            b2Vec2(-14.5f / PTM_RATIO, 24.0f / PTM_RATIO),
            b2Vec2(-19.5f / PTM_RATIO, 16.0f / PTM_RATIO),
            b2Vec2(-19.5f / PTM_RATIO, -13.0f / PTM_RATIO),
            b2Vec2(-12.5f / PTM_RATIO, -26.0f / PTM_RATIO)
        };
        bodyShape.Set(verts, num);

            // Create shape definition and add to body
            b2FixtureDef bodyFixtureDefinition;
            bodyFixtureDefinition.shape = &bodyShape;
            bodyFixtureDefinition.density = 5000.0f;
            bodyFixtureDefinition.friction = 1.0f;
            bodyFixtureDefinition.restitution = 1.0f;//*        
            fixture=body->CreateFixture(&bodyFixtureDefinition);            
        }           
    }

*NOTE:by setting restitution to 1 body will keep jumping all the time just need little force

薯片软お妹 2024-11-25 06:54:07

要模拟跳跃,您可以使用 cpBodyApplyImpulse 方法并沿直线向上方向施加力。假设您启用了重力,这应该提供准确的模拟,尽管您可能需要对其进行调整。例如,这是我最近的一个项目中的一行。

cpBodyApplyImpulse(charBody, cpv(0.0, -25 * scale), charBody->p);

这使得 charBody 代表的精灵跳跃。

To simulate jumping, you can use the cpBodyApplyImpulse method and apply a force in a straight up direction. Assuming you've got gravity enabled, this should provide an accuracte simulation, although you may need to tweak it. For example, here is a line from a recent project of mine.

cpBodyApplyImpulse(charBody, cpv(0.0, -25 * scale), charBody->p);

This is making the sprite represented by the charBody jump.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文