Colliding images change direction?

edited November 2017 in Questions about Code

I'm trying to make a program where the image changes direction when two images collide.

I want to use this code but I'm uncertain where I put it? Also what do I substitute for ob1 (object1) and x1 and where do I put the width and height variables? Thanks

 boolean Collision()

  ( ob1 - x1, ob1 -y1, ob1 - w, ob1 - h,
    ob2 - x1, ob2 -y1, ob2 -w, ob2-h)

  return 
  (ob1 - x1 < ob2 - x2 && ob1 - x2 > ob2 = x1 &&  //insert y variables








//start of code


    class Bouncy 
    {

      int x;
      int y;
      int dx; 
      int dy;

      PImage nw, ne, sw, se;

      Bouncy(int x, int y, int dx, int dy) 
      {
        this.x = x;
        this.y = y;
        this.dx = dx;
        this.dy = dy;
    nw = loadImage("NorthW.png");
    ne = loadImage("NorthE.png");
    sw = loadImage("SouthW.png");
    se = loadImage("SouthE.png");
      }


      void update() 
      {
        render();
        move();
      }

      void render() 
      {
    if (dx == -1 && dy == -1)
      image(nw,x,y);

    else if (dx == 1 && dy == -1)
    image(ne,x,y);

    else if (dx == -1 && dy == 1)
    image(sw,x,y);

    else if (dx == 1 && dy == 1)
    image(se,x,y);
          }

      void checkCollisions() 
      {
        int edge = 65; // half width of one of the PNG files

        if (y<=(edge-edge)) // hit top border
     dy=1; // switch to moving downwards

    if (y>=500-(edge*2)) // hit bottom border
     dy=-1; // switch to moving upwards

        if (x<=edge-edge) // hit left border
    dx=1; // switch to moving right

    if (x>=500-(edge*2)) // hit right border
    dx=-1;
      }



      void move() 
      {
    checkCollisions();
    x += dx;
    y += dy;

      }
    }

    Bouncy janet,jeff,jerry;

    void setup() 
    {
      size(500,500);
      janet = new Bouncy(10,100,1,-1);
      jeff = new Bouncy(10,150,-1,1);
      jerry = new Bouncy(10,350,1,1);

    }

    void draw() 
    {
      background(255);
      janet.update();
      jeff.update();
      jerry.update();

    }

Answers

Sign In or Register to comment.