0
votes

I am programming a game of sorts which would be kinda long to explain, but in short the player is an ellipse() and follows the mouse around, whilst the rect() is the obstacle that moves down the screen and needs to be dodged by the player, otherwise it's game over. There are multiple rect as I am using an ArrayList to store each obstacle object.

Currently, the player can just pass straight through the rect without anything happening to it. I have tried to solve it multiple times but it got extremely messy and I couldn't understand much due to being extremely new to Java (only 1 week of experience), so I have just placed the empty code below.

tldr; I need to figure out how to get an ellipse/rect collision to work (in its own method). I only have 1 week of Processing/Java experience. I've cut out most of the code that you don't need to look at, mainly just kept the variables used to define the shapes and the code for the shapes just in case you need that. Also, if possible could the collision method could be placed inside the Enemy Class.

Enemy Class (all the variables used to define the rect)

class Enemy {

  int enemyNumber; // used to determine enemy type

  //VARIABLES FOR ENEMY
  boolean redEnemy = false; // determine enemy colour
  color enemyColour = color(#B9B9E8); // sets default colour to blue
  PVector position, velocity;
  float xDist, yDist; // x and y distance for Bar
  float smallCircleRad, bigCircleRad; // radius for circles

   // **************************************************************************

  Enemy() { //CONSTRUCTOR
    position = new PVector(width/2, random(-300000, -250));
    //println(position.y);
    velocity = new PVector(0, 10);
    smallCircleRad = 200;
    bigCircleRad = 400;
    xDist = width;
    yDist = 200;
    enemyNumber = int(random(1, 6));
  }

   // **************************************************************************

  void redBar(float xPos, float yPos, float xDist, float yDist) {
    redEnemy = true;
    noStroke();
    enemyColour = color(#E38585);
    fill(enemyColour);
    rect(xPos, yPos, xDist, yDist);
  }

  void blueBar(float xPos, float yPos, float xDist, float yDist) {
    redEnemy = false;
    noStroke();
    enemyColour = color(#B9B9E8);
    fill(enemyColour);
    rect(xPos, yPos, xDist, yDist);
  }

Player Class (all the variables used to define the ellipse)

class Player {

  int r = 50; //player radius
  float playerX = width/2; //starting x coordinate
  float playerY = height/2+500; //starting y coordinate
  float speed = 20; //player speed
  float angle; //angle used to calculate trajectory for player

  void playerChar() { //method for player model and general rules
    stroke(10);
    rectMode(CENTER);
    fill(playerColour);
    ellipse(playerX, playerY, r*2, r*2);
  }
1
This has been answered many times on SO and other. Google around abit. - micycle

1 Answers

0
votes

Make your life easier by treating the player as a rectangle instead of a circle. You can still draw them as a circle, but for collision detection, use a rectangle. This is called a bounding box and is very popular in collision detection.

Then you can use rectangle-rectangle collision detection, which is much simpler.

Some basic google searches return these results:

If for some reason you absolutely need the player to be a circle when calculating the collision, then I'd start by googling something like "circle rectangle collision detection".

If you still can't get it figured out, please post a MCVE in a new question and we'll go from there. Good luck.