Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java Program Help

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Java Program Help

    I am working on a program that asks me to track the position of an imaginary ball as it bounces around an imaginary box. The user gives me input for the starting position of the ball (x,y), the bottom-left boundaries of the box (xl, yl), the top right boundaries (xr, yr) and the angle. The program then needs to find where the ball will hit the box, and then print that location and find the new angle for the next point. My program currently works in most cases if the angle is less than pi. Any help is apreciated! Here is my code

    //2014-09-16
    public class BouncyBall {
    	public static void main(String args[]){
    		if (args.length != 8) {
    			System.out.println("Error: usage is X, Y, Angle, Xl, Yl, Xr, Yr,N");
    			System.exit(1);
    		}
    		double x,y,xl,yl,xr,yr,angle;
    		int n;
    		x=Double.parseDouble(args[0]);
    		y=Double.parseDouble(args[1]);
    		angle=Double.parseDouble(args[2]);
    		xl=Double.parseDouble(args[3]);
    		yl=Double.parseDouble(args[4]);
    		xr=Double.parseDouble(args[5]);
    		yr=Double.parseDouble(args[6]);
    		n=Integer.parseInt(args[7]);
    		if ((x>xr || x<xl)||(y>yr || y<yl)) {
    			System.out.println("Error: Initial point is not within the box");
    			System.exit(1);
    		}
    		System.out.printf("%f, %f\n",x,y);
    		System.out.printf("Starting angle is %f Pi.\n", angle/Math.PI);
    		double startX, startY, endX, endY;
    		startX=x;
    		startY=y;
    		if (angle==0) {
    			System.out.printf("Ball bounces back and forth between (%.2f, %.2f) and (%.2f, %.2f).\n", xr,y,xl,y);
    		}else if (angle==Math.PI) {
    				System.out.printf("Ball bounces back and forth between (%.2f, %.2f) and (%.2f, %.2f).\n", xl,y,xr,y);
    		}else if (angle==(Math.PI/2)) {
    					System.out.printf("Ball bounces up and down between (%.2f, %.2f) and (%.2f,%.2f). \n", x, yr,x, yl);
    		}else if (angle == (3*Math.PI/2)) {
    			System.out.printf("Ball bounces up and down between (%.2f, %.2f) and (%.2f,%.2f). \n", x, yl,x, yr);
    		}else {
    		for (int i=0;i<n;i++) {
    			//declare local variables
    			while (angle>(2*Math.PI)) {
    			angle=angle-(2*Math.PI);
    		}while (angle<0) {
    			angle=angle+(2*Math.PI);
    		}
    			double distance;
     
    				if(angle>=0 && angle<(Math.PI/2)) {
    				//Quadrant 1	
    				//
    				endX=getX(yr,startY, startX, angle);
    				endY=getY(xr, startY, startX, angle);
    					//Use the distance formula to find which one is closer
    				distance=getDistance(endX, endY, startX, startY, xr, yr);
    				//get new x, y and angle variables
    				if(distance==1) {
    					startX=endX;
    					startY=yr;
    					}else if (distance==2) {
    					startX=xr;
    					startY=endY;
    					}else if (distance==3){
    					startX=xr;
    					startY=yr;
    					}
    				angle=getNewAngle(distance, angle);		
    				}else if (angle>(Math.PI/2) && angle<(Math.PI)) {
    					//Quadrant 2	
    					//
    					endX=getX(yr,startY, startX, angle);
    					endY=getY(xl, startY, startX, angle);
    						//Use the distance formula to find which one is closer
    					distance=getDistance(endX, endY, startX, startY, xl, yr);
    					//get new x, y and angle variables
    					if(distance==1) {
    						startX=endX;
    						startY=yr;
    						}else if (distance==2) {
    						startX=xl;
    						startY=endY;
    						}else if (distance==3){
    						startX=xl;
    						startY=yr;
    						}
    					angle=getNewAngle(distance, angle);
    				}else if (angle>(Math.PI) && angle<((3*Math.PI)/2)) {
    					//Quadrant 3	
    					//
    					endX=getX(yl,startY, startX, angle);
    					endY=getY(xl, startY, startX, angle);
    						//Use the distance formula to find which one is closer
    					distance=getDistance(endX, endY, startX, startY, xl, yl);
    					//get new x, y and angle variables
    					if(distance==1) {
    						startX=endX;
    						startY=yl;
    						}else if (distance==2) {
    						startX=xl;
    						startY=endY;
    						}else if (distance==3){
    						startX=xl;
    						startY=yl;
    						}
    					angle=getNewAngle(distance, angle);
    				}else if (angle>((3*Math.PI)/2) && angle<(2*Math.PI)) {
    					//Quadrant 4	
    					//
    					endX=getX(yr,startY, startX, angle);
    					endY=getY(xl, startY, startX, angle);
    						//Use the distance formula to find which one is closer
    					distance=getDistance(endX, endY, startX, startY, xr, yl);
    					//get new x, y and angle variables
    					if(distance==1) {
    						startX=endX;
    						startY=yl;
    						}else if (distance==2) {
    						startX=xr;
    						startY=endY;
    						}else if (distance==3){
    						startX=xr;
    						startY=yl;
    						}
    					angle=getNewAngle(distance, angle);
    				}
     
     
    			System.out.printf("%2f, %2f\n", startX, startY);
    			System.out.printf("New angle is %f Pi\n", angle/Math.PI);
    		}
    		}
    	}
     
    public static double getY(double xMax, double startY, double startX, double angle) {
    	double endY=(Math.tan(angle)*(xMax-startX))+startY;
    	return endY;
    };
    public static double getX(double yMax, double startY, double startX,double angle){
    	double endX=((yMax-startY)/Math.tan(angle))+startX;
    	return endX;
    	};
    public static double getDistance(double endX, double endY, double startX, double startY, double xMax, double yMax){
    	double distance1=Math.sqrt(((endX-startX)*(endX-startX))+((yMax-startY)*(yMax-startY)));
    	double distance2=Math.sqrt(((xMax-startX)*(xMax-startX))+((endY-startY)*(endY-startY)));
    	if (distance1< distance2) {
    		return 1.0;
    		//hits top/bottom
    	}else if (Math.abs(distance1)>Math.abs(distance2)) {
    		return 2.0;
    		//hits left/right side
    	}
    	else {
    		return 3.0;
    		//hits corner
    	}
     
    };
    public static double getNewAngle(double distance, double angle){
    	double newAngle=0;
    	double anglePlus;
    	if (angle> 0 && angle<(Math.PI/2)){
    		//Quadrant 1
    		anglePlus=angle;
    		if(distance==1) {
    		newAngle=(2*Math.PI)-anglePlus;
    		}else if (distance==2) {
    		newAngle=(Math.PI)-angle;
    		}else if (distance == 3) {
    		newAngle=anglePlus+Math.PI;	
    		}
    	}else if (angle> (Math.PI/2) && angle<(Math.PI)){
    		//Quadrant 2
    		anglePlus=(angle-(Math.PI/2));
    		if(distance==1) {
    		newAngle=(3*Math.PI/2)-anglePlus;
    		}else if (distance==2) {
    		newAngle=(Math.PI/2)-anglePlus;
    		}else if (distance == 3) {
    		newAngle=angle+Math.PI;	
    		}
    	}if (angle> Math.PI && angle<((3*Math.PI)/2)){
    		//Quadrant 3
    		anglePlus=angle-Math.PI;
    		if(distance==1) {
    		newAngle=(Math.PI)-anglePlus;
    		}else if (distance==2) {
    		newAngle=(2*Math.PI)-anglePlus;
    		}else if (distance == 3) {
    		newAngle=angle+Math.PI;	
    		}
    	}if (angle> ((3*Math.PI)/2) && angle<(2*Math.PI)){
    		//Quadrant 4
    		anglePlus=angle-((3*Math.PI)/2);
    		if(distance==1) {
    		newAngle=(Math.PI/2)-anglePlus;
    		}else if (distance==2) {
    		newAngle=(3*Math.PI/2)-anglePlus;
    		}else if (distance == 3) {
    		newAngle=angle+Math.PI;	
    		}
    	}

    THANKS!!!
    Last edited by CWalrus; October 3rd, 2014 at 02:54 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Program Help

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Too much code improperly posted. Please post your code correctly using code or highlight tags per the above link.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program Help

    Alright, I think I fixed the forum format issue.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Program Help

    This sounds more like a math problem than a java programming problem.
    Is there a specific math expression or equation that you are having problems coding in java?
    Post it and show what you have coded to solve it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Program Help

    Can you give sample runs or the inputs used when you think it is providing the correct results and others when it is not? Show sample runs, if possible (copy from your console and paste into a post).

  6. #6
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program Help

    I am not sure if it is a problem with my math or a problem with my programming, but either way I need help. With the inputs 1 1 .392699=(Pi/8) 0 0 4 3 20 I should expect the results

    1.000000 , 1.000000
    4.000000 , 2.242640
    2.171572 , 3.000000
    0.000000 , 2.100506
    4.000000 , 0.443652
    2.928929 , 0.000000
    0.000000 , 1.213202
    4.000000 , 2.870056
    3.686287 , 3.000000
    0.000000 , 1.473090

    Instead I get the first few results correct, but it starts getting weird after about number 4. My main problem is the ball starts to get out of the confines of the box, So I think that it is either a problem with my change in angle function, or for some reason my getDistance function is returning the wrong point, because the point should never leave the box. For reference, here are my results.

    1.000000, 1.000000
    4.000000, 2.242640
    2.171572, 3.000000
    0.000000, 2.100506
    -2.171572, 0.000000
    4.000000, 2.556348
    2.928929, 3.000000
    0.000000, 1.786798
    -2.928929, 0.000000
    4.000000, 2.870056
    3.686287, 3.000000

Similar Threads

  1. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 07:16 AM
  2. convert java program to java ME program
    By abhishek1212 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 03:22 PM
  3. [SOLVED] Write a java program to display even numbers between 2 and 200 using netbeans java
    By Shalom in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 11th, 2012, 05:16 AM
  4. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  5. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM