简体   繁体   English

iPhone上的OpenGL ES中的碰撞检测

[英]Collision Detection in OpenGL ES on the iPhone

I seem to be having a strange issue with a 2D game I'm working on. 我正在研究的2D游戏似乎有一个奇怪的问题。 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. 为了确定我的可玩角色精灵是否落在平台上,我首先执行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)];

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. 大多数情况下,此代码在整个游戏周期内都有效,但是不时地会忽略第二个“ if语句”,并且我的玩家精灵会通过平台。 I'm building this game in OpenGL ES 1.1 with a custom sprite class. 我正在使用自定义精灵类在OpenGL ES 1.1中构建此游戏。

Any insight into this issue would be appreciated! 任何对此问题的见解将不胜感激!

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. 如果平台高度不高,则玩家可能会在平台上方(在一帧中),然后在下一帧中(player.y + playerVelocity.y)超过平台另一半。 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. 您可以做的一件事是使用NSLog()打印出这些值。 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. 一种是确保角色已达到足够高的高度,可以在平台“上方”,然后,如果角色越过,则将其位置重置为在平台上。

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 . 不必为自己烦恼,并阅读有关平台物理学的MetaNet教程: N个教程 The issue you mention is addressed in particular, along with several other issues you are likely going to run into. 您提到的问题以及其他可能会遇到的其他问题都得到了特别的解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM