right use of boolean rect collide
in
Programming Questions
•
10 months ago
hi,
i try to use object and i want to make a game where if your hero ( rect cursor) touch the ball you loose the game (noLoop();)
i am using a very bad formulation for test if the player loose
/////////////////////////////
Mover mover;
Hero hero;
void setup() {
size(500, 500);
smooth();
background(255);
// Make Mover object
mover = new Mover();
hero = new Hero(mouseX, mouseY);
}
void draw() {
//rotateY(frameCount);
noStroke();
fill(255, 10);
rect(0, 0, width, height);
// Call functions on Mover object.
mover.update();
mover.checkEdges();
mover.display();
hero.move();
hero.display2();
hero.testloose();
}
class Mover {
PVector location2;
PVector location;
PVector velocity;
PVector velocity2;
PVector acceleration;
PVector acceleration2;
float topspeed;
float controlX, controlY;
Mover() {
location = new PVector(width/2, height/2);
location2 = new PVector(width/2-10, height/2-10);
velocity = new PVector(0, 0);
velocity2 = new PVector(0, 0);
acceleration = new PVector(0.1-controlX, 0.11-controlY);
topspeed = 10;
}
void update() {
println(location.x);
acceleration = new PVector(random(-1, 1), random(-1, 1));
acceleration.normalize();
acceleration.mult(random(2));
acceleration2 = new PVector(random(-1, 1), random(-1, 1));
acceleration2.normalize();
acceleration2.mult(random(2));
// velocity2.add(acceleration2);
velocity.add(acceleration);
velocity.limit(topspeed);
velocity2.limit(topspeed);
location.add(velocity);
location2.add(velocity2);
}
float display() {
text(location.x, 0, 0);
stroke(0);
fill(175);
ellipse(location.x, location.y, 16, 16);
ellipse(location2.x, location2.y, 16, 16);
line(location.x, location.y, location2.x, location2.y);
return location.x;
}
void checkEdges() {
if (location.x > width) {
velocity.mult(-2);
}
else if (location.x < 0) {
velocity.mult(-2);
}
if (location.y > height) {
velocity.mult(-2);
}
else if (location.y < 0) {
velocity.mult(-2);
}
if (location2.x > width) {
location2.x = 0;
}
else if (location2.x < 0) {
location2.x = width;
}
if (location2.y > height) {
location2.y = 0;
}
else if (location2.y < 0) {
location2.y = height;
}
}
}
class Hero extends Mover {
int x;
int y;
Hero(int x_, int y_) {
x = x_;
y = y_;
}
void move( ) {
x = mouseX;
y= mouseY;
}
void display2() {
rect(x, y, 20, 20);
}
void testloose() {
if (mouseX-location.x<8&&mouseX-location.x>-8&&mouseY-location.y<8&&mouseY-location.y>-8) {
noLoop();
}
}
}
///////////////////
my void testloose() is too bad because the result is totally wrong : if i move the cursor hero on the ball testloose isnt activated and sometime when the cursor isnt on the ball the game stop...
i know this boolean:
"boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2)
{
return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2);
}"
i dont understand this boolean , could you explain me how he can return if two rect collides?
how he select from!(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2)?
so much thanks for reading,
1