The Java Game Framework.

Examples, tutorials, documentation and download of the Genuts Framework.
Games made with the Genuts Frameworks.
General articles around games.
Links covering all needs around game dev.
Who are we?
Terms of Service

API >> Tutorials >> Playfield Scrolling and Pre-Collision mode >> Pre-Collision mode

Pre-collision mode: Balls behavior

The pre-collision mode is the ability of a sprite (or a sprite wrapper) to know that a collision will be thrown before it happens. To explain this, we will construct a new sprite wrapper, which moves a ball with right hand rules.

A little explanation is needed.

Right hand rules

The principal knowledge of a ball is to try to move in the counter clockwise order. In that way, when the direction of the ball is to the right: it tries to go first downwards, if it can not, it tries to go to the right, and so on in the clockwise order. The following pictures show how a ball tries to move in different cases:

Right direction

Upward direction

Left direction

Downward direction

In these 4 pictures, the ball tries to go initially to direction 1, then to direction 2, next to direction 3, and finaly to direction 4.

We study right hand rules, but for left hand rules, we only need to turn in the clockwise order.

We have to precise a simple thing; when a ball doesn't follow a tile, it turns in round. To avoid this, we need constants to tell the ball that it must follow a straight line.

 

The wrapper

To activate the pre-collision mode for a sprite, we only need to call the method setPremode(boolean) with true as argument.

Here is the sample Java code which tries to move a ball to the right with right hand rules (from the sprite wrapper class MovingBall.java:


...
	if (!linearDir && (setPosition(getX(), getY()+vy))) {
	  dir = DIRDOWN;
	} else if (!setPosition(getX()+vx, getY())) {
	  if (borderCollision) {
	    linearDir = false;
	  }
	  if (!setPosition(getX(), getY()-vy)) {
	    if (setPosition(getX()-vx, getY())) {
	      dir = DIRLEFT;
	    }
	  } else {
	    dir = DIRUP;
	  }
	}
...

Did you understand how it works?
To try if the ball can go somewhere, we only need to set a new position to the sprite, and if the setPosition(int, int) method returns true, this means that we can move here, otherwise the sprite must move elsewhere.

But to know if a collision will happen, the mechanism is the same as for collisions. We have to use a CollisionManager. In our case it is once again the SpriteCollisionManager.
This time, it doesn't call the method collisionWith(Sprite) of the sprite object but preCollisionWith(Sprite). This method is called when a collision is detected, and it must return true or false to tell other objects if the new position is avalaible or not. By default, this method returns always true to permit new positions and resizing.

In our example, we override the preCollisionWith(Sprite) method:


...
  /**
   * A collision should happen for the current movement.
   */
  protected boolean preCollisionWith(Sprite s) {
    if (s instanceof MovingBall) {
      linearDir = true;
      borderCollision = false;
    } else {
      borderCollision = true;
    }
    return false;
  }
...

Now balls are ready, we modified the previous example in the file PathFinder.java by adding ball sprites in random positions:


...
    // Disposes 20 balls
    int playfieldWidth = playfield.getPreferredWidth();
    int playfieldHeight = playfield.getPreferredHeight();
    for (int cpt = 0; cpt < 20; cpt++) {
      AnimatedSprite anim = new AnimatedSprite(ballImage, 8, 1, 2);
      anim.setCurrentPictureNumber((int) (Math.random()*8));
      MovingBall moving = new MovingBall(anim, 1, 1);
      if ((int) (Math.random()*2) == 0) {
	moving.setClockWise(false);
      }
      playfield.addSprite(moving);
      while (!moving.setPosition((int) (Math.random()*playfieldWidth), 
                                 (int) (Math.random()*playfieldHeight)));
    }
...

We can scroll in this playfield now...

<< Previous Page Next Page >>

API >> Tutorials >> Playfield Scrolling and Pre-Collision mode >> Pre-Collision mode