Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

6
Collision Detection with Two Player Objects Help
Post Flair (click to view more posts with a particular flair)
Post Body

I am creating a 2D Java game that will have two players going against each other for the most points. The issue I am running into is when either player fires a bullet that player takes damage. Currently, the bullet is doing damage, but it should not be damaging itself to fire. Originally I was using the method intersect to detect the collision, but the results were basically the same with the exception of the players taking damage faster.

I believe my issue is I need a way to set who owns the bullet, but my thought process is I can use the enumeration class to have the ID set to Player and Player 2. This process has been working for collisions of a bullet at the enemy, bullet at the wall, and enemy at the player. I provided my attempt at handling the collision of the bullet into the player and also the method that creates the bullet for the player that shot.

    for (int i=0; i<handler.object.size(); i  ) {
        GameObject tempObject = handler.object.get(i);

        if (tempObject.getId() == ID.Bullet) {
            // Player 1 hit with bullet
            if (id == ID.Player) {
                if (getBounds().contains(tempObject.getBounds())) {
                    System.out.println("Player 1 Health: "   game.hpPlayer1);
                    game.hpPlayer1--;
                    System.out.println("Player 1 Health: "   game.hpPlayer1);
                }
            }
            // Player 2 hit with bullet
            if (id == ID.Player2) {
                if (getBounds().contains(tempObject.getBounds())) {
                    System.out.println("Player 2 Health: "   game.hpPlayer2);
                    game.hpPlayer2--;
                    System.out.println("Player 2 Hit: "   game.hpPlayer2);
                }
            }
        }
    }

    // Creates the bullet for the player that shot
    private void shootBullet() {
        // Limits the tank to one shot per second
        if (System.currentTimeMillis() - LastFired > 1000) {
            // Player 1
            if (id == ID.Player) {
                if (game.ammoPlayer1 >= 1) {
                    handler.addObject(new Bullet(x   20, y   19, ID.Bullet, handler, ss, angle));
                    LastFired = System.currentTimeMillis();
                    game.ammoPlayer1--;
                }
            }
            // Player 2
            if (id == ID.Player2) {
                if (game.ammoPlayer2 >= 1) {
                    handler.addObject(new Bullet(x   20, y   19, ID.Bullet, handler, ss, angle));
                    LastFired = System.currentTimeMillis();
                    game.ammoPlayer2--;
                }
            }
        }
    }

Author
Account Strength
80%
Account Age
7 years
Verified Email
Yes
Verified Flair
No
Total Karma
89
Link Karma
63
Comment Karma
26
Profile updated: 5 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
5 years ago