java碰撞,Java中的碰撞检测

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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值