Created
October 3, 2018 12:32
-
-
Save halfd/a998a9a8678c24a75bd10df2154e7417 to your computer and use it in GitHub Desktop.
Simple Box Collision
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Box box1; | |
Box box2; | |
void setup() | |
{ | |
rectMode( CENTER ); | |
noStroke(); | |
size(600, 600); | |
box1 = new Box(); | |
box1.farve = color(0, 0, 200); | |
box1.x = width / 2; | |
box1.y = height / 2; | |
box1.sizeX = 200; | |
box1.sizeY = 50; | |
box2 = new Box(); | |
box2.sizeX = 70; | |
box2.sizeY = 60; | |
box2.farve = color(255, 200, 0); | |
} | |
void draw() | |
{ | |
background(0); | |
box1.show(); | |
box2.update(mouseX, mouseY); | |
box2.show(); | |
if ( | |
!( box2.x - box2.sizeX / 2 > box1.x + box1.sizeX / 2) && | |
!( box2.x + box2.sizeX / 2 < box1.x - box1.sizeX / 2) && | |
!( box2.y - box2.sizeY / 2 > box1.y + box1.sizeY / 2) && | |
!( box2.y + box2.sizeY / 2 < box1.y - box1.sizeY / 2) | |
) | |
{ | |
// Vi har et sammenstød! | |
box2.coll(); | |
} else { | |
// Der er ikke noget sammenstød! | |
box2.nocoll(); | |
} | |
} | |
/***************************************** | |
* | |
* Box Class | |
* | |
*****************************************/ | |
class Box | |
{ | |
int x; | |
int y; | |
int sizeX; | |
int sizeY; | |
color farve; | |
void update(int inX, int inY) | |
{ | |
x = inX; | |
y = inY; | |
} | |
void show() { | |
fill( farve ); | |
rect( x, y, sizeX, sizeY); | |
} | |
void coll() { | |
farve = color(255, 0, 0); | |
} | |
void nocoll() { | |
farve = color(255, 255, 255); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment