Java中的碰撞检测 [英] Collision Detection in Java

查看:216
本文介绍了Java中的碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



所以我选择学习Java,我正在关注Oracle网站上的官方Java教程,但作为一个侧面项目,我是开发一个自上而下的游戏。



到目前为止,我有2个精灵编入游戏,我希望能够检测到2之间的碰撞。



我在游戏中使用Grahpics2D和JFrame。现在我想要对游戏做的是当我检测到精灵1和精灵2之间的碰撞时,我想删除精灵1并将精灵2更改为另一个图像。我怎么能这样做。



谢谢。

Hello everyone,

So I am learning Java by choice, and I am following the Official Java Tutorials on the Oracle site, but as a side project I am developing a top down game.

I have 2 sprites programmed into the game so far, and I want to be able to detect a collision between the 2.

I am using Grahpics2D and JFrame for my game. Now what I was wanting to do with the game is when I detect a collision between sprite 1 and sprite 2, I want to remove sprite 1 and change sprite 2 to another image. How could I do this.

Thank you.

推荐答案

碰撞检测是一个非常大的话题。可能你会对一个非常小而简单的碰撞探测器没问题,但目前看来你不能自己写一个,我怀疑你想读一本关于碰撞探测的书,或者想要学习难以解决问题的数学。碰撞检测是游戏物理引擎的核心部分。除此之外,物理引擎还包含其他东西,使您可以模拟对象在世界中的移动,使其看起来真实。一个好的游戏物理引擎允许您在游戏物理引擎的世界内创建具有不同简单几何形状(圆形,矩形,线条)的对象,并允许您设置这些对象的属性(质量,摩擦力,弹性......) ,创造作用于这些物体的力(拖曳力,每个物体力......),连接这些物体等等......然后当你完成后,你可以用指定的时间步进/更新你的物理世界。如果你在空中创建了一个非零质量的圆,那么在你的物理世界中更新2秒就会将这个圆移向地面(如果你已经在你的世界中设置了地面和重力,或者它默认具有这些。 ..)。如果圆圈到达地面并且您设置了正确的参数,圆圈可能会像球一样从地面跳回来。因此,对于引擎的每个可移动对象,您必须在物理引擎世界中创建一个附带的物理对象。当您在主更新中更新游戏状态时(比如1/30秒),您首先要更新物理引擎,它会告诉您在自己的引擎中移动对象的位置。您可以使用任何物理参数为物理世界中的精灵定义圆形或矩形或任何形状,以使它们弹跳或表现得像100kg金属球。



物理引擎对于你的简单问题看起来可能看起来像是一个重量级的解决方案,但我仍然建议你学习和使用一个因为:写一个好的物理引擎是非常困难的,你可以把很酷的游戏/程序放在一起,而不知道怎么写一个只是通过能够使用其中之一。作为2D物理引擎,我建议您探索Box2D物理引擎的Java端口: http://www.jbox2d.org/ [ ^ ]



玩得开心!!!在2D中玩物理引擎已经很有趣了!查看链接网站上的演示.jar文件!该演示不包含精湛的图形,但是对场景进行蒙皮并检测与物理系统的碰撞是没有问题的。
Collision detection is a very huge topic. Probably you would be okay with a very small and simple collision detector but currently it seems you can't write one for yourself and I doubt that you want to read a book on collision detection or want to learn the difficult math to solve your problem. Collision detection is a core part of game physics engines. Besides this physics engines contain other things too that enables you to simulate the movement of your objects in the world so that it looks "real". A good game physics engine allows you to create objects with different simple geometries (circle, rectangle, line) inside the world of the game physics engine and allows you to set the properties of those objects (mass, friction, elasticy, ...), creating forces (drag force, per object force, ...) that act on these objects, connect these objects, etc... Then when you are done you can "step"/update your physics world with a specified amount of time. If you created a circle in the air with a nonzero mass, then updating 2 seconds on your physics world will move this circle towards the ground (if you have set up a ground and a gravity force in your world or it has these by default...). If the circle reaches the ground and you set up correct parameters the circle may jump back from the ground like a ball. So for each of your movable objects of your engine you have to create an accompanying physics object in the physics engine world. When you update the state of your game in your main update (lets say with 1/30 seconds) then you first you update your physics engine and it tells you where to move your objects in your own engine. You can define circle or rectangle or whatever shape for your sprites in the physics world with whatever physical parameters to make them bouncing or behave like 100kg metal balls.

A physics engine may look like a heavy-weight solution to your simple problem but I still recommend you to learn and use one because: Writing a good physics engine is extremely difficult and you can put together cool games/programs without knowing how to write one just by being able to use one of them. As a 2D physics engine I would recommend you to explore the java port of the Box2D physics engine: http://www.jbox2d.org/[^]

Have fun!!! Playing around with a physics engine in 2D is already fun! Check out the demo .jar file on the linked site! The demo doesn't contain superb graphics but skinning a scene and detecting collision with a physics system is a no brainer.


检查出来



http://devmag.org.za / 2009/04/13 / basic-collision-detection-in-2d-part-1 / [ ^ ]


这篇关于Java中的碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆