Hack Forums
[Java] Two Circles Coliding in sperate class ? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Two Circles Coliding in sperate class ? (/showthread.php?tid=5582903)



Two Circles Coliding in sperate class ? - Most3rs2 - Mar 23, 2017

Hey, so we need to make a pinball machine kinda thing for an assignement, and i need the to do something when 2 pinballs collide, now both circles have a diiferant class to modify there speed etc. how could I call Object1 in Object2. what should i use to do this


* - ☕⚛Austin⚛☕ - Mar 23, 2017

You should have an ArrayList(Object based array with no fixed size) of all your game objects. So this is a polymorphism problem. You should probably have an interface GameObject which has tick(); and render(); and then a class called BaseGameEntity immplements GameObject. In Game Object initialize a public static final ArrayList<Game Object> gameobjects and in the constructor simply add this Object to your list.
GameObject.java
PHP Code

<?php 
public interface  GameObject {
    
void tick();
    
void render();
} 
BaseGameObject.java
PHP Code

<?php 
public class  BaseGameObject implements GameObject  {
    public static final 
ArrayList<BaseGameObject> gameObjects = new ArrayList<>();
    public 
GameObject() {
        
gameObjects.add(this);
    }
    
void tick() {
    }
    
void render() {
    }
} 

Main.java
PHP Code

<?php 
public class  Main {
    public static 
void main(String[] args) {
        new 
BaseGameObject("Red Ball", 100, 200, "ball1.png");
        new 
BaseGameObject("Blue Ball", 100, 200, "ball2.png");
        while(
running) {
            
ArrayList<BaseEntity> gameObjects = BaseEntity.gameObjects;
            for(
int i = 0; i < gameObjects.size(); i++) {
                for(
int j = 0; j < gameObjects.size(); j++) {
                    if(
distanceTo(gameObjects.get(i), gameObjects.get(j)) < gameObjects.get(i).radius + gameObjects.get(j).radius) {
                        
// Collide
                    
}
                }
            }
        }
    }
} 

This code isn't ideal for a real game I left out threading and a physics loop but it should resemble something like that.
Typed this out in edit comment so the syntax might be off.


* - Most3rs2 - Mar 23, 2017

(Mar 23, 2017 10:48 PM)☕⚛Austin⚛☕ Wrote:  You should have an ArrayList(Object based array with no fixed size) of all your game objects. So this is a polymorphism problem. You should probably have an interface GameObject which has tick(); and render(); and then a class called BaseGameEntity immplements GameObject. In Game Object initialize a public static final ArrayList<Game Object> gameobjects and in the constructor simply add this Object to your list.
GameObject.java
PHP Code

<?php 
public interface  GameObject {
    
void tick();
    
void render();
} 
BaseGameObject.java
PHP Code

<?php 
public class  BaseGameObject implements GameObject  {
    public static final 
ArrayList<BaseGameObject> gameObjects = new ArrayList<>();
    public 
GameObject() {
        
gameObjects.add(this);
    }
    
void tick() {
    }
    
void render() {
    }
} 

Main.java
PHP Code

<?php 
public class  Main {
    public static 
void main(String[] args) {
        new 
BaseGameObject("Red Ball", 100, 200, "ball1.png");
        new 
BaseGameObject("Blue Ball", 100, 200, "ball2.png");
        while(
running) {
            
ArrayList<BaseEntity> gameObjects = BaseEntity.gameObjects;
            for(
int i = 0; i < gameObjects.size(); i++) {
                for(
int j = 0; j < gameObjects.size(); j++) {
                    if(
distanceTo(gameObjects.get(i), gameObjects.get(j)) < gameObjects.get(i).radius + gameObjects.get(j).radius) {
                        
// Collide
                    
}
                }
            }
        }
    }
} 

This code isn't ideal for a real game I left out threading and a physics loop but it should resemble something like that.
Typed this out in edit comment so the syntax might be off.

Its not exactly a game, I have 5 classes, demo which had the objects, game time any anything to do with the actual game, pinballobject1 which has everything to do with the first pinball, pinballobject2 same thing as 1 but for the 2nd pinball, machine which has the size of canvas and most of the values and canvas, is there a way me can can talk on skype ?


* - ☕⚛Austin⚛☕ - Mar 23, 2017

Sorry I don't do one on one. But if your going to be making pinball it should probably ressemble a game.


* - Most3rs2 - Mar 23, 2017

ok sure but it's a verry simple game, i dont know a lot about java, there are no controls its automatic kind of thing