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 >> Fly my robot, FLY!!! >> Pretty collisions

Pretty collisions

You noticed in the previous chapter how long the robot must walk on a tile before it falls. Its legs are in the empty space, and it doesn't fall. Now it is time to introduce collision offsets.

Collision offsets are four variables to set in a sprite. By default, they are set to 0. Some times, it is useful to chance their values, like in our case.

Collision offset explanation This picture shows how offsets are disposed in a sprite. You can change their value with the following methods of the sprites object:

In our example, we only need to change the left and the right offsets, as show in the following picture:
Collision offset for the robot
  • 6 pixels for the left collision offset
  • 8 pixels for the right collision offset

This is done in the class DisplayRobotFinal.java.
But, we have to change something else. In the DummyRobot object, when we change the position of the new sprite, we need to consider collision offsets. In DummyRobotFinal.java, needed changes are done in changeDirection(RobotSprite, RobotSprite) method:


...
  /**
   * Changes direction of the robot
   */
  protected void changeDirection(RobotSprite fromSprite, RobotSprite toSprite) {
    PlayField parent = getParent();
    Sprite finalWrapper = getFinalWrapper();
    Point p = getPosition();
    toSprite.setCurrentPictureNumber(fromSprite.getCurrentPictureNumber());
    parent.removeSprite(finalWrapper);
    setActionSprite(toSprite);
    setPosition(p.x + (fromSprite.getLeftCollisionOffset() -
		       toSprite.getLeftCollisionOffset()) ,
		p.y);
    toSprite.fly(fromSprite.isFlying());
    parent.addSprite(finalWrapper);
  }
...

Finally, we assemble all of these elements to obtain this applet:

  Use arrow keys to move the robot.

Thanks to Hermann Hillmann for its Small WarBot.

But, with only one type of tiles, it's a little bit boring. In the next chapter, we will see how to add new type of tiles.

<< Previous Page Next Page >>

API >> Tutorials >> Fly my robot, FLY!!! >> Pretty collisions