将多个碰撞形状附加到单个刚体

发布于 2024-11-24 00:20:00 字数 523 浏览 2 评论 0原文

我使用 iOS + Cocos2d + Chipmunk 创建了一个单人游戏,我正在寻找一个解决方案来演示如何将多个碰撞形状附加到单个刚体。我有一个形状不规则的目标(汽车),我需要检测其碰撞。玩家从侧视图看到目标(汽车),其他物体可能会从多个方向撞击目标,而不仅仅是从前面或后面。由于形状如此,我无法使用单个 cpPolyShape 来实现逼真的碰撞效果。两个 cpPolyShapes(矩形)堆叠在一起,底部矩形较大应该可以解决问题。

有人可以举个例子来说明如何实现这一点吗?

我阅读了有关 cpShape 的 Chipmunk 文档,http://code.google.com/p /chipmunk-physicals/wiki/cpShape,它在注释部分的页面最底部指出“您可以将多个碰撞形状附加到刚体”,但没有提供示例。

我目前有一个正在运行的功能性项目,并正在尝试进行一些最终调整以改进游戏玩法。

I have created a single player game using iOS + Cocos2d + Chipmunk and I'm looking for a solution that demonstrates how to attach multiple collision shapes to a single rigid body. I have a target that has an irregular shape (a car) that I need to detect collisions for. The target (car) is seen by the player from a side view and other objects may impact the target from multiple directions, not just from the front or the rear. The shape is such that I am unable to use a single cpPolyShape and achieve a realistic collision effect. Two cpPolyShapes (rectangular) stacked on top of each other, with the bottom rectangle being larger should do the trick.

Can someone provide a example of how this can be achieved?

I read the Chipmunk docs about cpShape, http://code.google.com/p/chipmunk-physics/wiki/cpShape, and it states that 'You can attach multiple collision shapes to a rigid body' in the very bottom of the page in the notes section, but no example is provided.

I currently have a working, functional project and am trying to make some final adjustments to improve game play.

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

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

发布评论

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

评论(2

过去的过去 2024-12-01 00:20:00

当您调用 cp*ShapeNew() 时,第一个参数是要将其附加到的主体。简单地制作多个共享同一主体的形状。没有什么窍门。

When you call cp*ShapeNew(), the first parameter is the body to attach it to. Simple make more than one shape that share the same body. There is no trick.

硪扪都還晓 2024-12-01 00:20:00

您可以添加方法

在 .h 文件中添加原型

static int FunctionName (cpArbiter *arb, cpSpace *space, void *unused);

现在在 .m 文件中添加代码:

    cpSpaceAddCollisionHandler(<space name>, <cpCollisionType of  body a >, <cpCollisionType of body b>, <cpCollisionBeginFunc name>, <cpCollisionPreSolveFunc preSolve>, <cpCollisionPostSolveFunc postSolve>, <cpCollisionSeparateFunc separate>, <void *data>);

static int FunctionName(cpArbiter *arb, cpSpace *space, void *unused)
{
    cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);    
    printf("\n Collision Detected");    
    return 1;   
}

注意:- 不要忘记给出两个 Body 的碰撞类型。

You can add the method

In the .h file add the prototype

static int FunctionName (cpArbiter *arb, cpSpace *space, void *unused);

Now in the .m file add the code as

    cpSpaceAddCollisionHandler(<space name>, <cpCollisionType of  body a >, <cpCollisionType of body b>, <cpCollisionBeginFunc name>, <cpCollisionPreSolveFunc preSolve>, <cpCollisionPostSolveFunc postSolve>, <cpCollisionSeparateFunc separate>, <void *data>);

static int FunctionName(cpArbiter *arb, cpSpace *space, void *unused)
{
    cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);    
    printf("\n Collision Detected");    
    return 1;   
}

Note:- Don't Forget to give the collision type of both Body.

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