2 Replies - 813 Views - Last Post: 09 February 2014 - 09:16 AM Rate Topic: -----

#1 juniorcoder20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-October 13

Grid-based collision with circles

Posted 09 February 2014 - 09:05 AM

I am trying to implement grid-based collision in a 2d game with moving circles. The canvas is 400x400 pixels. Below you can see the code for my Grid class. What I want it to do is check inside which box the entities are located and then run a collision check if there are 2 or more entities in the same box.

Right now I do not know how to find the position of an entity in a specific box.
I know there are many tutorials online, but I haven't been able to find an answer to my question, because they are either written in C/C++ or use the 2d array approach.

Code snippets and other help is greatly appreciated. Thanks.
    public class Grid {

    ArrayList<ArrayList<Entity>> boxes = new ArrayList<>();
    double boxSize = 40;
    double boxesAmount = 10;
    ...
    ...
    public void checkBoxLocation(ArrayList<Entity> entities) {

          for (int i = 0; i < entities.size(); i++) {
    // Get top left coordinates of each entity         
                double entityLeft = entities.get(i).getLayoutX() - entities.get(i).getRadius();
                double entityTop = entities.get(i).getLayoutY() + entities.get(i).getRadius();
    
    // Divide coordinate by box size to find the approximate location of the entity
                for (int j = 0; j < boxesAmount; j++) {   //Select each box
                    if ((entityLeft / boxSize <= j + 0.7) && (entityLeft / boxSize >= j)) {
                        if ((entityTop / boxSize <= j + 0.7) && (entityTop / boxSize >= j)) {
                            holdingBoxes.get(j).add(entities.get(i));
                            System.out.println("Entity " + entities.get(i) + " added to box " + j);
                        }
                    }
                }
            }
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Grid-based collision with circles

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Grid-based collision with circles

Posted 09 February 2014 - 09:11 AM

You should look at using a QuadTree to handle collisions rather than a 2D ArrayList. The QuadTree makes it easier to access elements than does a 2D ArrayList.
Was This Post Helpful? 0
  • +
  • -

#3 juniorcoder20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-October 13

Re: Grid-based collision with circles

Posted 09 February 2014 - 09:16 AM

View Postmacosxnerd101, on 09 February 2014 - 09:11 AM, said:

You should look at using a QuadTree to handle collisions rather than a 2D ArrayList. The QuadTree makes it easier to access elements than does a 2D ArrayList.


I would like to do it by using this method. It's for a school assignment. Can you help me out please, I really have tried hard to do it on my own, but I'm still learning.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1