-1

I am trying to create a basic falling sand game in java, I have each particle store it's X and Y in a Point array. Each Point array is specific to it's element, so sand and water are two different arrays, what i would like to know is how do i check my sand array against itself to see if two particles have the same position, and if so, move them?

This is my Sand Class

    package testingzone;

import java.awt.Point;
import java.util.Stack;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public class Sand {

    public Graphics g = new Graphics();
    public float sizeX = 1;
    public float sizeY = 1;
    public int speed = 1;
    public String BaseState = "fall";
    public String state = "fall";
    public String Name = "Sand";
    public Point[] point = new Point[(600 * 800)];
    public Point[] pointcheck = new Point[(600 * 800)];
    public int num = 0;
    public boolean check = false;

    public org.newdawn.slick.Color c = org.newdawn.slick.Color.yellow;

    public void drawParticle(float x, float y) throws SlickException{
        g.setColor(c);
        g.drawRect(x, y, sizeX, sizeY);
    }

    public void createParticle() throws SlickException{
        if(state == "fall"){
            fall(point);
        } else {

        }
        if(state == "stay"){
            stay(point);
        } else {

        }
    }

    public void fall(Point[] point) throws SlickException{
        for (int index = 0; index < point.length; index++) {
            Point p = point[index];
            if (p != null) {
                if (p.y >= 598) {
                    drawParticle(point[index].x, 598);
                } else {
                    drawParticle(p.x, p.y);
                    p.y += speed;
                }
              }
            }
          }

    public void stay(Point[] point) throws SlickException{
        for (int index = 0; index < point.length; index++) {
            Point p = point[index];
            if (p != null) {
                drawParticle(p.x, p.y);
                }
              }
            }

}

And this is a simplified version of my main class

    package testingzone;
import java.awt.Point;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Control extends BasicGameState {
    public static final int ID = 2;

    public Sand s = new Sand();

    public Run run = new Run();
    int maxnum = (800 * 600);
    int pressedX;
    int pressedY;
    int num = 0;
    String Build = "1.4";
    String Element = "sand";
    String name;
    public boolean endgame = false;


    public void init(GameContainer container, StateBasedGame game) throws SlickException{
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        if(Element == "sand"){
            s.createParticle();
            name = s.Name;
        }
        g.setColor(Color.yellow);
        g.drawRect(240, 10, 15, 15);
        g.fillRect(240, 10, 15, 15);
        if(Element == "sand"){
            // g.setColor(Color.white);
            // g.drawRect(235, 5, 25, 25);
            g.drawLine(235, 35, 260, 35);
        }
        g.setColor(Color.white);
        g.drawString("Open Sand", 700, 0);
        g.drawString("Build: " + Build, 700, 15);
        g.drawString("Total Pixels: " + num, 10, 25);
        g.drawString("Current Type: " + name, 10, 40);
        g.drawString("Mouse X: " + Mouse.getX(), 10, 55);
        g.drawString("Mouse Y: " + Mouse.getY(), 10, 70);
        }


    public void update(GameContainer container, StateBasedGame game, int delta) {
        if(endgame == true){
            container.exit();
        }
    }

    public void mouseDragged(int oldx, int oldy, int newx, int newy) {
        pressedX = newx;
        pressedY = newy;
        num = num + 1;
        if(Element == "sand"){
            s.num = s.num + 1;
            s.point[s.num] = new Point(pressedX, pressedY);
            s.pointcheck[s.num] = new Point(pressedX, pressedY);
        }
    }

    public void keyPressed(int key, char c) {
        if (key == Input.KEY_ESCAPE) {
            endgame = true;
        }
        if (key == Input.KEY_1) {
            Element = "sand";
    }

    public int getID() {
        return ID;
    }

}

user1610541
  • 111
  • 1
  • 11
  • There's not enough context here for anyone to figure out what you're asking. You need to provide some code and examples showing what you want to accomplish, or risk getting downvoted and closed as "not a real question". – Jim Garrison Oct 13 '12 at 05:29

2 Answers2

0

I'm not sure if I understood your question..
check if this is what you want..

// These loops will make each of the points compare to each other..
Point sands[] = new Point[10];
for ( int i = 0; i < sands.length - 1; i++ ) {
    for ( int k = i + 1; k < sands.length; k ++ ) {
        if ( sands[i].equals(sands[k]) ) {
            // Move Them
        }
    }
}
sam
  • 526
  • 1
  • 6
  • 11
  • I put that into a method called check and ran check every time a new particle was created and told it to draw true or false depending on the outcome. it stays true until i click then it just crashes :L – user1610541 Oct 13 '12 at 05:20
  • when it crashes, do you get any stacktrace ? can you post some code of where you think it might be the problem? – sam Oct 13 '12 at 05:34
0

A suggestion: Particle class has a Point (x-y coordinates). Implement equals method of the particle

boolean equals(Object o){
   if(! o instanceof Particle) return false;
   if(o.getPoint().equals(this.getPoint())) return true;

Now implement equals for Point class something like:

    boolean equals(Object o){
       if(! o instanceof Point) return false;
       if(o.getXCoordinate.equals(this.getXCoordinate()) 
&& o.getYCoordinate.equals(this.getYCoordinate())) return true;

You can check indexes if you want instead of having getXCoordinate and getYCoordinate methods on the Point class.

Atul
  • 2,673
  • 2
  • 28
  • 34
  • Because the Point is an array i cant get the X and Y to check. – user1610541 Oct 13 '12 at 05:25
  • @user1610541 So there is an array of java.awat.Point. And you want to compare if two elements in that array are same? Is this correct? If that is so, the Point class provides equals method http://docs.oracle.com/javase/6/docs/api/java/awt/Point.html – Atul Oct 13 '12 at 11:25