Featured Posts, J2ME Programming

HOW TO DETECT COLLISION IN MARIO-BASED GAMES

Introduction

Mario-based games demands a greater details of implementation of collision detection than the already existed library in J2ME. For example, the methods provided only return true or false when detecting collision. This does not tell you the collision points, or which side of the Sprite the collision happen, which is necessary to tell if Mario jump on his enemy’s head or just hit him and die. However we can achieve the seemingly complicated algorithm with just an if clause. This how-to will give you some ideas about implementing such function.

2. When to apply?

Anywhere when you need more than just the boolean returned from collidesWith function in J2ME. If you want to know which side of the Sprite has been collided, use the algorithm presented below.

3. Advantages & Disadvantages

  • Advantages: easy and simple to implement
  • Disadvantages: may not be as precise as some other complex algorithm
  • Your Sprite must have some variables to indicate if it is moving or falling

4. Requirement

  • Your Sprite must have some variables to indicate if it is moving or falling

5. Steps

Here is the pseudo-code that show how-to implements the algorithm:

if collision happen

                if character is moving or jumping

                                he hits the enemy and die

                else if character is falling and he is above the enemy

                                he jump on the enemy’s head

                                enemy dies.

So the idea is simple: if the character hits the enemy when he is moving on the ground or jumping, then he dies, or if he is falling then he must hits the enemy’s head and the enemy dies. This idea is much more easier to implement than we do complex calculation to produce the same answer.

6. References

http://j2megame.uw.hu/ch10lev1sec247.html: provides a detailed explanation on how to detect collision in J2ME games in different ways.

Standard

Leave a comment