Subscribe to Stuck in an Infiniteloop        RSS Feed
-----

Yet More Collision

Icon Leave Comment
I couldn't stop watching the little red square bounce around yesterday, so I decided to extend the program a bit to illustrate more complex movement and collision with other moving objects. The code is at the point where if I were to add more stuff, I'd need to start wrapping my little square pieces into their own Class objects to make the code cleaner, but for two moving squares and the completion of a box it's ok for now.

With a delta for both x and y movement we can simulate complicated moves without needing to any trigonometry. For example, you can do a 90 degree reflect off of a straight surface by inverting the delta opposite of the plane, i.e. x delta for vertical walls and y delta for horizontal walls. Yay math!


There's a bug in the code below, see if you can figure out what it is...

import javax.swing.*;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.lang.Math;

class Track extends JPanel{

	private Rectangle rect;
	private Rectangle rect2;
	private Rectangle rightWall;
	private Rectangle leftWall;
	private Rectangle upperWall;
	private Rectangle lowerWall;
	//start going in different directions
	private int xdelta = 10;
	private int ydelta = 10;
	private int xdelta2 = -10;
	private int ydelta2 = -10;
	private double angle  = 0.0;

	public Track(){
		super();
		//default middle of the frame
		rect = new Rectangle(250, 250, 10, 10);
		rect2 = new Rectangle(150, 150, 10, 10);
		rightWall = new Rectangle(400, 0, 10, 500);
		leftWall = new Rectangle(50, 0, 10, 500);
		upperWall = new Rectangle(50, 0, 350, 10);
		lowerWall = new Rectangle(50, 455, 350, 10);
	}

	private boolean outOfBounds(Rectangle r){
		if (r.x > 500 || r.x < 0){
			return true;
		}
		else if (r.y > 500 || r.y < 0){
			return true;
		}

		return false;
	}

	public void paintComponent(Graphics g){

		if (rect.intersects(rightWall)){
			System.out.println("Hit right wall!");
			xdelta = -xdelta;
		}
		else if (rect.intersects(leftWall)){
			System.out.println("Hit left wall!");
			xdelta = -xdelta;
		}
		else if (rect.intersects(upperWall)){
			System.out.println("Hit upper wall!");
			ydelta = -ydelta;
		}
		else if (rect.intersects(lowerWall)){
			System.out.println("Hit lower wall!");
			ydelta = -ydelta;
		}

		if (rect2.intersects(rightWall)){
			System.out.println("Hit right wall!");
			xdelta2 = -xdelta2;
		}
		else if (rect2.intersects(leftWall)){
			System.out.println("Hit left wall!");
			xdelta2 = -xdelta2;
		}
		else if (rect2.intersects(upperWall)){
			System.out.println("Hit upper wall!");
			ydelta2 = -ydelta2;
		}
		else if (rect2.intersects(lowerWall)){
			System.out.println("Hit lower wall!");
			ydelta2 = -ydelta2;
		}

		if(rect.intersects(rect2)){
			System.out.println("Hit each other!");
			xdelta = -xdelta;
			ydelta = -ydelta;
			xdelta2 = -xdelta2;
			ydelta2 = -ydelta2;
		}

		if(outOfBounds(rect)){
			rect.x = (int)(Math.random() * 100 + 100);
			rect.y = (int)(Math.random() * 100 + 100);
			System.out.println("Resetting player");
		}
		if(outOfBounds(rect2)){
			rect2.x = (int)(Math.random() * 100 + 100);
			rect2.y = (int)(Math.random() * 100 + 100);
			System.out.println("Resetting player2");
		}

		g.clearRect(0,0,500,500);
		g.setColor(Color.RED);
		g.fillRect(rect.x, rect.y, rect.width, rect.height);
		g.setColor(Color.GREEN);
		g.fillRect(rect2.x, rect2.y, rect2.width, rect2.height);


		g.setColor(Color.BLACK);
		g.fillRect(rightWall.x, rightWall.y, rightWall.width,rightWall.height);
		g.fillRect(leftWall.x, leftWall.y, leftWall.width,leftWall.height);
		g.fillRect(upperWall.x, upperWall.y, upperWall.width, upperWall.height);
		g.fillRect(lowerWall.x, lowerWall.y, lowerWall.width, lowerWall.height);

		rect.x += xdelta;
		rect.y += ydelta;
		rect2.x += xdelta2;
		rect2.y += ydelta2;
	}
}
public class Test extends JFrame{

	private Track canvas;
	public Test(){
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setTitle("Knowles' PingPong");
		this.setSize(500,500);
		canvas = new Track();
		this.add(canvas);
		this.setVisible(true);
	}

	public void cycle(){

		while(true){
			canvas.repaint();
			try{
				Thread.sleep(100);
			}
			catch(Exception e){
				System.out.println("Barfed on sleep!");
			}
		}
	}
	public void paintComponent(Graphics g){
	}


	public static void main(String[] args){
		new Test().cycle();
	}
}



Spoiler


Picture!

Posted Image

0 Comments On This Entry

 

September 2014

S M T W T F S
 123456
78910111213
14151617181920
21 22 2324252627
282930    

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    1 user(s) viewing

    1 Guests
    0 member(s)
    0 anonymous member(s)