Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java collision detection

Posted on 2011-03-16
7
Medium Priority
?
438 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression
Suggested Courses

636 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