Solved

# Java collision detection

Posted on 2011-03-16
Medium Priority
439 Views
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
Question by:tango2009

LVL 12

Accepted Solution

satsumo earned 1000 total points
ID: 35150091
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

Author Comment

ID: 35151070
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

LVL 12

Expert Comment

ID: 35152634
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

Author Comment

ID: 35156044
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

LVL 5

Assisted Solution

josephtsang earned 1000 total points
ID: 35158567
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

LVL 12

Expert Comment

ID: 35159487
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

LVL 3

Expert Comment

ID: 35391277
If you are set on doing this with the sprite collision, josephtsang's method is the way to go.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Screencast - Getting to Know the Pipeline
###### Suggested Courses
Course of the Month17 days, 5 hours left to enroll