在cocos2d和chipmunk中从CGRect创建cpShape

发布于 2024-12-13 18:39:29 字数 510 浏览 0 评论 0原文

我不知道如何创建适合 CGRect 的 cpShape。这是我到目前为止所尝试的,但没有成功:

    CGPoint p1 = rect.origin;
    CGPoint p2 = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y);
    CGPoint p3 = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
    CGPoint p4 = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);

    int num = 4;
    CGPoint verts[] = {p2, p1, p3, p4};
    shape = cpPolyShapeNew(body, num, verts, cpvzero);

我不明白为什么这不完全适合我的 CGRect ?

I can't figure out how to create a cpShape that will fit a CGRect. Here is what I tried so far, without any success :

    CGPoint p1 = rect.origin;
    CGPoint p2 = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y);
    CGPoint p3 = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
    CGPoint p4 = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);

    int num = 4;
    CGPoint verts[] = {p2, p1, p3, p4};
    shape = cpPolyShapeNew(body, num, verts, cpvzero);

I don't understand why this doesn't fit perfectly my CGRect ?

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

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

发布评论

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

评论(1

北座城市 2024-12-20 18:39:29

代表user251552回答

好的我明白了:

//Converting points in chipmunk coordinates
    CGPoint p1 = CGPointMake(-rect.size.width/2, -rect.size.height/2);
    CGPoint p2 = CGPointMake(-rect.size.width/2, +rect.size.height/2);
    CGPoint p3 = CGPointMake(+rect.size.width/2, +rect.size.height/2);
    CGPoint p4 = CGPointMake(+rect.size.width/2, -rect.size.height/2);

    int num = 4;
    CGPoint verts[] = {p1, p2, p3, p4};

    shape = cpPolyShapeNew(body, num, verts, cpvzero);

Answered on behalf of user251552

Ok I figured this out:

//Converting points in chipmunk coordinates
    CGPoint p1 = CGPointMake(-rect.size.width/2, -rect.size.height/2);
    CGPoint p2 = CGPointMake(-rect.size.width/2, +rect.size.height/2);
    CGPoint p3 = CGPointMake(+rect.size.width/2, +rect.size.height/2);
    CGPoint p4 = CGPointMake(+rect.size.width/2, -rect.size.height/2);

    int num = 4;
    CGPoint verts[] = {p1, p2, p3, p4};

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