GTGE API

com.golden.gamedev.object.collision
Class BasicCollisionGroup

java.lang.Object
  extended by com.golden.gamedev.object.CollisionManager
      extended by com.golden.gamedev.object.collision.BasicCollisionGroup
Direct Known Subclasses:
CollisionGroup

public abstract class BasicCollisionGroup
extends CollisionManager

Basic collision check, only check whether a collision occured or not.

This class does not gather any information from the collision. To get more information from the collision, such as collision side, use CollisionGroup instead.

This type of collision check is the best to use for hit and destroy collision.

For example: collision between projectile and enemy ships

    Playfield   playfield;
    SpriteGroup PROJECTILE, ENEMY;

    playfield.addCollisionGroup(PROJECTILE, ENEMY,
       new BasicCollisionGroup() {
          public void collided(Sprite s1, Sprite s2) {
             // after enemy collided with projectile,
             // the enemy explode (set to non-active)
             s2.setActive(false);
          }
       }
    );
 

See Also:
PlayField.addCollisionGroup(SpriteGroup, SpriteGroup, CollisionManager)

Field Summary
 boolean pixelPerfectCollision
          Indicates whether this collision detection should use pixel-perfect precision or not.
protected  CollisionRect rect1
          Default collision shape used as every sprites in group 1 bounding box.
protected  CollisionRect rect2
          Default collision shape used as every sprites in group 2 bounding box.
 
Constructor Summary
BasicCollisionGroup()
          Creates new BasicCollisionGroup.
 
Method Summary
 void checkCollision()
          Checks for collision between all members in group 1 againts all members in group 2.
abstract  void collided(Sprite s1, Sprite s2)
          Notified when sprite1 from group 1 collided with sprite2 from group 2.
 CollisionShape getCollisionShape1(Sprite s1)
          Returns collision shape (bounding box) of specified sprite from group 1.
 CollisionShape getCollisionShape2(Sprite s2)
          Returns collision shape (bounding box) of specified sprite from group 2.
 boolean isCollide(Sprite s1, Sprite s2, CollisionShape shape1, CollisionShape shape2)
          Performs collision check between Sprite s1 and Sprite s2, and returns true if the sprites (shape1, shape2) is collided.
 
Methods inherited from class com.golden.gamedev.object.CollisionManager
getGroup1, getGroup2, getIntersectionRect, isActive, isPixelCollide, setActive, setCollisionGroup
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rect1

protected final CollisionRect rect1
Default collision shape used as every sprites in group 1 bounding box.


rect2

protected final CollisionRect rect2
Default collision shape used as every sprites in group 2 bounding box.


pixelPerfectCollision

public boolean pixelPerfectCollision
Indicates whether this collision detection should use pixel-perfect precision or not.

The usual way to turn on this variable is :

 class ThisThatCollision extends BasicCollisionGroup {

    // class initialization
    { pixelPerfectCollision = true; }

 }
 

Constructor Detail

BasicCollisionGroup

public BasicCollisionGroup()
Creates new BasicCollisionGroup.

Method Detail

getCollisionShape1

public CollisionShape getCollisionShape1(Sprite s1)
Returns collision shape (bounding box) of specified sprite from group 1.

In this implementation, the sprite bounding box is set as large as Sprite dimension:

     public CollisionShape getCollisionRect1(Sprite s1) {
         rect1.setBounds(s1.getX(), s1.getY(),
                         s1.getWidth(), s1.getHeight());

         return rect1;
     }
 

Parameters:
s1 - the sprite from group 1 to be check its collision
Returns:
The collision shape of the sprite, or null to skip collision check.
See Also:
rect1, getCollisionShape2(Sprite), CollisionShape.intersects(CollisionShape)

getCollisionShape2

public CollisionShape getCollisionShape2(Sprite s2)
Returns collision shape (bounding box) of specified sprite from group 2.

In this implementation, the sprite bounding box is set as large as Sprite dimension:

     public CollisionShape getCollisionRect2(Sprite s2) {
         rect2.setBounds(s2.getX(), s2.getY(),
                         s2.getWidth(), s2.getHeight());

         return rect2;
     }
 

Parameters:
s2 - the sprite from group 2 to be check its collision
Returns:
The collision shape of the sprite, or null to skip collision check.
See Also:
rect2, getCollisionShape1(Sprite), CollisionRect.intersects(CollisionShape)

checkCollision

public void checkCollision()
Description copied from class: CollisionManager
Checks for collision between all members in group 1 againts all members in group 2.

Specified by:
checkCollision in class CollisionManager

isCollide

public boolean isCollide(Sprite s1,
                         Sprite s2,
                         CollisionShape shape1,
                         CollisionShape shape2)
Performs collision check between Sprite s1 and Sprite s2, and returns true if the sprites (shape1, shape2) is collided.

Note: this method do not check active state of the sprites.

Parameters:
s1 - sprite from group 1
s2 - sprite from group 2
shape1 - bounding box of sprite 1
shape2 - bounding box of sprite 2
Returns:
true, if the sprites is collided one another.

collided

public abstract void collided(Sprite s1,
                              Sprite s2)
Notified when sprite1 from group 1 collided with sprite2 from group 2.

Parameters:
s1 - sprite from group 1
s2 - sprite from group 2

GTGE API

Copyright © 2003-2005 Golden T Studios. All rights reserved. Use is subject to license terms.
GoldenStudios.or.id