![]() |
|
|
| APPLET: My code for tutorial 3. |
| Controls |
|
| Arrow keys: | Move |
| Space: | Change Sprite |
| A: | Animation on/off |
| z,x | Rotate animation |
| s | Reset rotation to 0 |
| Drag mouse | Position sprite |
Collision Detection:
So far I have used the GGD package to draw and animate sprites. In this tutorial I will implement and discuss collision detection. Collision detection is imperative for almost any game that is to be designed. If you view the SpaceWar game that I created you will see collision detection in action. The Slot machine game I designed is probably one of the few games that doesn't require collision detection. I have also implemented a rotation method to the sprite. This is handy for games that are implemented in an asteroid like fashion. The rotation of a sprite that uses collision detection is best performed with a square bounding sprite. (eg; 32x32, 12x12).
This is a fairly simple tutorial. I did want to introduce pixel perfect collision detection as well but that will have to be a topic for a future tutorial as I cannot find any clear information on this topic as yet. If you have any information or code on this subject please email it to me.....;-)
As you can see from the applet above I have implemented a simple rectangle sprite collision algorithm. All the method detectCollision(MyRect r); does is return a boolean value of true or false dependant on a collision with the supplied MyRect or Sprite object, since it inherits the MyRect class. The applet above detects a collision with another sprite and displays the intersection which is the area that overlaps within the two sprites. This information will prove to be handy when I implement pixel perfect collision detection.
There is other options available as well, To bring a more realistic approach to collisions you can reduce the size of the collision detection area. This will allow the two sprites to overlap until the collision area is reached. This is handy if you feel that collisions happen when the two sprite's do not seem to collide with each other. The MoveRect object's method calls are:
// set the collision area using integers.
void setCollisionArea(int x, int y, int w, int h);
// set the collision area using percentage values eg; 0.5 for 50%
void setCollisionArea(double f);
// returns the collision area reletive to the sprites location.
Rectangle getCollisionArea();
// returns the collision area relitive to the screen coordinates.
MyRect getRelativeCollisionArea();See the MoveRect API for more information on its methods. One thing to note when setting the collision area within the sprite is the the x, y coordinates of the collision area are relative to the sprite rectangle which is shown in fig 3.1.
![]() |
| fig1.1: Collision Area. |
When using percentage values to size the collision area the area is always centered within the sprite. If you wish to have a collision area somewhere other than centered in the sprite use the setCollisionArea(int x, int y, int w, int h) method. By default with no call to setCollisionArea() the collision area is equal to the sprite area.
When I nut out the technique for pixel perfect collision detection I will place it here in this tutorial.
........
Rotation:
Another little point to note is the fact that this package supports image rotation I will be explaining this more in the next tutorial. The code that manipulates the image rotation is contained within the BitmapSprite class check out the API for more information. In this example I create an int theta object and just pass the sprite the theta var using whitie.rotate(theta); One thing to note here is that the method whitie.setRotate(true); must be called to enable rotation drawing otherwise nothing will rotate so to speek. I put this method in because rotating an image requires creating a buffer to draw to and it is slightly slower than drawing an image straight to the screen therefor if the sprite does not use rotation there is no need for the extra overhead.
Misc thoughts:
In the next tutorial I mean to implement a class that handles missiles. The missile can be in the form of a simple rectangle or an image. the sprite should be able to be moved in any direction as well as be rotated. Animation of the image missile sprite would be nice so inheritance of the BS_Loop will be used.
![]() |