Need help making Physics simulator
I'm new at programming and I wanted to make a small physics simulator for an assignment. I have to get the program working by tonight, so I thought that someone in here might be able to help.
I am trying to add some collision detection to my program.. I have a ball that drops and I want it to react to circles, lines and triangles..
Here is my code as it is:
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseListener;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.*;
publicclass Mainextends Frameimplements MouseListener, MouseMotionListener, Runnable
{
int posx, posy;
double bally, ballx, speedy, speedx;
boolean stop =false;
Graphics g = getGraphics();
public Main(double x1,double y1,double yfart,double xfart){
ballx = x1;
bally = y1;
speedy = yfart;
speedx = xfart;
setSize(600, 500);
setTitle("Simulator^2");
setBackground(Color.WHITE);
setVisible(true);
Thread t =new Thread(this);
t.start();
this.addMouseMotionListener(this);
this.addMouseListener(this);
}
publicvoid mouseMoved(MouseEvent me)
{
Graphics g = getGraphics();
posx = me.getX();
posy = me.getY();
g.clearRect(520,40,100,20);
g.drawString("("+posx+","+posy+")", 525, 50);
}
publicvoid mouseDragged(MouseEvent me){
}
publicvoid mousePressed(MouseEvent me){
}
publicvoid mouseReleased(MouseEvent me){
}
publicvoid mouseClicked(MouseEvent me){
}
publicvoid mouseEntered(MouseEvent me){
}
publicvoid mouseExited(MouseEvent me){
}
publicvoid run()
{
Graphics g = getGraphics();
while (true){
g.setColor(Color.WHITE);
g.drawOval((int) ballx, (int) bally, 50, 50);
g.fillOval((int) ballx,(int) bally, 49, 49);
g.clearRect(525,50,550,550);
g.setColor(Color.BLACK);
g.drawLine(5, 30, 490, 30);
g.drawLine(490, 30, 490, 490);
g.drawLine(5, 30, 5, 490);
g.drawLine(5, 490, 490, 490);
g.drawString("Speedx: "+(int) speedx, 525, 70);
g.drawString("Speedy: "+(int) speedy, 525, 90);
g.drawString("Ballx: "+(int) ballx, 525, 110);
g.drawString("Bally: "+(int) bally, 525, 130);
g.drawString("Stop: "+stop, 525, 150);
//Updating the ball's position
if (stop ==false)
{
bally = bally + speedy;
ballx = ballx + speedx;
}
g.drawOval((int) ballx, (int) bally, 50, 50);
//Updating speedy
if (stop ==false){
speedy = speedy + 0.1;
}
//Updating speedx
if (speedx > 0){
//Loses more speed on the ground
if ((int) bally == 439){
speedx = speedx - 0.04;
}
//Normal "Braking"
else
speedx = speedx - 0.005;
}elseif (speedx < 0){
if ((int) bally == 439){
speedx = speedx + 0.01;
}else
speedx = speedx + 0.005;
}
//Collision Detection (walls)
if (bally + 50 >= 489){
if (speedy > 0){
speedy = ( -speedy) + 1;
}
if (speedy > 0){
speedy = 0;
stop =true;
}
}
if (bally <= 31){
bally = 31;
speedy = ( -speedy);
}
if (ballx + 50 > 490 - 2){
speedx = -1 * speedx;
speedx = speedx + 0.005;
}
if (ballx < 6){
speedx = -1 * speedx;
speedx = speedx + 0.005;
}
//Pause
try{
Thread.sleep(5);
}catch (Exception e){}
;
}
}
}
Hope someone can help...

