使用 TILED 地图进行 Java 碰撞检测
我正在制作一款游戏,遇到了一个问题。 我进行了碰撞检测,当多边形接触瓷砖时,结果为真。 虽然这非常适合让玩家不要走墙(真的吗?)。 当我施加重力时,它应该用相同的方法停止,但这造成了问题。 它一直下落,直到撞到地板,但随后你也无法再行走,所以我需要另一个碰撞检测。 我不知道从哪里开始? :( 谢谢。
I was making a game and came across a problem.
I made a collision detection that says true when the poly touches a tile.
While that was perfect for making the player not to walk (true?) a wall.
When i applied gravity it should stop with the same method BUT that made a problem.
it kept falling till it hitter the floor but then you also can't walk anymore so i need another collision detection.
and i have no clue where to start? :(
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将所有图块加载到 ArrayList 中。然后,您可以使用 for-each 循环对玩家的所有图块应用碰撞检测。请注意,以下示例可能不会声明工作所需的所有内容,但它应该可以帮助您了解这种碰撞检测方法的工作原理,并允许您将其实施到游戏中。
Tile.java
CheckCollision.java
Player.java
Graphics.java(绘制图块和播放器的类)
Load all of your tiles into an ArrayList. Then you can use a for-each loop to apply collision detection to all of the tiles with your player. Please note that the following example may not declare everything necessary to work, but it should help you understand how this method of collision detection works and allow you to implement it into your game.
Tile.java
CheckCollision.java
Player.java
Graphics.java (Your class that draws the tiles and player)
我的《我的世界》克隆版也遇到了类似的问题。问题是我的代码每帧都会检测到与地板的碰撞,我通过取消所有 3 个维度的运动来响应这一碰撞。我改变了它,所以我只取消了该运动的垂直分量,并且效果很好。
I had a similar problem with my Minecraft clone. The problem was that my code kept detecting a collision with the floor every frame, which I was responding to by cancelling motion in all 3 dimensions. I changed it so I only cancelled the vertical component of that motion, and it worked fine.