Results 1 to 5 of 5
  1. #1
    Ypsilon IV is offline Member
    Join Date
    Apr 2009
    Location
    Earth
    Posts
    14
    Rep Power
    0

    Default [SOLVED] Bouncing ball with polar coordinates

    Hi everyone! I've looked through the forums but still I couldn't find similar problem.
    Basically, what I want is:
    1. balls bouncing from the "walls"
    2. balls bouncing from each other

    I have managed to write a function that detects collisions of the balls and changes the speed and directions. Howerver, I have a problem with making balls bouncing from the wall. I want the to hit the wall and then to fly away with the angle which is equal to the angle of the hit. But in my program, they fly away with the same angle after collision with the "walls".
    Sorry for the messy code, I still have much work to do (I use Eclipse Europa, each class is in separate file):
    Java Code:
    package test;
    
    import java.awt.*;
    import java.lang.*;
    
    public class Ball {
    	float x, y;
    	float radius;
    	float speed;
    	float Angle;
    	Color color;
    	public static Graphics g;
    	
    	public Ball (float x, float y, float r, float s, float a, Color c) {
    		this.x = x;
    		this.y = y;
    		this.radius = r;
    		this.speed = s;
    		System.out.print(a);
    		System.out.print("  ");
    		this.Angle = (float)Math.toRadians(a);
    		if(Angle > 3.14 || Angle < -3.14) {
    			
    		}
    		System.out.print(Angle);
    		System.out.print("  ");
    		this.color = c;
    	}
    	
    	public double getSpeedX() {
    		return speed * (float)Math.cos(Angle);	
    	}
    	
    	public double getSpeedY() {
    		return speed * (float)Math.sin(Angle);
    	}
    	
    	public void move() {
    		x += getSpeedX(); y += getSpeedX();
    
    		if((x < radius)) {
    			System.out.print("west  ");
    			Angle = ((float)(Math.PI) - Math.abs(Angle));
    		}
    		if((y < radius)) {
    			System.out.print("north  ");
    			System.out.print(Angle);
    			System.out.print("  ");
    			//Angle = ((float)(Math.PI)-  Math.abs(Angle));
    			float temp = (float)Math.PI / Angle;
    			Angle = (temp-1)/temp;
    			System.out.print(Angle);
    			//System.out.print(Angle);
    			//System.out.print("  ");
    		}
    		if(x > (400 - radius)) {
    			System.out.print("east  ");
    			Angle = ((float)(Math.PI) - Math.abs(Angle));
    		}
    		if(y > (400 - radius)) {
    			System.out.print("south  ");
    			Angle = ((float)(Math.PI) - Math.abs(Angle));
    		}
    	}
    	
    	public static boolean collide(Ball b1, Ball b2) {
    		float TempX, TempY;
    		if(b1.x > b2.x) 
    			TempX = b1.x - b2.x - 1;
    		else
    			TempX = b2.x - b1.x - 1;
    		if(b1.y > b2.y)
    			TempY = b1.y - b2.y - 1;
    		else
    			TempY = b2.y - b1.y - 1;
    		
    		int dDistance = (int)Math.sqrt((double)(TempX * TempX + TempY * TempY));
    
    		if(dDistance > (int)(b1.radius + b2.radius))
    			return false;
    System.out.print("collision!");
    		float Alpha = ((float)Math.atan2(TempY, TempX));
    		
    		float v1P = b1.speed * (float)Math.cos(Alpha - b1.Angle);
    		float v1Q = b1.speed * (float)Math.sin(Alpha - b1.Angle);
    		float v2P = b2.speed * (float)Math.cos(Alpha - b2.Angle);
    		float v2Q = b2.speed * (float)Math.sin(Alpha - b2.Angle);
    
    	    double v3P = ((b1.radius - b2.radius) * v1P + 2.0f * b2.radius * v2P) / (b1.radius + b2.radius);
    	    double v4P = (2.0f * b1.radius * v1P + (b2.radius - b1.radius) * v2P) / (b1.radius + b2.radius);
    	 
    	    double v3Q = ((b1.radius - b2.radius) * v1Q + 2.0f * b2.radius * v2Q) / (b1.radius + b2.radius);
    	    double v4Q = (2.0f * b1.radius * v1Q + (b2.radius - b1.radius) * v2Q) / (b1.radius + b2.radius);
    	    
    	    b1.speed = (float)Math.sqrt(v3P * v3P + v3Q * v3Q);
    	    b2.speed = (float)Math.sqrt(v4P * v4P + v4Q * v4Q);
    	    
    	    b1.Angle = Alpha - (float)Math.atan2(v3Q, v3P);
    	    b2.Angle = Alpha - (float)Math.atan2(v4Q, v4P);
    	    
    	    return true;
    	}
    	
    	public void paint(Graphics g) {
    		g.setColor(color);
    		g.fillOval((int)(x - radius), (int)(y - radius), (int)(2*radius), (int)(2*radius));
    	}
    }
    Java Code:
    package test;
    
    import java.applet.*;
    import java.awt.*;
    
    
    public class Go extends Applet implements Runnable {
    	Thread thread;
        Ball ballx;//, bally;//, ballz;
        Image dbImage; 
        Graphics dbg;
        
    	public void init() { 
    		setSize(400,400);
           //ballx = new Ball(90, 140, 25, 1, 25, Color.red);
            ballx = new Ball(200, 100, 25, 5, 190, Color.red);
            // public Ball (float x, float y, float r, float s, float a, Color c)
            //bally = new Ball(365, 30, 25, 1, 25, Color.blue);
            //bally = new Ball(185, 180, 25, 4, 50, Color.blue);
            //ballz = new Ball(355, 350, 25, 3, 50, Color.yellow);
    	}
    	
    	public void start() {
    		if (thread == null) {
    			thread = new Thread(this);
    			thread.start();
    		}
    	}
    	
    	public void stop() {
    		if (thread != null) {
    			thread.stop();
    			thread = null;
    		}
    	}
    	
    	public void destroy() {
    		
    	}
    	
    	public void run() {
    		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    		while (true) {
    			//ballx.collide(bally);
    			//Ball.collide(ballx, bally);
    			//Ball.collide(ballx, ballz);
    			//Ball.collide(bally, ballz);
    			ballx.move();
    			//bally.move();
    			//ballz.move();
    			repaint();
    			
    			try {
    				thread.sleep(20);
    			}
    			catch(Exception e) {
    				
    			}
    			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    		}
    	}
    	
    	public void update(Graphics g) {
    		if (dbImage == null) { 
    			dbImage = createImage (this.getSize().width, this.getSize().height); 
    			dbg = dbImage.getGraphics (); 
    			} 
    		dbg.setColor (getBackground ()); 
    		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); 
    		dbg.setColor (getForeground()); 
    		paint (dbg); 
    		g.drawImage (dbImage, 0, 0, this);
    		}
    	
    	public void paint(Graphics g) {
    		ballx.paint(g);
    		//bally.paint(g);
    		//ballz.paint(g);
    	}
    }
    Java Code:
    package test;
    
    import java.awt.Dimension;
    
    import javax.swing.JApplet;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class NewJApplet extends javax.swing.JApplet {
    
    	/**
    	* Auto-generated main method to display this 
    	* JApplet inside a new JFrame.
    	*/
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				JFrame frame = new JFrame();
    				NewJApplet inst = new NewJApplet();
    				frame.getContentPane().add(inst);
    				((JComponent)frame.getContentPane()).setPreferredSize(inst.getSize());
    				frame.pack();
    				frame.setVisible(true);
    			}
    		});
    
    	}
    	
    	public NewJApplet() {
    		super();
    		initGUI();
    	}
    	
    	private void initGUI() {
    		try {
    			setSize(new Dimension(400, 300));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    }
    Thanks in advance!
    Last edited by Ypsilon IV; 04-24-2009 at 02:10 PM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    personally, i'd rather use direction along each axis for movement instead of messing with angles. makes it all much easier.

  3. #3
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Check out your code here:

    Java Code:
         public void move() {
    		x += getSpeedX(); y += getSpeedX();
    I think you want it to be:

    Java Code:
         public void move() {
    		x += getSpeedX(); y += getSpeedY();

    Then you also want to reverse your Angle when you hit the bounds of your frame...

    Java Code:
    if (x > (400 - radius )  || x < 0) {
                //System.out.print("east  ");
                Angle = (((float) (Math.PI) - Math.abs(Angle)))*-1;
                
            }
            if (y > (400 - radius)) {
                //System.out.print("south  ");
                Angle = (((float) (Math.PI) - Math.abs(Angle)))*-1;
               
                //System.out.println("Y: "+y);
            }

    I multiplied your equasion by "-1" to reverse it...

    Overall pretty cool program... It is neat when they collide...
    Last edited by markw8500; 04-21-2009 at 09:34 PM.
    Who Cares... As Long As It Works...

  4. #4
    Ypsilon IV is offline Member
    Join Date
    Apr 2009
    Location
    Earth
    Posts
    14
    Rep Power
    0

    Default

    markw8500, thank you very very much! It works! :)

  5. #5
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

Similar Threads

  1. How to create a bouncing ball animation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:43 PM
  2. high bouncing ball
    By bouncingball in forum Reviews / Advertising
    Replies: 1
    Last Post: 06-19-2008, 11:21 AM
  3. Replies: 0
    Last Post: 04-16-2008, 10:55 PM
  4. bouncing ball issue
    By adam405 in forum New To Java
    Replies: 1
    Last Post: 03-18-2008, 03:48 AM
  5. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •