collision with top and bottom of tiles

This commit is contained in:
Makussu 2023-10-05 10:52:44 +02:00
parent 40e00559c6
commit c5d00cdfc2

View File

@ -13,13 +13,13 @@ float jumpcooldown = 12;
// for moving x-axis (mostly depracted)
// But dont delete yet!!
PVector movement = new PVector(0,0);
PVector direction = new PVector(0,0);
PVector pos = new PVector(220, 50);
int playerRadius = 25;
float actualPos;
PVector tilesize = new PVector(50, 50);
void setup() {
size(800,800);
@ -38,8 +38,8 @@ void draw() {
// noFill();
menu();
// play();
// menu();
play();
helper();
}
@ -80,9 +80,9 @@ void play() {
rect(pos.x, pos.y, playerRadius, playerRadius);
baseline();
// jumpToJumpheight();
newJump();
collisionTest();
cooldown();
jumpToJumpheight();
generate_tiles();
@ -92,12 +92,12 @@ void play() {
// This is basically the Level file:
// It consists out of an array, that spawns tiles
// It is also checked in collisionTest()
int[] tiles_x = { 300, 50, 600 };
int[] tiles_y = { 500, 50, 600 };
int[] tiles_x = { 300, 50, 600, 50};
int[] tiles_y = { 500, 50, 600, 200 };
void generate_tiles() {
for (int i = 0; i < tiles_x.length; i++ ) {
rect(tiles_x[i], tiles_y[i], 50, 50);
rect(tiles_x[i], tiles_y[i], tilesize.x, tilesize.y);
}
}
// TODO Collision erweitern wie in https://happycoding.io/tutorials/processing/collision-detection
@ -106,10 +106,13 @@ void collisionTest() {
if ( pos.y > baseline_y - playerRadius ) {
pos.set(pos.x, baseline_y - playerRadius);
}
// Collision with all the tiles top site
// Collision with all the tiles bottom site
for (int i = 0; i < tiles_x.length; i++) {
if ((pos.x > tiles_x[i]-25 && pos.x < tiles_x[i]+25) && pos.y < tiles_y[i] - playerRadius) {
pos.set(pos.x, tiles_y[i] - playerRadius);
if ((pos.x > tiles_x[i] - tilesize.x/2 && pos.x < tiles_x[i]+tilesize.x/2) && (pos.y < tiles_y[i] + tilesize.y/2 && pos.y > tiles_y[i] - tilesize.y/2)) {
pos.set(pos.x, tiles_y[i] + tilesize.y/2);
}
if ((pos.x > tiles_x[i] - tilesize.x/2 && pos.x < tiles_x[i]+tilesize.x/2) && (pos.y < tiles_y[i] + tilesize.y/2 && pos.y > tiles_y[i] -tilesize.y/2 - playerRadius)) {
pos.set(pos.x, tiles_y[i] - tilesize.y/2 - 20);
}
}
}