cocos2d iphone - 游戏架构:物理VS。代码模拟精灵行为

发布于 2024-08-04 04:07:20 字数 312 浏览 5 评论 0原文

你好 stackoverflow 社区!

我正在尝试弄清楚如何使用 cocos2d 构建我的游戏。

我的问题是cocos2d的物理引擎(我说的是花栗鼠)位于精灵背后的世界。

当我移动精灵时,我想知道是否应该

*1)通过在其后面的物理主体上施加力来移动它

,或者

2)我是否应该通过代码模拟移动然后仅使用物理当发生碰撞时。*


我知道你可能会说这取决于我。但是 cocos2d iphone 开发时的预期行为是什么?

我提前谢谢你<3

Hello stackoverflow community !

I am trying to figure out how to architect my game using cocos2d.

My problem is that cocos2d's physics engine (i'm talking about chipmunk) lies in a world behind sprites.

When I move the sprite, I'm wondering if I should

*1) be moving it by applying forces on the physics body behind it

OR

2) if i should just simulate movements through code then use physics only when a collision occurs.*


I know you may say that it's up to me. But what was the intended behavior as cocos2d iphone was developed?

I thank you in advance <3

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

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

发布评论

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

评论(2

好倦 2024-08-11 04:07:20

为了将其保持在花栗鼠系统内,您需要施加力/脉冲。有一些例子,人们用自己的代码控制精灵的移动,让身体跟随精灵,然后将身体的控制权交还给花栗鼠模拟。

不少人也只是使用花栗鼠进行碰撞检测并完全用自己的代码控制精灵运动。这可以通过将身体添加到花栗鼠空间但不将形状添加到空间来完成。

以下是有关该主题的一些讨论..我无法将它们作为超链接发布,因为我是新用户..

http://groups.google.com/group/cocos2d-iphone-discuss/browse_thread/thread/ae37e90eac059135/081da9bcefec76ba?#081da9bcefec76ba

http://www.cocos2d-iphone.org/forum/topic/877"

" http://jeanbombeur.com/content/2009-07-08-synchronizing-sprites-positions-and-rotations- Between-chipmunk-and-cocos2d

欢迎并祝你好运!

附注
还有一种方法可以打开花栗鼠模式,让您可以更自由地控制可以控制的内容,但代价是模拟更加真实。

To keep it within the chipmunk system you would apply forces/impulses. There are examples of folks taking control of moving the sprite with their own code, getting the body to follow the sprite then giving control of the body back to the chipmunk simulation.

Quite a few people also just use chipmunk for collision detection and control sprite movement with their own code entirely. This can be done by adding the body to the chipmunk space but not adding the shape to the space.

Here are some discussions on the subject.. I can't post them as hyperlinks because I am a new user..

"http://groups.google.com/group/cocos2d-iphone-discuss/browse_thread/thread/ae37e90eac059135/081da9bcefec76ba?#081da9bcefec76ba"

"http://www.cocos2d-iphone.org/forum/topic/877"

"http://jeanbombeur.com/content/2009-07-08-synchronizing-sprites-positions-and-rotations-between-chipmunk-and-cocos2d"

Welcome and good luck!

PS
There is also a way to turn on a Chipmunk mode that gives you a little more freedom over what you can control but at the cost of a more realistic simulation.

待天淡蓝洁白时 2024-08-11 04:07:20

对于您想要与形状保持一致的精灵,您需要在创建形状时将精灵作为形状->数据绑定。

然后使用每个形状函数

cpSpaceHashEach(空格->activeShapes,
&函数回调、数据)

在回调函数中使用 shape->data 您可以将精灵位置更新为附加到形状的主体位置,

Sprite s = shape->data
[s setPosition:cpv(body->p.x,body->p.y)];

使用 Cocos 调度来调用 cpSpaceStep() 和 cpSpaceHashEach()。确保您不要每一帧都调用它们,否则您将无法保持同步。

我使用

[self schedule:@selector(step:) interval:(1.0f/60.0f)/3]

这种方式,我的精灵都遵循黑猩猩数据并执行物理操作,而不需要我做额外的工作。

For sprites you want want to keep in line with shapes you need to tie the sprite as the shape->data when creating the shape.

Then use the each shape function

cpSpaceHashEach(space->activeShapes,
&functioncallback, data)

In your callback function using the shape->data you can update the sprites location to the position of the body attached to the shape

Sprite s = shape->data
[s setPosition:cpv(body->p.x,body->p.y)];

use a Cocos schedule to call cpSpaceStep() and cpSpaceHashEach(). Make sure you don't call them each frame or you will have problems with things staying in sync.

I use

[self schedule:@selector(step:) interval:(1.0f/60.0f)/3]

This way my sprites all follow the chimpmunk data and performing the physics with no extra work from me.

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