Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Updated 3-Sep-13 6:50am
Kenneth Haugland 3-Sep-13 13:05pm    
Real Time Collition detection by Christer Ericson might be a good place to start then.
Gigabyte Giant 3-Sep-13 13:11pm    
Thank you, I will try that out. I may have figured out another way to do it as well.

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:[^]

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.
Share this answer
Gigabyte Giant 3-Sep-13 16:02pm    
Thank you very much. I am willing to learn the math behind a physics engine if that is what it takes. With me being a student, I want to learn as much as possible. Since computer science and Math are my favorite things to learn, I have always taken an interest in Physics but really had nothing to apply it to, so this is perfect! Thank you again, ~Gigabyte Giant
pasztorpisti 3-Sep-13 16:58pm    
You are welcome! Sometimes using a physics engine is an overkill when you need only a small subset of the functionalities. Sometimes custom code is better, for this reason it is good to know how to write a basic physics system. Writing very good and complicated ones is very hard. I would recommend playing around with 2D physics first instead of 3D ones. In physics simulation systems a very big problem is usually dealing with the inaccuracy of float calculations. Dealing with inaccuracy and less degrees of freedom in 2D allows you to write much more stable 2D engines than 3D engines with the same effort.
Learning: For both 2D and 3D physics first you have to have good basic 2D or 3D math skills.
To learn 2D physics I haven't seen any good books but you can use google:
For 3D physics here is a good beginner book on 3D math (skip the boring first 50-100 page if you want):
Beginner 3D physics engine book without dry math:
Gigabyte Giant 3-Sep-13 17:02pm    
Cool, I think that before I continue with my game project I am going to try to figure out 2D physics engines. I did look at jbox and I am really excited to get to work on my own engine. Thank you again.
pasztorpisti 3-Sep-13 17:06pm    
This is solid fun if you are interested! Good luck and have fun! :-) With this physics stuff you can learn 2D math playfully. Different kind of shapes (line, rectangle, cricle, ellipse, ...), testing the intersection of these, making different calculations on these objects, and so on. This 2D math is very useful stuff that you would need anyway in order to create the games you dream about. If we speak of 2D math then you should focus on 2x2 and 3x3 matrix operations and the usage of these in coordinate geometry. The other two very important operations are the Dot product and the
pasztorpisti 3-Sep-13 17:05pm    
Something important for later game development: Seomtimes its easier to find out the game by using a full fledged physics system and when you found out the game logic you can think whether it is worth rewriting a small subset of the physics for that particular game - for example if your game contains only circles with really stupid physics then you can write a simple physics engine that handles only circles pretty well in a robust way optionally putting in some game-specific handling. A mini engine may consume less resource (cpu, memory) and some small features required by your game may be implemented with simple techniques that are easy to put in a small engine but in a pre-baked physics engine you may have to use complicated workarounds to get the desired effect in your "prototype-game" and sometimes a simple physics engine can contain a few features in a much better and stable manner than a big engine. Still often using a pre-baked engine does pretty good job depending on the kind of game! Knowing physics internals helps you in decide when to use own or pre-written engines depending on the requirements of the game and the time available for your project.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900