Can anyone help me with collision for player (Platformer)

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

basically the player falls when he/she is not on a platform

ON a platform is x>leftOfPlatform && x < rightOfPlatform and y < topOfPlatform-playerHeight but if player is much higher than topOfPlatform she still falls…

if ON a platform, falling stops

// Adventures of Dingding
// By Dane MacFadden
// 2019/06/03
ArrayList<Platform> platforms; // Holds platforms
Dingding dingding; // Character object

// ----------------------------------------------------------------------------

void setup() {
  size(600, 400);
  platforms = new ArrayList();
  dingding = new Dingding();

  levelOne();
}

void draw() {
  background(255);
  PVector gravity = new PVector(0, 0.3); // Adds gravity

  dingding.applyForce(gravity); // Applies gravity to character
  dingding.display();
  dingding.update();

  for (Platform i : platforms) { // Enhanced for loop to show all platforms
    i.display();
  }
}

// ----------------------------------------------------------------------------

void levelOne() { // Creates first level
  platforms.add(new Platform(width/2, height-30, 
    width-111, 30));
}

void keyPressed() {// Lets character move
  if (key == 'a') {
    dingding.move(-5);
  }

  if (key == 'd') {
    dingding.move(5);
  }

  if (key == 'w') {
    dingding.vel.y=-9;
    dingding.isColliding=false;
  }
}

// ==========================================================================

class Dingding {

  PImage sprite;

  PVector position;
  PVector dimensions;
  PVector vel;
  boolean isColliding=false;

  Dingding() {
    position = new PVector(width/2, height/2);
    dimensions = new PVector(50, 25);
    vel = new PVector(0, 0);
    sprite = loadImage("dingding.png");
  }

  void display() {
    imageMode(CENTER);
    ellipseMode(CENTER);
    //image(sprite, position.x, position.y, dimensions.x, dimensions.x);
    fill(245, 3, 2); 
    ellipse(position.x, position.y, 
      dimensions.x, dimensions.y);
  }

  void move(float steps) { // Lets character move
    position.x += steps;
  }

  void applyForce(PVector force) { // Applies forces when the character is not touching a platform
    if (!isColliding) {
      vel.add(force);
    }
  }

  void update() {  // Updates position of character
    //if (!isColliding) 
    position.add(vel);

    for (Platform i : platforms) {
      if (i.collide(position)) {
        position.y = i.pos.y-dimensions.y;
        dingding.vel.y=0.0; 
        isColliding = true;
      }
    }
  }
}//class

// ========================================================

class Platform {

  PVector pos;
  PVector dim;

  Platform(float x, float y, 
    float pWidth, float pHeight) {
    pos = new PVector(x, y);
    dim = new PVector(pWidth, pHeight);
  }

  void display() {
    stroke(0);
    rectMode(CENTER);
    fill(0);
    noStroke();
    rect(pos.x, pos.y, 
      dim.x, dim.y);
  }

  boolean collide(PVector position) {
    //
    if (position.x > (pos.x-dim.x/2) &&
      position.x < (pos.x+dim.x/2)-1 && 
      position.y >= pos.y-dim.y/2-1 
      ) {
      return true;
    }
    return false;
  }
}//class
//

@Chrisir
thank you, i rewrite my code using ur code

1 Like