• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

Java collision detection

I am creating a smalll java midlet game. It is a racing game and I have collision detection with the player and the finish line. Which below

    if (playerSprite.collidesWith(finishSprite, true)) {
      // Play a wave sound for hitting the enemy ship
      try {
   
        Lap += 1;
        minePlayer.start();
      }
      catch (MediaException me) {
      }

 
    }

The problem I have with it at the minute is that when the bike collides with the finish line it adds laps on as its passing over the line so it jumps from lap 1 to lap 14 or something.  Is there anyway to put in a delay so that it will only add one at a time?


0
tango2009
Asked:
tango2009
2 Solutions
 
satsumoSoftware DeveloperCommented:
Colliding with a sprite is not the way to do this.  The bike is colliding with the line if any part of the bike is touching any part of the line.  Because it might take the entire bike more than one frame to pass the finish line sprite, you will get lots of collisions.

You need to know if the bike crosses the line.  Assuming that you know how long the race is, you just check how far the bike has travelled along the track.  Add a lap when it has travelled far enough to go around the whole track.
0
 
tango2009Author Commented:
How can I track how far the bike has moved? The bike can change its speed as it goes around the track when it picks up items. Instead of using an integer that is added by one with every collision would be possible to have say four checkpoints. Each using a boolean that would start at false and be turned to true on collision. When all four are true a lap is added and then they are all reset to false for the next lap.
0
 
satsumoSoftware DeveloperCommented:
What variables does the program have at the moment?  Does it keep the speed of the bike or the length of the track?

When you first asked about making a Road Rash game, that got me interested in the rendering a track in a browser using techniques from that time (before 3D hardware).  I'm a big fan of Wipeout, so I made this.  It's not going to be a finished game because I can't get the quality I'd like this way, so I've decided to do a proper 3D version, still I thought you'd be interested.
whoosh.png
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
tango2009Author Commented:
I have a variable for the speed but not the length of the track. The speed can be increased though when you pick up items along the track.

Your game looks pretty cool what language are you doing it in java?
0
 
josephtsangCommented:
Store a new boolean flag "nowCollided" in your player instance, by default make it false. If the original collision method returns true, and at the same time nowCollided is false, then you turn nowCollided as true and increase the lap. If your original collisino method returns false, and at the same time nowCollided is true, then you turn nowCollided into false too.

Basically this is the concept of two-phase commit.
0
 
satsumoSoftware DeveloperCommented:
If you don't have the length of the track how do you know where to put the finish line sprite?  Does the track scroll?

The game is in Javascript and uses the HTML5 canvas.  Javascript syntax is close to Java syntax, though the language have slightly different constructs.  I might upset some Java programmers by saying that.  I should point out, the image shows a section of track that rises, that's not a mistake in the perspective.  

One of the problems is that I can't get it to fill the area under the track, at least not very efficiently.  Although while writing this I've had an idea that might work.
0
 
StormSeedCommented:
If you are set on doing this with the sprite collision, josephtsang's method is the way to go.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now