Distance function help
in
Programming Questions
•
11 months ago
At the moment I'm working on using the dist() function for a game I have, and this is a bit of side code I'm working on to figure this out. Presently it is not working properly. I want a red square to appear when the mouse controlled line gets within a certain range...... but I don't know how to give the distance function a range.
Is there a way to program in something like if (pos1 <=20 to pos2) then perform action?
Code is below. Changing <= will cause the square to always appear. Changing it to == and it will not appear even when I try to make them equal in program.
Is there a way to program in something like if (pos1 <=20 to pos2) then perform action?
Code is below. Changing <= will cause the square to always appear. Changing it to == and it will not appear even when I try to make them equal in program.
- int startX;
int startY;
float endX;
int endY;
float posX; //mouse X position of player
int posY; //mouse Y position of player
int fireX;
int fireY;
void setup() {
size(700,500);
startX = 100;
startY = 200;
endX = 450;
endY = 450;
}
void draw() {
background(255);
smooth();
strokeWeight(2.5);
stroke(255,0,0);
line(startX, startY, endX, endY);
float antidist = dist(fireX, fireY, posX, posY);
float misdist = dist(startX, startY, endX, endY);
if (antidist <= misdist) {
fill(255,0,0);
rect(50,100,100,50);
} else{
}
}
void mousePressed() {
fireX = 250;
fireY = 480;
posX = mouseX;
posY = mouseY;
smooth();
strokeWeight(3);
stroke(255,0,0);
line(fireX, fireY, posX, posY);
}
1