简体   繁体   中英

Array of bullets collision detection in slick2d

I've created a Bullet class using vectors to draw bullets and store them in an array. I've tried but couldn't figure out how to detect collision with these bullets.

Here is the Bullet class:

public void render(GameContainer gc, Graphics g) throws SlickException {

    if(active) {
        g.setColor(Color.red);
        g.fillOval(pos.getX()-10, pos.getY()-10, 20, 20);

        g.drawString("BulletX: "+pos.getX(), 400, 100);
        g.drawString("BulletY: "+pos.getY(), 400, 120);
    }
}

public void update(int t) {

    if(active) {
        Vector2f realSpeed = speed.copy();
        pos.add(speed.copy().scale(t/1000.0f));
        pos.add(realSpeed);

        lived += t;
        if(lived > MAX_LIFETIME) {
            active = false;
        }
    }
}

Here is the Play class:

I have filled the array in the init method using this code:

bullets = new Bullet[8];
    for(int i=0; i<bullets.length; i++) {
        bullets[i] = new Bullet();
    }

In the update class I've used this code to display the bullets when the player chooses to fire:

if(t > FIRE_RATE && gc.getInput().isKeyDown(Input.KEY_SPACE)) {
        if(player == movingUp) {
            bullets[current] = new Bullet(new Vector2f(420,295), new Vector2f(0,-1));
            current++; 
            if(current >= bullets.length) current = 0;
            t = 0;
        }

Here is the player and enemy movement

//up
    if(input.isKeyDown(Input.KEY_UP)) {
        player = movingUp;
        playerPositionY += delta * .3f; //moves world
        enemyPositionY += delta * .3f;
        if(playerPositionY > 273) { //collision detection
            playerPositionY -= delta * .3f;
            enemyPositionY -= delta * .3f;
        }
    }

if(enemyPositionY > shiftY) {
        enemyPositionY -= delta *.1f;
    }
    if((enemyPositionX >= 355 && enemyPositionX <= 440) && (enemyPositionY >= 230 && enemyPositionY <= 310)) {
        enemyPositionX = playerPositionX;
        enemyPositionY = playerPositionY;
        lives--;
    }

Can you help me understand how I can detect collisions with these bullets please?

Have you tried adding a Rectangle property to the bullets in the bullets class (initialize it before hand and update the position in the update method)? Something like

bulletRectangle = new Rectangle(pos.getX(), pos.getY(), 20, 20);

And then create the rectangle for the player such as playerRectangle and then using the intersect function check for the collision in a loop.

collision = false; 
for(int i = 0; i < bullets.length; i++){
    if(playerRectangle.intersects(bullet[i].getBulletRectangle())){
        collision = true;
}
//perform whatever you want to happen if bullet collides with player

Personally, I would use an array list because you don't have to set the number of bullets to a specific number, but this should work just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM