Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Two Circles Coliding in sperate class ?

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
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
(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 ?
Sorry I don't do one on one. But if your going to be making pinball it should probably ressemble a game.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by lockheadApr 22, 2021 07:10 AM 0 147 Apr 22, 2021 07:10 AM
Last Post: lockhead
[Python] by Bora or HyunaJan 27, 2020 12:26 PM 6 182 Jan 27, 2020 12:26 PM
Last Post: Lindros
[Python] by XRiBENov 19, 2015 04:55 PM 2 194 Nov 19, 2015 04:55 PM
Last Post: Pentahedron