Results 1 to 3 of 3
  1. #1
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Question Wall bug on physics simulation

    Im creating a simulation whit gravity and a ball in a confined box. You can also control the ball. Sometimes the ball gets stuck in the wall and slowly goes through it. I will post the code. is there any way to make so that there is no way for the ball to pass the wall.
    Java Code:
                 [COLOR="Red"]This is the main class[/COLOR]
    
    import processing.core.PApplet;
    
    public class gravity extends PApplet 
    {
    	
    	
    	 Ball b1;
    
    	public void setup()
    	{
    		size(500,600);
    		background(1);
    		b1 = new Ball((float)0.2,0,50,50);
    	}
    
    	
    	public void draw()
    	{
    		
    		b1.speed(width, height);
    		fill(34,240,30);
    		ellipse(b1.Xget(),b1.Yget(),30,30 );
    		rect(5,580,500,2);
    		
    		
    		System.out.println( b1.Yget() );
    	
    	}
    	
    	public void keyPressed()
    	{
    		if(key == CODED)
    	
    		{
    			if(keyCode == LEFT)
    			{
    				b1.speedX -= 0.5;
    			
    			}
    			
    			if(keyCode == RIGHT)
    			{
    				b1.speedX += 0.5;
    			}
    			
    			if(keyCode == UP)
    			{
    				b1.speedY -= 0.5;
    			}
    		
    			if(keyCode == DOWN)
    			{
    				b1.speedY += 0.5;
    			}
    		}
    	}
    
    
    	
    }
    Java Code:
              [COLOR="Red"]This is the Ball class[/COLOR]
    
    import processing.core.PApplet;
    public class Ball extends PApplet
    {	
    	
    	float x;
    	float y;
    	
    	float speedX;
    	float speedY;
    	float gravity = (float)0.03;
    	float friction = (float)-0.9;
    	
    	public Ball(float spdX,float spdY, float xIn, float yIn)
    	{
    		speedX = spdX;
    		speedY = spdY;
    		x = xIn;
    		y = yIn;
    	}
    	
    	public Ball()
    	{
    		speedX = 0;
    		speedY = 0;
    		x = 0;
    		y = 0;
    	}
    	
    	public void speed(int width,int height)
    	{
    		y += speedY;
    		x += speedX;
    		
    		
    		if(speedY>= 6)
    		{
    			speedY = 6;
    		}
    		
    		if(y < 590)
    		{
    			speedY += gravity;
    		}
    		
    		else if(y>= 590)
    		{
    			speedY = speedY * friction;
    		}
    		
    		else
    			speedY += gravity;
    		
    		
    		if(x >= 490 || x <= 0)
    		{
    			speedX = speedX * friction;
    		}
    		
    	}
    
    	public float Xget()
    	{
    		return x;
    	}
    	
    	public float Yget()
    	{
    		return y;
    	}
    	
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem is that you forgot to turn off quantum mechanical tunneling.

    But seriously, if you are having your ball bounce off of walls, it is useful to use Math.abs(...) so that you're sure that the speed vector is going in the correct direction. For instance if x is less than or equal to min, make sure that speed in the x direction is positive:
    Java Code:
    speedXDirection = Math.abs(speedXDirection);

    Similarly if x plus width of ball is greater than or equal to maximum, make sure that the speed in the x direction is negative:

    Java Code:
    speedXDirection = -Math.abs(speedXDirection);
    Last edited by Fubarable; 07-14-2010 at 03:48 AM.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    Your problem is that you forgot to turn off quantum mechanical tunneling.
    I wasn't aware there was a Java setting for that! Must look that up! :rolleyes:


    Besides the worrying prospect of a quantum mechanics library, the only thing I can mention that Fubarable didn't is that floats are damn risky... I personally always use doubles - the precision is much better. You could get a few odd occurences (probably not noticeable, but you never know) of balls bouncing a bit further from the wall than they should, and other collisions occuring slightly off what would be 'normal'.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Simulation of Print Server
    By killingvirus in forum New To Java
    Replies: 7
    Last Post: 05-16-2010, 05:09 PM
  2. Dice Simulation Program
    By touandcim in forum New To Java
    Replies: 3
    Last Post: 03-15-2010, 01:09 PM
  3. beehive simulation
    By BlueF4re in forum New To Java
    Replies: 2
    Last Post: 12-02-2009, 09:31 AM
  4. Mars Simulation Project 2.84
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 06-26-2008, 07:18 PM
  5. Pulley Simulation..HELP
    By dazza-s in forum Java 2D
    Replies: 2
    Last Post: 06-18-2008, 11:51 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
  •