box2d 与曲线碰撞

发布于 2024-10-05 22:38:49 字数 180 浏览 2 评论 0原文

大家好 我正在使用 cocos2d Objective C 和 box2d 开发 iPad 应用程序。我的问题与 box2d 和曲线碰撞有关。基本上我在一个更大的圆形竞技场中有圆圈。较小的圆圈彼此碰撞得很好,但我想知道如何使它们正确地与较大竞技场圆圈的边缘碰撞,这样它们就不会离开竞技场并反弹回来。任何关于如何解决这个问题的想法都会很棒 干杯

Hi all
im working on an ipad app using cocos2d objective c and box2d. my question is in relation to box2d and collisions with curves. basically i have circles within a larger circle arena. the smaller circles collide with each other fine but i want to know how to make them collide with the edges of the larger arena circle correctly so that they dont leave the arena and bounce back in. any ideas on how to approach this problem would be great
cheers

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

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

发布评论

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

评论(2

涙—继续流 2024-10-12 22:38:49

好的,我成功了,但我不确定它是否是正确的方法,

int sides = 50;

    float x_coordPREV=([Arena contentSize].width/2)*cos(0.0);
    float y_coordPREV=([Arena contentSize].width/2)*sin(0.0);
    x_coordPREV += Arena.position.x;
    y_coordPREV += Arena.position.y;

    b2BodyDef arenaBodyDef;
    arenaBodyDef.position.Set(0,0);
    b2Body *arenaBody = _world->CreateBody(&arenaBodyDef);
    b2PolygonShape arenaBox;
    b2FixtureDef arenaShapeDef;
    arenaShapeDef.shape = &arenaBox;

    for(float angle=0.0f;angle<(2*M_PI);angle+=(2*M_PI)/sides){
        float x_coord=([Arena contentSize].width/2-10)*cos(angle);
        float y_coord=([Arena contentSize].width/2-10)*sin(angle);
        x_coord += Arena.position.x;
        y_coord += Arena.position.y;
        arenaBox.SetAsEdge(b2Vec2(x_coordPREV/PTM_RATIO,y_coordPREV/PTM_RATIO),b2Vec2( x_coord/PTM_RATIO,y_coord/PTM_RATIO ));
        arenaBody->CreateFixture(&arenaShapeDef);
        x_coordPREV = x_coord;
        y_coordPREV = y_coord;
    }

这会创建一个 50 条边的圆,并且似乎正在做我想要的事情。如果有人有更好的方法请告诉我

ok i got it working but im not sure if its the correct way to do it

int sides = 50;

    float x_coordPREV=([Arena contentSize].width/2)*cos(0.0);
    float y_coordPREV=([Arena contentSize].width/2)*sin(0.0);
    x_coordPREV += Arena.position.x;
    y_coordPREV += Arena.position.y;

    b2BodyDef arenaBodyDef;
    arenaBodyDef.position.Set(0,0);
    b2Body *arenaBody = _world->CreateBody(&arenaBodyDef);
    b2PolygonShape arenaBox;
    b2FixtureDef arenaShapeDef;
    arenaShapeDef.shape = &arenaBox;

    for(float angle=0.0f;angle<(2*M_PI);angle+=(2*M_PI)/sides){
        float x_coord=([Arena contentSize].width/2-10)*cos(angle);
        float y_coord=([Arena contentSize].width/2-10)*sin(angle);
        x_coord += Arena.position.x;
        y_coord += Arena.position.y;
        arenaBox.SetAsEdge(b2Vec2(x_coordPREV/PTM_RATIO,y_coordPREV/PTM_RATIO),b2Vec2( x_coord/PTM_RATIO,y_coord/PTM_RATIO ));
        arenaBody->CreateFixture(&arenaShapeDef);
        x_coordPREV = x_coord;
        y_coordPREV = y_coord;
    }

this creates a circle out of 50 sides and appears to be doing what i want. if anyone has a better way please let me know
ty

好菇凉咱不稀罕他 2024-10-12 22:38:49

不确定这是什么,但我在论坛上看到了这个:

b2BodyDef edgedef;
edgedef.position.Set(0.f,10.f);
b2Body* edge = world->CreateBody(&edgedef);

b2Vec2 vertices[2];
vertices[0].Set(0.0f, 0.0f);
vertices[1].Set(10.0f, 0.0f);
int32 count = 2;

b2PolygonShape polygon;
polygon.Set(vertices, count);

b2FixtureDef edgefixtureDef;
edgefixtureDef.shape = &polygon;

edge->CreateFixture(&edgefixtureDef);

您可以看到如何通过发送更大的数组来添加更多点

not sur eif this i what you are looking for but i saw this on a forum:

b2BodyDef edgedef;
edgedef.position.Set(0.f,10.f);
b2Body* edge = world->CreateBody(&edgedef);

b2Vec2 vertices[2];
vertices[0].Set(0.0f, 0.0f);
vertices[1].Set(10.0f, 0.0f);
int32 count = 2;

b2PolygonShape polygon;
polygon.Set(vertices, count);

b2FixtureDef edgefixtureDef;
edgefixtureDef.shape = &polygon;

edge->CreateFixture(&edgefixtureDef);

you could see how you could add more points by sending a larger array

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