Removing an object in an array upon collision

edited April 2017 in Questions about Code

Hi, i was wondering if anyone could help in regards to in regards collision detection void collisionDetection(){ for(int i = 0; i < aesteroidBelt.size(); i++){ Aesteroid current = aesteroidBelt.get(i);

    for(int b = 0; b < bullets.size(); b++)
    {
      Bullet currentBullet = bullets.get(b);
        if(dist()){
        bullets.remove(b);
        aesteroidBelt.remove(i);

Cant figure out the values to use for keeps telling me that booleans are not accept and only floats are.

These are the variables i have

int n=12;

PVector ship;

//Initilaizes Variables used
float bulletSpeed = 5;
float angle = 0;
float targetAngle = 0;
float rotation = 0.05;
float xSpeed = random(0.6, 2.5);
float ySpeed = random(0.6, 2.5);
float x = random(1, 590);
float y = random(1, 390);

My Aesteroids are also a random PVector every time you run this code(part which i am stuck on)

Answers

  • edited April 2017

    if(dist()){

    dist expects for parameters: currentBullet.x,currentBullet.y and x,y for the Asteroid

    to use remove() you must loop backward through the bullets. Change the for-loop

  • edited April 2017

    Forgot to remove the x and y variables.

    this is the function im using to implement the balls and i cant seem to get the x and y coordinates for them

    for (int i=0; i<n; i++)
          aesteroidBelt.add(new Aesteroid(color(255), 
          new PVector(random(height), random(width)), 
          new PVector(random(xSpeed), random(ySpeed))));
    

    I altered the code in other to use those x and y variables and all the balls started spawning in the same spot give or take a few and would stick to the walls and not bounce

  • POST YOUR ENTIRE CODE

    Normally it's x,y not y,x

  • edited April 2017

    yes

  • void collisionDetection(){
      for(int i = aesteroidBelt.size()-1; i >=0; i--)
        {
            Aesteroid current = aesteroidBelt.get(i);
    
        for(int b = bullets.size() -1; b >=0; b--)
        {
            Bullet currentBullet = bullets.get(b);
            if(dist(current.x, current.y, currentBullet.x, currentBullet.y) == 0 ){
            print("Hit");
            bullets.remove(b);
    
            aesteroidBelt.remove(i);
            }
        }
      }
    }
    

    Altered function in accordance to backwards like in the ArrayList and still no luck

  • If i add !=0 every shot will remove so im guessing theres an issue with the logical / spacing?

  • edited April 2017

    http://www.JeffreyThompson.org/collision-detection/point-circle.php

    http://studio.ProcessingTogether.com/sp/pad/export/ro.9K-kjhuONcJDZ/latest

    void checkDeath() {
      for (int lenB = bullets.size(), b = lenB; b-- != 0;) {
        final Bullet u = bullets.get(b);
    
        for (int lenA = asteroids.size(), a = lenA; a-- != 0;)
          if (u.checkHit(asteroids.get(a))) {
            asteroids.set(a, asteroids.get(--lenA));
            bullets.set(b, bullets.get(--lenB));
    
            asteroids.remove(lenA);
            bullets.remove(lenB);
    
            score += NUM;
            println("An asteroid has been pulverized!");
    
            break;
          }
      }
    }
    
  • I dont quite understand his implementation? is there anyway you could edit mine in accordance to what is wrong?

  • You need to use break after the double remove(). L-)

  • And you need to implement your own checkHit() method too. >-)

  • How would i go about that? isnt that what the dist function is?

  • edited April 2017

    Collision is 1 of the most requested themes is this forum. You can always check the old answers:
    https://forum.Processing.org/two/discussions/tagged?Tag=#collisions

    Or read about it: http://www.JeffreyThompson.org/collision-detection/index.php

    Or try to understand my own implementation: :P

    boolean checkHit(Asteroid a) {
      return Asteroid.IS_CIRCLE
        ? sq(a.x - x) + sq(a.y - y) < Asteroid.RAD_SQ
    
        : Asteroid.RECT_MODE == CORNER
        ? x >= a.x & x < a.x+Asteroid.DIM_X
        & y >= a.y & y < a.y+Asteroid.DIM_Y
    
        : x >= a.x-Asteroid.RAD_X & x < a.x+Asteroid.RAD_X
        & y >= a.y-Asteroid.RAD_Y & y < a.y+Asteroid.RAD_Y;
    }
    
  • Answer ✓

    You need to implement backward removal. For collision detection, you need to understand the basic concept. You check the distance between the two object's center against the sum of their radii. This is valid for the circular problem only.

    Kf

    void collisionDetection() {
    
    
      for (int i = aesteroidBelt.size()-1; i >=0 ; i--){  
        Aesteroid current = aesteroidBelt.get(i);
    
        for (int b = bullets.size()-1; b >=0; b--){
          Bullet currentBullet = bullets.get(b);
          if (dist(currentBullet.x, currentBullet.y, current.loc.x, current.loc.y)<(current.radius+5)) {
            bullets.remove(b);
            aesteroidBelt.remove(i);
          }
        }
      }
    }
    
  • Wow thankyou very much

Sign In or Register to comment.