使用 SpaceManager 时,如何使静态精灵成为 cocos2D 中另一个精灵的子对象
我有两个静态 (STATIC_MASS) SpaceManager 精灵。一个是另一个的孩子 - 我的意思是一种建立了另一个,但是尽管孩子的图像显示在正确的位置,但孩子似乎并不存在于花栗鼠物理引擎中,就像我一样会期望。就我而言,我有一个篮板(矩形精灵)和一个篮筐(圆形精灵)。由于我可能想要移动篮板,因此我想将篮筐连接到篮板上,以便篮筐自动与篮板一起移动。
在这里,我们看到一个带有箍的旋转篮板。它在屏幕上看起来不错,但其他物体只会从篮板上弹开,但会直接穿过篮筐(从这个术语的不好的意义上来说)。我的子精灵在物理引擎中似乎不存在什么?
// Add Backboard
cpShape *shapeRect = [smgr addRectAt:cpvWinCenter mass:STATIC_MASS width:200 height:10 rotation:0.0f ];// We're upgrading this
cpCCSprite * cccrsRect = [cpCCSprite spriteWithShape:shapeRect file:@"rect_200x10.png"];
[self addChild:cccrsRect];
// Spin the static backboard: http://stackoverflow.com/questions/2691589/how-do-you-make-a-sprite-rotate-in-cocos2d-while-using-spacemanager
// Make static object update moves in chipmunk
// Since Backboard is static, and since we're going to move it, it needs to know about spacemanager so its position gets updated inside chipmunk.
// Setting this would make the smgr recalculate all static shapes positions every step
// cccrsRect.integrationDt = smgr.constantDt;
// cccrsRect.spaceManager = smgr;
// Alternative method: smgr.rehashStaticEveryStep = YES;
smgr.rehashStaticEveryStep = YES;
// Spin the backboard
[cccrsRect runAction:[CCRepeatForever actionWithAction:
[CCSequence actions:
[CCRotateTo actionWithDuration:2 angle:180],
[CCRotateTo actionWithDuration:2 angle:360],
nil]
]];
// Add the hoop
cpShape *shapeHoop = [smgr addCircleAt:ccp(100,-45) mass:STATIC_MASS radius: 50 ];
cpCCSprite * cccrsHoop = [cpCCSprite spriteWithShape:shapeHoop file:@"hoop_100x100.png"];
[cccrsRect addChild:cccrsHoop];
上面的代码有点乱。篮筐的图像显示在板旁边,这就是我想要的,如果我检测到碰撞,碰撞只会发生在屏幕的左下角。奇怪的是,即使我检测到碰撞,我的物体实际上似乎并没有碰撞,它只是穿过它而不是从它反弹。
注意:SpaceManager 是一个用于 cocos2D-iphone 的工具包
I have two static (STATIC_MASS) SpaceManager sprites. One is a child of the other - by which I mean that one sort of builds up the other one, but although the child's images shows up in the right place, the child doesn't seem to exists in the chipmunk physics engine, like I would expect. In my case, I have a backboard (rectangular sprite) and a hoop (a circular sprite). Since I might want to move the backboard, I'd like to attach the hoop to backboard so that the hoop automatically moves right along with the backboard.
Here, we see a rotating backboard with attached hoop. It looks OK on the screen, but other objects only bounce off the backboard but pass right through the hoop (in a bad sense of the term). What doesn't my child sprite seem to exist in the physics engine?
// Add Backboard
cpShape *shapeRect = [smgr addRectAt:cpvWinCenter mass:STATIC_MASS width:200 height:10 rotation:0.0f ];// We're upgrading this
cpCCSprite * cccrsRect = [cpCCSprite spriteWithShape:shapeRect file:@"rect_200x10.png"];
[self addChild:cccrsRect];
// Spin the static backboard: http://stackoverflow.com/questions/2691589/how-do-you-make-a-sprite-rotate-in-cocos2d-while-using-spacemanager
// Make static object update moves in chipmunk
// Since Backboard is static, and since we're going to move it, it needs to know about spacemanager so its position gets updated inside chipmunk.
// Setting this would make the smgr recalculate all static shapes positions every step
// cccrsRect.integrationDt = smgr.constantDt;
// cccrsRect.spaceManager = smgr;
// Alternative method: smgr.rehashStaticEveryStep = YES;
smgr.rehashStaticEveryStep = YES;
// Spin the backboard
[cccrsRect runAction:[CCRepeatForever actionWithAction:
[CCSequence actions:
[CCRotateTo actionWithDuration:2 angle:180],
[CCRotateTo actionWithDuration:2 angle:360],
nil]
]];
// Add the hoop
cpShape *shapeHoop = [smgr addCircleAt:ccp(100,-45) mass:STATIC_MASS radius: 50 ];
cpCCSprite * cccrsHoop = [cpCCSprite spriteWithShape:shapeHoop file:@"hoop_100x100.png"];
[cccrsRect addChild:cccrsHoop];
The above code is a bit whacked. The image of the hoop shows up next the board, which is what I want, if I detect collisions, the the collision only happens in the lower left of the screen. Oddly, even though I'm detecting a collision, my object doesn't actually seem to collide, it just passes through it instead of bouncing off of it.
Note: SpaceManager is a toolkit for working with cocos2D-iphone
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是父子关系在物理引擎中没有意义。您可以将多个形状附加到同一个主体,这样当您移动一个主体时,它就会移动多个形状。
The problem is that a parent child relationship doesn't make sense in a physics engine. You can attach multiple shapes to the same body so that when you move the one body it moves several shapes.
对此的简单回答是,这实际上是不可能的。 Chipmunk 喜欢一切都在世界坐标中,当你开始在 cocos2d 中创建父子关系时,你正在设置坐标系的错误不匹配......
但是......我确实尝试弥合这个问题,但没有取得太大成功(问题是旋转我相信)看看SpaceManager.m中的函数“eachShapeAsChildren”。如果您将 SpaceManager 的 iterateFunc 属性设置为指向此函数,您可以看到会发生什么......
在某些时候我可能会再试一次;事实上,我已经感到更有灵感了。
The simple answer to this is that it's not really possible. Chipmunk likes everything being in world coordinates and when you start creating parent-child relationships in cocos2d you are setting up a bad mismatch of coordinate systems...
However... I did try to bridge this issue without much success (the problem being rotation I believe) Look at the function "eachShapeAsChildren" in SpaceManager.m. If you set iterateFunc property of SpaceManager to point to this function, you can see what happens....
At some point I may give it another go; in fact I'm feeling more inspired already.