Collision with images

trying to use:

//collision detection
  boolean Colarrow(arrow other){    
    return (abs(this.x-other.x) <50 && abs(this.y-other.y)<150);
  }

this is how I’m trying to use it:

arrow phil, grill; 
  
  void setup(){
    size(700,700);
    phil = new arrow(50,50,5,5);    
    grill = new arrow(50,0,7,3);
  }
  
  void draw(){
    background(255);
    phil.update();
    grill.update();
    Col();
  } 
  //collision detection and what to do
void Col() {
  if (phil.Colarrow(grill)) {
    phil.xSpeed *= -1; 
    phil.ySpeed *= -1; 
    grill.xSpeed *= -1; 
    grill.ySpeed *= -1; 
   //boom(phil.x, phil.y);
  }
}

and yes my arrows are called phil and grill… lol

in my class I have:

class arrow
{
  //declaring variables that will be used
  PImage img1, img2, img3, img4;
  int x, y;
  int speed;
  int count = 0;

  int xSpeed;
  int ySpeed;
  
  int x1 = 0;
  int y1 = 0;
  int x2 = 150;
  int y2 = 75;
  
  int index;

  //construct the arrow
  arrow(int x, int y, int dx, int dy)
  {
    this.x = x;
    this.y = y;
    this.xSpeed = dx;
    this.ySpeed = dy;
    //pre loading images in arrow constructor
    img1 = loadImage("Up.png");
    img2 = loadImage("Down.png");
    img3 = loadImage("Left.png");
    img4 = loadImage("Right.png");
  }
  
  void update(){
    render();
    move();
  }

  //image changing method
 void render()
  {
    if (index==1)
    {
      image(img1, x, y);
      img1.resize(150, 75);
    } 
    else if (index==2)

    {
      image(img2, x, y);
      img2.resize(150, 75);
    } 
    else if (index==3)

    {
      image(img3, x, y);
      img3.resize(150, 75);
    } 
    else if (index==4)

    {
      image(img4, x, y);
      img4.resize(150, 75);
    } 
  }

  //allowing images to move around x and y axis and to bounce off walls
  void move()
  {
    x += xSpeed;
  //collsion with right wall
    if (x > width - x2  )
    {
      xSpeed *= -1;  //chnage x directoon
      index = 3; //set image-index     CORRECT
    }
    if (x < 0)
    {
      xSpeed *= -1;
      index =4; 
    }
    y += ySpeed;
    if (y > height - y2)
    {
      ySpeed *= -1;
      index = 1; 
    }
    if (y < 0)
    {
      ySpeed *= -1;
      index = 2; 
    }
  }
  //collision detection
  boolean Colarrow(arrow other){    
    return (abs(this.x-other.x) <50 && abs(this.y-other.y)<150);
  }
}

in Summary,

  • i have 4 images of arrows facing up. down ,left and right.
    -trying to get 2 of them to collide each other and face the other way
    -i thought i had the collision bit worked out but it doesnt work, :frowning:

i SWEAR, sometimes my brain switches on and tells me the answer:

arrow phil, grill; 
  
  void setup(){
    size(700,700);
    phil = new arrow(0,350,5,-3);    
    grill = new arrow(0,100,7,3);
  }
  
  void draw(){
    background(255);
    phil.update();
    grill.update();
    Col();
  } 
  //collision detection and what to do
void Col() {
  if (phil.Clash(grill)) {
    phil.xSpeed *= -1; 
    phil.ySpeed *= -1; 
    grill.xSpeed *= -1; 
    grill.ySpeed *= -1; 
   //boom(phil.x, phil.y);
  }
}

well, thank you for reading my question as I’ve answered it once again alone.

1 Like