Using hue to increment java breakpoint counter
I am creating a racing game using java and getting a checkpoint counter and lap counter. I drew lines on the track to increase the breakpoint counter. When the breakpoint counter reaches 9 and they pass along the blue line, the lap counter then increases. The problem is that when the car goes over the line, the checkpoint counter or lap counter will either keep increasing or not move at all.
Player player;
float scaleFactor, translateX, translateY;
color c;
int counterlap=0;
int countercheck=0;
void setup() {
size(displayWidth, displayHeight);
frame.setResizable(true); //make the screen adjustable
background=loadImage("track.png");
player = new Player(100, 100); //initialize the player object
scaleFactor = 2;
}
PImage background; //define an image for the background
void draw() {
background(0);
imageMode(CORNER);
pushMatrix();
translate(width/2, height/2);
scale(scaleFactor);
translate(-width/2, -height/2);
pushMatrix();
translate(-player.position.x+width/2, -player.position.y+height/2);
image(background, 0, 0, background.width, background.height); //draw background
popMatrix();
popMatrix();
c = get(width/2, height/2);
println(hue(c));
player.draw(); //draws the player
text("Laps "+counterlap, width-50, 20);//lapcounter
if (countercheck>=9) {
if ((hue(c)>135)&&(hue(c)<141)) {
counterlap++;
if ((counterlap>=1)&&(countercheck>=9)) {
countercheck=0;
}
}
}
text("Checkpoint "+countercheck+"/9", width-200, 20);//checkpoint counter
if ((hue(c)>249)&&(hue(c)<254)) {
countercheck++;
}
}
class Player { //creates the player class for the player object
PVector position, velocity; //defines position & velocity vectors
float vel, accel, heading; //scalar magnitudes
PImage car;
Player(float xposition, float yposition) {
position = new PVector(xposition, yposition); //initialize everything and pass the spawn point
velocity = new PVector(0, 0);
accel=0;
car = loadImage("car.png"); //loads the image for the player
}
void draw() {
pushMatrix();
translate(width/2, height/2); //translates car origin to the position vector
scale(scaleFactor);
rotate(heading+PI); //rotates to the current heading
imageMode(CENTER); //centers the car image for better rotation
tint(color(255, 0, 255)); //colour of the car
image(car, 0, 0, 64, 40); //draws the image and resizes to 64x40
noTint(); //disable tint for remainder of frame
popMatrix();
if (up) { //acclerate when up is pressed
accel=0.15;
} else { //otherwise no accleration
accel=0;
}
if (down) { //brake/reverse
accel=-0.15;
}
if (left) {
heading-=0.04*sqrt(abs(vel)); //
}
if (right) {
heading+=0.04*sqrt(abs(vel));
}
if (!(up||down||left||right)) {
}
vel+=accel;
velocity=PVector.fromAngle(heading);
velocity.setMag(vel);
if (hue(c)==97.48252) {
vel*=0.9;
} else {
vel*=0.99;
}
velocity.limit(50);
position.add(velocity);
if (position.x>background.width) {
position.x=background.width;
velocity.x*=-0.1;
}
if (position.x<0) {
position.x=0;
velocity.x*=-0.1;
}
if (position.y>background.height) {
position.y=background.height;
velocity.y*=-0.1;
}
if (position.y<0) {
position.y=0;
velocity.y*=-0.1;
}
}
}
boolean up, down, left, right;
void mouseWheel(MouseEvent e)
{
scaleFactor *= pow(2, e.getAmount() /10F);
translateX -= e.getAmount() * mouseX / 100;
translateY -= e.getAmount() * mouseY / 100;
}
void keyPressed() {
if (keyCode == UP) {
up = true;
text("D", width-500, 20);
}
if (keyCode == DOWN) {
down = true;
text("R", width-500, 20);
}
if (keyCode == LEFT) {
left = true;
}
if (keyCode == RIGHT) {
right = true;
}
if (key == 'r')
{
scaleFactor = 2;
translateX = 0;
translateY = 0;
}
}
void keyReleased() {
if (keyCode == UP) {
up = false;
}
if (keyCode == DOWN) {
down = false;
}
if (keyCode == LEFT) {
left = false;
}
if (keyCode == RIGHT) {
right = false;
}
}
source to share
There are two problems with the current approach:
- If your player stays on the line, for each frame he stays there, you will count the knees.
- If your player goes quickly from a point on one side , a circle line to another point on the other side ... he crossed the line, but if there was no intermediate box when he was standing straight on top, you will not count the knees.
To fix this, instead of looking at "where is the player in this frame", you should check "if the player has moved in this last frame from being on one side of the line to being on top or on the other side of the line, - and he did the same for all N breakpoints before doing it. " Since this can only happen once and exactly once for each lap, you will now be counting the number of laps correctly.
This means you cannot rely on hue alone; unless you make sure that, regardless of the speed of the vehicle, it is impossible to run "from one side to the other without being directly on top" in one shot.
source to share