目前我是一名 flash 游戏开发者,我开始使用 cocos 2d 引擎为 iphone 编写游戏,我已经实现了单独的 Axis theorem 来进行碰撞检测,效果非常好。所有的多边形在openGl中绘制如下。 经过多次搜索后,我现在正在尝试将重力应用于这个 16x16 盒子,我找到了这个教程 http://www.seinia.com/tutorials/Bounce/并在 Objective-C 中实现了相同的内容。
我遇到的问题是,在正方形停止后,它会继续以分数形式向上/向下弹跳。我尝试了很多来解决这个问题,但我无法控制那个微小的移动。我在 flash 中从来没有遇到过这样的问题,但这里的 float 值对正方形位置的影响很大。
请让我知道处理此类问题的写法是什么,任何引用 URL 都会有所帮助。感谢您的帮助。谢谢!
0,16 16,16
------------
| |
| |
| |
| |
------------
0,0 16,0
Objective-C 代码
if (square.points[0].y <= 0.1f) {
velocity.vy *= -bounce;
[square restInPeace:[Vector2D createVectorWithComponentX:velocity.vx Y:8.0f]];
// landed
if (fabs(velocity.vy) < 0.9f) {
velocity.vy = 0.0f;
[square restInPeace:[Vector2D createVectorWithComponentX:velocity.vx Y:8.0f]];
isLanded = YES;
}
}
翻译对象
-(void) translateVelocity:(Vector2D*)velocity
{
// The center as well as all of the vertices need to be
// accommodated.
center.x += velocity.vx;
center.y += velocity.vy;
for(int i=0; i < count; i++)
{
points[i].x += velocity.vx;
points[i].y += velocity.vy;
////NSLog(@"velocity %f %f",points[i].x , points[i].y);
}
}
最佳答案
当使用反弹算法时,通常建议实现一个轻微的缺陷以确保不会发生此事件。您还可以扩大被接受为“着陆”的范围,但请记住确保您随后将对象粘贴到地板上以确保没有视觉伪影。
我的意思是不完美:
velocity.vy *= (-bounce + 0.01f);
例如。这应该使您的对象始终停止。
关于objective-c - Iphone 2d 游戏 - 应用重力,弹跳问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6173291/