iPhone 上 OpenGL ES 中的碰撞检测

发布于 2024-09-08 03:10:38 字数 722 浏览 0 评论 0原文

我正在开发的 2D 游戏似乎遇到了一个奇怪的问题。为了确定我的可玩角色精灵是否落在平台上,我首先执行 if 语句检查 CGRectIntersectsRect,一旦确定玩家精灵和平台精灵已相交,我就会检查以确保中心点(加上一半)玩家的高度)在平台上方,如果是的话,我将玩家的“y”速度设置为零。

playerVelocity.x=5;

playerVelocity.y+=gravity;

if (CGRectIntersectsRect([turtle getBounds], [platform getBounds])) {

 if ([player getPosition].y>p.position.y+([player getBounds].size.height/2)) {
  if (playerVelocity.y>0) {
   playerVelocity.y=0;
   inJump=NO;
  }

 }else {
  inJump=YES;
 }
}

[player setPosition:CGPointMake([player getPosition].x+playerVelocity.x, [player getPosition].y-playerVelocity.y)];

大多数情况下,此代码在游戏周期中运行,但是时不时地,第二个“if 语句”会被忽略,并且我的玩家精灵会穿过平台。我正在 OpenGL ES 1.1 中使用自定义精灵类构建这个游戏。

任何对此问题的见解将不胜感激!

I seem to be having a strange issue with a 2D game I'm working on. In order to determine if my playable character sprite lands on a platform I first do an if statement checking CGRectIntersectsRect and once I've determined that the player sprite and platform sprite have intersected, I then check to make sure that the center point (plus half the player's height) are above the platform, if it is, I then set the player's 'y' velocity to zero.

playerVelocity.x=5;

playerVelocity.y+=gravity;

if (CGRectIntersectsRect([turtle getBounds], [platform getBounds])) {

 if ([player getPosition].y>p.position.y+([player getBounds].size.height/2)) {
  if (playerVelocity.y>0) {
   playerVelocity.y=0;
   inJump=NO;
  }

 }else {
  inJump=YES;
 }
}

[player setPosition:CGPointMake([player getPosition].x+playerVelocity.x, [player getPosition].y-playerVelocity.y)];

Most of the time this code works over the game cycle, however every now and then, the second "if statement" is disregarded and my player sprite passes through the platform. I'm building this game in OpenGL ES 1.1 with a custom sprite class.

Any insight into this issue would be appreciated!

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

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

发布评论

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

评论(2

倾听心声的旋律 2024-09-15 03:10:38

要检查的一件事是确保玩家没有真正通过平台。平台有多高?如果平台高度不大,则玩家可能位于平台上方(在一帧中),然后在下一帧中 (player.y + playerVelocity.y) 超过另一帧中平台的一半。这意味着它永远不会“击中”平台,尽管它在视觉上看起来就像是那样。

您可以做的一件事是使用 NSLog() 打印出这些值。这样,当您错过时,您可以检查日志以查看错过的数据值。

应对“登陆平台”的方法有很多。一是确保角色已达到足够高的高度,位于平台“上方”,然后如果它穿过,则将其位置重置为在平台上。

One thing to check is to make sure that the Player doesn't really pass through the platform. How tall is the platform? If the platform height isn't much, then the player could be above the platform (in one frame) and then in the next frame (player.y + playerVelocity.y) more than halfway through the platform on the other. Which would mean that it would never "hit" the platform even though it seems visually like it did.

One thing you can do is to print out using an NSLog() the values. this way when you miss, you can check the logs to see what the data values were that missed.

There are many ways to deal with "landing on platforms". One is to make sure that the character has reached a high enough altitude to be "above" the platform, and then if it ever crosses then you reset its location to be on the platform.

暮倦 2024-09-15 03:10:38

如果您正在制作平台游戏,那么您将不得不处理很多类似的问题。为了省去一些麻烦,请通读有关平台物理的 MetaNet 教程:N 个教程。您提到的问题以及您可能会遇到的其他几个问题都得到了特别解决。

If you're making a platformer, there are a lot of issues like this you're going to have to deal with. Save yourself some headaches and read through the MetaNet tutorials on platformer physics: N tutorials. The issue you mention is addressed in particular, along with several other issues you are likely going to run into.

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