Prime b

Prime b

  • NA
  • 810
  • 346k

Object collision

Mar 14 2012 8:33 PM
Ok , i got kind of half way though and as usual i got stuck.
This time I need to find out if two object collide(interact with each other, on top of each other, etc)

==============Rectangle class======================
public class Rectangle
{

    private int height;
    private int width;
    private Point location;

    /**
     *
     * @param height
     * @param width
     * @param x
     * @param y
     */
    public Rectangle(int height, int width, int x, int y)
    {
        this.height = height;
        this.width = width;
        this.location = new Point(x, y);
    }

    /**
     *
     * @param height
     * @param width
     */
    public Rectangle(int height, int width)
    {
        this.height = height;
        this.width = width;
        this.location = new Point();
    }
   
    /**
     *
     */
    public Rectangle()
    {
        this.height = 0;
        this.width = 0;
        this.location = new Point();
    }

    /**
     * Returns this rectangle objects area
     * @return
     */
    public int getArea()
    {
        return height * width;
    }

    /**
     * Returns this rectangle objects parimeter
     * @return
     */
    public int getParimeter()
    {
        return height * 2 + width * 2;
    }

    /**
     * @return the height
     */
    public int getHeight()
    {
        return height;
    }

    /**
     * @param height the height to set
     */
    public void setHeight(int height)
    {
        this.height = height;
    }

    /**
     * @return the width
     */
    public int getWidth()
    {
        return width;
    }

    /**
     * @param width the width to set
     */
    public void setWidth(int width)
    {
        this.width = width;
    }

    /**
     * @return the x coord in the upper left hand cornet of this rectange object.
     */
    public int getX()
    {
        return location.getX();
    }

    /**
     * @param x the upperLeftX to set
     */
    public void setX(int x)
    {
        this.location.setX(x);
    }

    /**
     * @return the upperLeftY
     */
    public int getY()
    {
        return location.getY();
    }

    /**
     * @param y the upperLeftY to set
     */
    public void setY(int y)
    {
        this.location.setY(y);
    }
   
    public void move(int x, int y)
    {
        this.location.move(x, y);
    }
   
    public void move(Point otherLocation)
    {
        this.location.move(otherLocation);
    }
   
    public boolean collision(Rectangle other)
    {
        boolean doesOverlap = false;
       
       
        return doesOverlap;
    }


==================Point class==================
public class Point
{
    private int x;
    private int y;
   
    /**
     * tbw
     */
    public Point()
    {
        this.x = 0;
        this.y = 0;
    }
   
    /**
     * tbw
     * @param x
     * @param y
     */
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    /**
     * @return the x
     */
    public int getX()
    {
        return x;
    }

    /**
     * @param x the x to set
     */
    public void setX(int x)
    {
        this.x = x;
    }

    /**
     * @return the y
     */
    public int getY()
    {
        return y;
    }

    /**
     * @param y the y to set
     */
    public void setY(int y)
    {
        this.y = y;
    }
   
    /**
     *
     * @param x
     * @param y
     */
    public void move(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
   
    /**
     *
     * @param otherLocation
     */
    public void move(Point otherLocation)
    {
        this.x = otherLocation.getX();
        this.y = otherLocation.getY();
    }
   
    /**
     *
     * @param deltaX
     * @param deltaY
     */
    public void shift(int deltaX, int deltaY)
    {
        this.x = this.x + deltaX;
        this.y = this.y + deltaY;
    }
    ===============Main method=================
        Rectangle r1 = new Rectangle(25, 10, 100, 550);

        Rectangle r2 = new Rectangle(50, 50);

        Rectangle r3 = new Rectangle();

        System.out.println("The area of r1 is " + r1.getArea());
        System.out.println("The area of r2 is " + r2.getArea());
        System.out.println("The area of r3 is " + r3.getArea());


        System.out.println("The height of r1 is " + r1.getHeight());
        System.out.println("The area of r1 is " + r1.getArea());

        System.out.println("r1 is at x: " + r1.getX());
        System.out.println("r1 is at y: " + r1.getY());

        Point overThere = new Point(200, 200);
        r1.move(overThere);
        r2.move(overThere);
        r3.move(overThere);

        System.out.println("r1 is at x: " + r1.getX());
        System.out.println("r1 is at y: " + r1.getY());
        System.out.println("r2 is at x: " + r2.getX());
        System.out.println("r2 is at y: " + r2.getY());
        System.out.println("r3 is at x: " + r3.getX());
        System.out.println("r3 is at y: " + r3.getY());

Answers (2)