player p;
ArrayList<Platform> platforms = new ArrayList();
boolean[] keys = new boolean[2];
int[][] map = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
void setup() {
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 8; j ++) {
if (map[j][i] == 1) {
Platform pl = new Platform(i*50+25, j*50+25, 50, 50);
platforms.add(pl);
}
}
}
size(600, 400);
p = new player();
for (int i = 0; i < platforms.size(); i++) {
platforms.get(i).show();
}
}
void draw() {
rectMode(CENTER);
background(200);
p.show();
p.fall(0.1);
p.collision();
p.movement();
for (int i = 0; i < platforms.size(); i++) {
platforms.get(i).show();
}
}
void keyPressed() {
if (key == 'a') {
keys[0] = true;
}
if (key == 'd') {
keys[1] = true;
}
}
void keyReleased() {
if (key == 'a') {
keys[0] = false;
}
if (key == 'd') {
keys[1] = false;
}
}
class player {
float a = 5;
float Yspeed = 0.2;
float Xspeed = 5;
float x = 100;
float y = 50;
float size = 50;
void show() {
rectMode(CENTER);
fill(0);
rect(x, y, size, size);
noFill();
}
void fall(float gravity) {
Yspeed += gravity;
y += Yspeed;
}
void movement() {
Xspeed = a;
}
void collision() {
for (int i = 0; i < platforms.size(); i++) {
if (y + 50 > platforms.get(i).y && y - 50 < platforms.get(i).y && x + 50 > platforms.get(i).x && x - 50 < platforms.get(i).x) {
Yspeed = -0.1;
}
if (x - 25 <= platforms.get(i).x + 25) {
a = 0;
} else {
a = 5;
}
if (x - 25 <= platforms.get(i).x + 25) {
a = 0;
} else {
a = 5;
}
}
}
}
class Platform {
float Width;
float Height;
float x;
float y;
Platform(float X, float Y, float w, float h) {
Width = w;
Height = h;
x = X;
y = Y;
}
void show() {
fill(40, 40, 150);
rect(x, y, Width, Height);
noFill();
}
}
idk how to do a 2d collision, thank you in advance