Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualPsychotikRabbit

Posted 19 April 2013 - 01:43 PM

You can change your CollisionDetection class to an utility class, with static-only methods and therefore you could access it from anywhere.
As CJ_COIMBRA said, you could create a new collection of Entity object and store them in the CollisionDetection class.
 

public class CollisionDetection {
    private static ArrayList<Entity> entities = new ArrayList<Entity>();

    public static boolean collides(Entity e) {
        return collidesWith(e) != null;
    }

    public static Entity collidesWith(Entity e) {
        Entity obstacle = null;

        for(Entity entity: entities) {
            //If its not the same entity
            if(entity!= e) {
                if(e.getRect().intersects(entity.getRect()) {
                    obstacle = entity;
                }
            }
        }

        return obstacle;
    }

    //Add obstacle to detect
    public static void addEntity(Entity entity) {
        entities.add(entity);
    }

    //Remove obstacle to detect
    public static void removeEntity(Entity entity) {
        entities.remove(entity);
    }
}


You could consider your walls and other obstacles as Entity. With this utility class you can initialize the detection in your init method like this:
 

CollisionDetection.addEntity(ball);
CollisionDetection.addEntity(topWall);
etc...


Then in the entities that you need to manage collision (like the ball) in the update method, after updating the position of the ball you can detect collision with any other entities and handle it.

 

//Detect if the ball (this) collides with any other entities
boolean collision = CollisionDetection.collides(this);

//Detect with which entity the ball collides
//If its not colliding with anything it will return null.
//Its your job to ensure that it will not collide with more than one entity at the time
//by reacting on collision.
Entity obstacle = CollisionDetection.collidesWith(this);

 


 


#3PsychotikRabbit

Posted 19 April 2013 - 01:39 PM

You can change your CollisionDetection class to an utility class, with static-only methods and therefore you could access it from anywhere.
As CJ_COIMBRA said, you could create a new collection of Entity object and store them in the CollisionDetection class.
 

public class CollisionDetection {
    private static ArrayList<Entity> entities = new ArrayList<Entity>();

    public static boolean collides(Entity e) {
        return collidesWith(e) != null;
    }

    public static Entity collidesWith(Entity e) {
        Entity obstacle = null;

        for(Entity obstacle : entities) {
            //If its not the same entity
            if(obstacle != e) {
                obstacle = e.getRect().intersects(obstacle.getRect());
            }
        }

        return obstacle;
    }

    //Add obstacle to detect
    public static void addEntity(Entity entity) {
        entities.add(entity);
    }

    //Remove obstacle to detect
    public static void removeEntity(Entity entity) {
        entities.remove(entity);
    }
}


You could consider your walls and other obstacles as Entity. With this utility class you can initialize the detection in your init method like this:
 

CollisionDetection.addEntity(ball);
CollisionDetection.addEntity(topWall);
etc...


Then in the entities that you need to manage collision (like the ball) in the update method, after updating the position of the ball you can detect collision with any other entities and handle it.

 

//Detect if the ball (this) collides with any other entities
boolean collision = CollisionDetection.collides(this);

//Detect with which entity the ball collides
//If its not colliding with anything it will return null.
//Its your job to ensure that it will not collide with more than one entity at the time
//by reacting on collision.
Entity obstacle = CollisionDetection.collidesWith(this);

 


 


#2PsychotikRabbit

Posted 19 April 2013 - 01:38 PM

You can change your CollisionDetection class to an utility class, with static-only methods and therefore you could access it from anywhere.
As CJ_COIMBRA said, you could create a new collection of Entity object and store them in the CollisionDetection class.
 

public class CollisionDetection {
    private static ArrayList<Entity> entities = new ArrayList<Entity>();

    public static boolean collides(Entity e) {
        return collidesWith(e) != null;
    }

    public static Entity collidesWith(Entity e) {
        Entity obstacle = null;

        for(Entity obstacle : entities) {
            //If its not the same entity
            if(obstacle != e) {
                obstacle = e.getRect().intersects(obstacle.getRect());
            }
        }

        return obstacle;
    }

    //Add obstacle to detect
    public static void addEntity(Entity obstacle) {
        entities.add(obstacle);
    }

    //Remove obstacle to detect
    public static void removeEntity(Entity obstacle) {
        entities.remove(obstacle);
    }
}


You could consider your walls and other obstacles as Entity. With this utility class you can initialize the detection in your init method like this:
 

CollisionDetection.addEntity(ball);
CollisionDetection.addEntity(topWall);
etc...


Then in the entities that you need to manage collision (like the ball) in the update method, after updating the position of the ball you can detect collision with any other entities and handle it.

 

//Detect if the ball (this) collides with any other entities
boolean collision = CollisionDetection.collides(this);

//Detect with which entity the ball collides
//If its not colliding with anything it will return null.
//Its your job to ensure that it will not collide with more than one entity at the time
//by reacting on collision.
Entity obstacle = CollisionDetection.collidesWith(this);

 


 


#1PsychotikRabbit

Posted 19 April 2013 - 01:38 PM

You can change your CollisionDetection class to an utility class, with static-only methods and therefore you could access it from anywhere.
As CJ_COIMBRA said, you could create a new collection of Entity object and store them in the CollisionDetection class.
 

public class CollisionDetection {
    private static ArrayList<Entity> entities = new ArrayList<Entity>();

    public static boolean collides(Entity e) {
        return collidesWith(e) != null;
    }

    public static Entity collidesWith(Entity e) {
        Entity obstacle = null;

        for(Entity obstacle : entities) {
            //If its not the same entity
            if(obstacle != e) {
                obstacle = e.getRect().intersects(obstacle.getRect());
            }
        }

        return obstacle;
    }

    //Add obstacle to detect
    public static void addEntity(Entity obstacle) {
        obstacle.add(obstacle);
    }

    //Remove obstacle to detect
    public static void removeEntity(Entity obstacle) {
        obstacle.remove(obstacle);
    }
}


You could consider your walls and other obstacles as Entity. With this utility class you can initialize the detection in your init method like this:
 

CollisionDetection.addEntity(ball);
CollisionDetection.addEntity(topWall);
etc...


Then in the entities that you need to manage collision (like the ball) in the update method, after updating the position of the ball you can detect collision with any other entities and handle it.

 

//Detect if the ball (this) collides with any other entities
boolean collision = CollisionDetection.collides(this);

//Detect with which entity the ball collides
//If its not colliding with anything it will return null.
//Its your job to ensure that it will not collide with more than one entity at the time
//by reacting on collision.
Entity obstacle = CollisionDetection.collidesWith(this);

 


 


PARTNERS