Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make the increment when two rectangles intersect predictable?
    text
    copied!<p>Long story short, I'm making a racing game in Java. I'm self-taught using some books and my knowledge of math logic and I've only been programming for three weeks so I'm still learning the ins and outs of it all. Here's some background:</p> <p>I've got a player image surrounded by a bounding rectangle and the code will check to see when that player rectangle intersects a rectangle that is the finish line. Each time it successfully intersects the line, p1Laps gets incremented. When the value reaches a certain point, the game is over and the player is declared the winner.</p> <p>Here's the problem and the questions: My problem is that Java is counting multiple intersects each time the rectangles cross. Usually 8 intersects so p1Laps gets incremented 8 times. This would not be a problem if it happened consistently, but sometimes the laps increment at different values. I've encountered increments of 4,7, and 8 so it's hard to set a value to ensure that the race will end after a certain number of laps.</p> <p>My first question is "why?" Why is java counting so many intersects when the two rectangles cross? I'm assuming it has something to do with them both being 2D shapes, but I could be wrong.</p> <p>My second question is how to make the increments happen at a consistent value? Preferably "1" but that is not paramount as I can just adjust the finishing value.</p> <p>Here is the code that seems relevant (a lot of code is removed):</p> <pre><code>import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.*; import java.applet.AudioClip; public class RacerDoom extends JFrame { //lap counter int p1Laps=0; //bouding rectangles Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2,WIDTH/30,WIDTH/30); Rectangle finishtop = new Rectangle(WIDTH/9,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5),HEIGHT/70); //check for intersect if(p1.intersects(finishtop)&amp;&amp;p1Direction==UP){ p1Laps++;} //choose winner if(p1Laps&gt;=24) { if(!winnerChosen) { winnerChosen = true; break; } } </code></pre> <p>As stated, the increments usually go up by 8, but I've had them go by 7 for (seemingly) no reason and they go up by only 4 if "Boost" is enabled (doubles the speed of player). Thanks for your help. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload