I am trying to make a Breakout clone game and try to connect the Arduino to control the LED when the ball hit the bricks
and I wrote the codes like this
void checkCollision(){
if ( ball.x > bar.x && ball.x < bar.x + bar.barWidth ){
if ( ball.y > bar.y && ball.y < bar.y + bar.barHeight ){
ball.vY = -ball.vY;
}
}
for (int i = 0; i < MAX_BLOCKS; i++){
if ( blocks[i].hitFlag == false ){
if ( ball.x > blocks[i].x && ball.x < blocks[i].x + blocks[i].blockWidth ){
if ( ball.y > blocks[i].y && ball.y < blocks[i].y + blocks[i].blockHeight ){
ball.vY = -ball.vY;
blocks[i].hitFlag = true;
this is to realize the function of disappearing after the collision between ball and bar and bricks.
and I want the LED to change color every time the collision happened so I need to send the signal to Arduino, using these codes
import processing.serial.*;
Serial serial;
void Color() {
if ( ball.x > bar.x && ball.x < bar.x + bar.barWidth ){
if ( ball.y > bar.y && ball.y < bar.y + bar.barHeight ){
serial.write("R");
}
}
for (int i = 0; i < MAX_BLOCKS; i++){
if ( ball.x > blocks[i].x && ball.x < blocks[i].x + blocks[i].blockWidth ){
if ( ball.y > blocks[i].y && ball.y < blocks[i].y + blocks[i].blockHeight ){
serial.write("L");
but the problem I found is that every time the ball hit the bricks the processing sends a lot of R or L to Arduino instead of a single R or L that I expected.
i mean although it can work and achieve the effect I want, its processing efficiency is too low, because it has to be sent many times in each collision, which leads to the collision being stuck.
I am a beginner. Maybe my mistakes are very basic. I hope someone can help me.