Results 1 to 3 of 3
Thread: Wall bug on physics simulation
- 07-14-2010, 02:26 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
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; } }
-
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 02:48 AM.
- 07-14-2010, 05:56 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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
-
Simulation of Print Server
By killingvirus in forum New To JavaReplies: 7Last Post: 05-16-2010, 04:09 PM -
Dice Simulation Program
By touandcim in forum New To JavaReplies: 3Last Post: 03-15-2010, 12:09 PM -
beehive simulation
By BlueF4re in forum New To JavaReplies: 2Last Post: 12-02-2009, 08:31 AM -
Mars Simulation Project 2.84
By Java Tip in forum Java SoftwareReplies: 0Last Post: 06-26-2008, 06:18 PM -
Pulley Simulation..HELP
By dazza-s in forum Java 2DReplies: 2Last Post: 06-18-2008, 10:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks