如何在Java中制作一个实心矩形?
如何在java中制作一个实心矩形(或块)以便我的玩家可以站在上面?
目前我正在保存最后一个位置,一旦我检测到玩家的矩形和块的矩形相交...我将位置重置为前一个位置。但这效果并不好。
有更好的办法吗?
How do I make a solid rectangle (or block) in java so my player can stand on it?
At the moment I am saving the last position, and once I detect the player's rectangle and the block's rectangle intersect...I reset the position to the previous one. But that doesn't work out well.
Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是一个简单的问题。不存在所谓的实心矩形;你必须使用大量代码来创造这种错觉(除非你可以从某处借用一些代码)。我的建议是使用圆形而不是矩形。您可以通过中心的位置来确定它们的位置,通过半径来确定它们的大小。 (如果你把它们画成矩形,我猜没有人会注意到它们的行为像圆形。)
现在,每当两个“矩形”的中心点比它们的直径之和更近时,就会发生碰撞,并且您可以轻松计算出为防止重叠而必须备份每一项的数量。通过一些算术(和几何),你可以让它看起来不错。您可以回溯并确定碰撞发生的时间和地点,并计算碰撞后的正确路径以及当前的正确位置。
让圆起作用,你就可以回到矩形——它们只是半径随方向变化的圆。 (我不会打扰。)但这并不容易,除非你让它发挥作用。然后,您可以在一千个地方使用该代码,而忘记最初编写它是多么痛苦。
This is not a simple problem. There is no such thing as a solid rectangle; you have to create the illusion using a lot of code (unless you can borrow some from somewhere). My suggestion would be to use circles rather than rectangles. You can identify their location by where there centers are, and their size by their radius. (If you draw them as rectangles, my guess is no one will notice that they behave like circles.)
Now, whenever the center points of the two "rectangles" get closer than the sum of their diameters, you have a collision, and the amount you have to back each one up to prevent overlapping is easily calculated. With a bit of arithmetic (and geometry) you can make this look good. You can work back and determine when and where the collision occurred and figure the correct paths after the collision and hence the current correct location.
Get circles working and you can get back to rectangles--they're just circles with radii that change with direction. (I wouldn't bother.) But this is not easy until you have it working. Then you can use the code in a thousand places and forget what a pain it was to write it in the first place.
要绘制一个实心矩形,您需要进入您的绘制方法并调用 fillRect。
To draw a solid rectangle you woul go into your paint method and call fillRect.
从某种意义上说,您是在问如何实现二维 Physics Engine 的一些首要基础知识。
请注意,即使是大型游戏工作室也在使用其他人构建的引擎...像 Halo、Bioshock、Assassin's Creed 等大牌游戏都使用“Havok”:
http://en.wikipedia.org/wiki/Havok_(software)
即使在更简单的 2D 平台游戏世界中,从一些更高级别的东西开始可能会更好。您接受已经为您提供了绘制矩形和圆形的例程(而不是您自己绘制像素)。为什么不让自己使用别人已经制作的精灵库呢?
如果您想使用更基本的物理和算法,还有 JBox2D 之类的工具,它可能很有趣:
http:// /www.jbox2d.org/
除此之外,以下问题的答案可能有用,包括一些平台游戏库的线索:
Java 中两个图像之间的碰撞检测
我有时还会补充一点,如果您正在尝试开发一款游戏,在经历编写游戏的所有麻烦之前,制作原型并证明您的设计实际上有趣可能更为重要。有一些很好的工具可以实现这一点,但 GameSalad 是我最近听说的一个重要工具。其他资源:
http://en.wikipedia.org/wiki/Category:Video_game_creation_software
You're asking, in a sense, how to implement some of the first basics of a two-dimensional Physics Engine.
Note that even large game studios are using engines others have built...big names like Halo, Bioshock, Assassin's Creed etc. all use "Havok":
http://en.wikipedia.org/wiki/Havok_(software)
Even in the much simpler world of 2-D platformers, it might be better to start from a bit of higher level stuff. You accept that you've been given a routine to draw rectangles and circles (instead of plotting pixels out by yourself). Why not let yourself use a sprite library someone's already made?
If you want to work with more basic physics and algorithms, there's also the likes of JBox2D, which can be fun:
http://www.jbox2d.org/
Beyond that, the answers to the following question might be useful, including some leads to platformer libraries:
Collision Detection between two images in Java
I'll also add that sometimes if you are trying to come up with a game, it can be more important to prototype and prove that your design is actually fun before going through all the trouble of writing it. Some good tools out there for that, but GameSalad is a big one I'm hearing about lately. Other resources:
http://en.wikipedia.org/wiki/Category:Video_game_creation_software
如果您想知道两个矩形是否相交,可以使用 Java 类 矩形。
它定义了一个函数 intersect,它接受另一个矩形并告诉您两者是否相交。
在游戏中,您通常会在移动玩家之前计算玩家和障碍物的新位置。然后检查新计算的位置是否与玩家相交。如果是,则不要移动他;如果否,请将位置更新为您计算的值。
让我们假设您的玩家现在位于 oldRect。
If you want to know whether two rectanges intersect you can use the Java class Rectangle.
It defines a function intersect which takes another rectangle and tells you whether the two intersect.
In a game you would usually calculate the new position of the player and obstacles before moving him. Then you check whether the newly calculated position would intersect with the player. If yes you do not move him if no, update the position to what you calculated.
Let us asume that you player is now at oldRect.