Solved

Java collision detection

Posted on 2011-03-16
7
431 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
7 Comments
 
LVL 12

Accepted Solution

by:
satsumo earned 250 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 250 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now