?
Solved

Java collision detection

Posted on 2011-03-16
7
Medium Priority
?
437 Views
Last Modified: 2012-05-11
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
Comment
Question by:tango2009
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 12

Accepted Solution

by:
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

by:tango2009
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

by:satsumo
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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

Author Comment

by:tango2009
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

by:josephtsang
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

by:satsumo
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

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

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Make the most of your online learning experience.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question