Collision Programming

How does one do collision programming in java? Lets say i’m using pics for ship, and they are shooting at each other, how do I know when they are hitting each other? Assuming that it is not predetermined.

well, assuming the bullet or rocket the ship fires is an object aswell, it is likely that its got bounds and a position, same goes for the ship. You just have to check if two rectangles intersect.

You don’t program collisions. You write code for detecting em :wink:

For simple tickbased action games where it only matters if something hit something else it’s pretty simple. Ensure that the objects don’t travel too much within one frame (otherwise they can warp through each other) and then simply check if the rectangles (or spheres or whatever) overlap.

Rectangle covers the things you need. That is… you can change the size and position and you can check if this Rectangle overlaps another one.

You can also use several rectangles for one sprite for getting a hitable area which is more similar to the graphic. If you’re using several shapes it’s a good idea to wrap em all with another one. That allows you to quickly discard the in-detail checks (against all those inner shapes) if the outer shape doesn’t intersect with the other object.

In most cases a collision doesn’t happen. Therefore it’s important to exclude as many checks as possible as quick as possible. This way you only do all those time consuming tests if it looks like there actually might be a collision happening (and otherwise not).

Well, for now… start with simple rectangles representing your objects.

You might want to checkout Kev’s spaceinvaders 101 tutorial:
http://www.cokeandcode.com/tutorials

OK this will be very helpful. Thanks!