user11582989
user11582989

Reputation:

Any ideas on how i could create a "Object collision detection" with the code i have here?

Im trying to make a game were if the main character (gubbe) collides with the enemy (fiende) the character will take damage(Healthdisplay/HW). ive tried to make a collision detection thing in many ways, mainly if(x.intersects(y)){}. Im stuck and don't know how to solve it. ill put the main code down here VVV. if more code is needed just say and ill put in the needed code.

HW is the hp-pool were the damage should be done to.

void display is were the main character/rectangle is created.

the enemy is in another page in processing.

class gubbe {

    float x = 0;
    float y = 0;
    int HW = 20;
    //rect sidor
    int h1 = 100;
    int w1 = 100;
    //----

    //VV movement VV
    void move() {

    if (keyPressed) {
        if (key == 'w' || key == 'W') {
            //terms, move forward, Y-led.
            y -= 100;
        }
    }
    if (keyPressed) {
        if (key == 'a' || key == 'A') {
            //terms, move right, X-led.
            x -= 100;
        }
    }
    if (keyPressed) {
        if (key == 's' || key == 'S') {
            //terms, move backwards, Y-led.
            y += 100;
        }
    }
    if (keyPressed) {
        if (key == 'd' || key == 'D') {
            //terms, move left, X-led.
            x += 100;
        }
    }
}

// VV visual VV
void display() {
    //Grabb
    fill(127);
    stroke(0);
    rect(x, y, w1, h1);
}

void display2() {
    fill(127);
    stroke(0);
    rect(x, y, w1, h1);
}

// VV Edgechechning VV
void checkEdges() {
    if (x + 100 > width) {
        x -= 50;
    } else if (x < 0) {
        x += 50;
    } else if (y > height) {
        y -= 50;
    } else if (y < 0) {
        y += 50;
    } else {
    }
}

// VV Fighting VV
void Fight() {
    if (keyPressed) {

        if (key == 'm'|| key == 'M') {
            //villkor, flytta höger, X-led.
            fill(255, 0, 0);
            stroke(255, 0, 0);
            rect(x, y-100, 100, 100);
        }
    }
}

// VV  health bars VV
void BarsDisplay() {
    fill(204, 204, 204);
    stroke(204, 204, 204);
    rect(x, y-30, 100, 9);
}

void HealthDisplay() {
    fill(0, 0, 255);
    stroke(255, 0, 0);
    rect(x, y-31, HW, 3);
    !!! here there should be collision detection!!!

}

void XpDisplay() {
    fill(255, 255, 0);
    stroke(255, 255, 0);
    rect(x, y-22, 2, 1);
    //if(om dödar fiende){
    //  width+=x
    //}
    //else{
    //}
}
}

Upvotes: 1

Views: 169

Answers (2)

Rabbid76
Rabbid76

Reputation: 210878

A method in the class gubbe which checks for an intersection against an object of type fiende may look as follows (I've assuming that fiende is a rectangle and has the attributes x, y, w1, h1:

class gubbe {

    // [...]

    Boolean intersects(fiende f){

        // if intersecting then return 'true', else return 'false'    
        return this.x < f.x+f.w1 && f.x < this.x+this.w1 && 
               this.y < f.y+f.h1 && f.y < this.y+this.h1;
    }

    // [...]
}

Call the method like this:

gubbe g = new gubbe();
gubbe f = new fiende();

if (g.intersects(f)) {
   // do somthing
   // [...]
}

Explanation:

To check for a collision of to rectangles, you've to check if the rectangles are overlapping in both dimensions.

For a range from x1 to x1+w1 and a second range from x2 to x2+w2, there are the following possible cases:

Not overlapping:

x1      x1+w1
  +----+
            +----+
          x2      x2+w2
           x1      x1+w1
             +----+
  +----+
x2      x2+w2

Overlapping

x1                x1+w1
  +--------------+
       +----+
     x2      x2+w2
     x1      x1+w1
       +----+
  +---------------+
x2                 x2+w2
x1           x1+w1
  +---------+
       +----------+
     x2            x2+w2
     x1            x1+w1
       +----------+
  +----------+
x2            x2+w2

This mean, that the ranges are overlapping if

x1 < x2+w2 AND x2 < x1+w1

For 2 rectangles which are defined by origin and a size (x1, y1, w1 h1) respectively (x2, y2, w2 h2) the condition is:

x1 < x2+w2 AND x2 < x1+w1 AND y1 < y2+h2 AND y2 < y1+h1

This leads to the following function:

Boolean intersect(float x1, float y1, float w1, float h1,
                  float x2, float y2, float w2, float h2){

    // if intersecting then return 'true', else return 'false'
    return x1 < x2+w2 && x2 < x1+w1 && 
           y1 < y2+h2 && y2 < y1+h1;
}

Upvotes: 0

GitPhilter
GitPhilter

Reputation: 171

Your Objects seem to be in rectangular shape, so a collision takes place if the distance between both centers along the x-axis as well as the y-axis is smaller than half the size of each object in direction of that axis added up.

Take a piece of paper and sketch it down, then you will see it!

Upvotes: 0

Related Questions