Techmo
Techmo

Reputation: 23

Adding many objects in java

Correct me if i'm wrong. I am very new to java and am trying to create a simple physics engine using lwjgl. At the moment the code is a bit messy, but it starts and works correctly, and the physics work perfectly.I am trying to think of a way to allow the user to click their mouse and add a physics_object. Currently, it is possible, but with a limit, and i have to code in all the object names.

adding a physics object:

phys_square rec1 = new phys_square(100, 100, 10.0f, -1.0f, 10, 0.0f, 0.5f, 0.0f);

and the code for the phys_square class:

package quad;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class phys_square {

float velocityY;
float velocityX;
int x;
int y;
int particleSize;
float red;
float green;
float blue;
public phys_square(int xpos, int ypos, float velocity_x, float velocity_y, int size, float red, float green, float blue) { this.x = xpos; this.y = ypos; this.velocityY = velocity_y; this.velocityX = velocity_x; this.particleSize = size; this.red = red; this.green = green; this.blue = blue;} 
public void updatePos() {
    if ( this.y == 0 | this.y < 0) {
        this.velocityY *= -0.825f;
    }
    if (this.y > 0 | this.y >= this.velocityY) {
        this.y -= this.velocityY;
        this.velocityY += 0.2f;
    }

    if (this.x <= 0 | this.x + quad_main.particleSize >= 800) {
        this.velocityX *= -0.5f;
    }
    if (this.x > 0 | this.x >= this.velocityX) {
        this.x -= this.velocityX;
        if (Math.abs(this.velocityX) <= 0){
            this.velocityX -= 0.2f;
        }
    }
    if (this.x < 0) {
        this.x = 0;
    }
    if (this.x > 800 + quad_main.particleSize) {
        this.x = 800 + quad_main.particleSize; 
    }
    if (this.y < 0){
        this.y = 0;
    }
}
public void render() {
    window.particle(this.x, this.y, this.red, this.green, this.blue, this.particleSize);
}
public static void main(String[] args[]){

}
}

Is there a way to create multiple objects without manually assigning them all names? I heard in PHP and some other languages it is possible to use the value of a string as the name of another.

Upvotes: 2

Views: 83

Answers (1)

McLovin
McLovin

Reputation: 3674

Note: this answer assumes that you don't need names at all, not that you want to create names at run time. Use a hash map for that.

You can store objects in a list, such as an ArrayList. This acts like an array of objects, but is resizeable and and gives you the ability to add them at will, access them using an index, and iterate through them.


Put this with your imports:

import java.util.ArrayList;  

The following code creates your ArrayList:

ArrayList<phys_square> squaresList = new ArrayList<phys_square>();

You can add an object to your ArrayList like this:

squaresList.add(new phys_square( /* paramenters here */));

When you want to process the objects, you can iterate through your list. The following code executes methods on every element of the list.

for (phys_square sq : squaresList) {
    processPhysicsStuff(sq);
    processGraphicsStuff(sq);
}

You can view the documentation for Arraylist here. Read up, it's a very useful class.

Upvotes: 2

Related Questions