Java 几次反弹后球停止移动

Java 几次反弹后球停止移动,java,applet,physics,game-physics,Java,Applet,Physics,Game Physics,好的,所以我已经试着让这个球自然反弹了几个星期了,但似乎不能把它弄对。程序应该允许用户输入一个设定的重力值,然后让球根据该值反弹。这对前几次反弹有效,但阻止球是个问题。我必须故意将其移动设置为0,否则它将在适当的位置无休止地反弹,但不会在x轴上移动。这个问题只有在重力设置为15时才会发生,其他的量和情况都变糟了。球会自然弹跳,但会永远在x轴上滚动。我从来没有上过物理课,所以问题可能在物理代码中。有人知道问题在哪里吗 这是代码,我的代码- import java.applet.Applet; im

好的,所以我已经试着让这个球自然反弹了几个星期了,但似乎不能把它弄对。程序应该允许用户输入一个设定的重力值,然后让球根据该值反弹。这对前几次反弹有效,但阻止球是个问题。我必须故意将其移动设置为0,否则它将在适当的位置无休止地反弹,但不会在x轴上移动。这个问题只有在重力设置为15时才会发生,其他的量和情况都变糟了。球会自然弹跳,但会永远在x轴上滚动。我从来没有上过物理课,所以问题可能在物理代码中。有人知道问题在哪里吗

这是代码,我的代码-

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

import javax.swing.JOptionPane;

//key stuff
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;


public class StartingPoint extends Applet implements Runnable{

//key press
private static boolean wPressed = false;
public static boolean isWPressed() {
    synchronized (StartingPoint.class) {
        return wPressed;
    }
}
//for position of circle
int x = 0;
int y= 0;

//for position change
double dx = 2;
double dy = 2;

//for circle size
int rad = 11;

//image for update()
private Image i;
private Graphics gTwo;

//Physics
double grav = 15;
double engloss= .65;
double tc = .2;
double friction = .9;



@Override
public void init() {
    // sets window size
    setSize(800,600);


}

@Override
public void start() {
    //"this" refers to the implemented run method
    Thread threadOne = new Thread(this);
    //this goes to run()
    threadOne.start();

}

@Override
public void run() {

    String input = JOptionPane.showInputDialog( "How much gravity?" );
    grav=  Double.parseDouble(input);
    // sets frame rate
    while (true){
        //makes sure it doesn't go off screen x wise
        //right side
        if (x + dx > this.getWidth() - rad -1){
            x= this.getWidth() -rad -1; //blocks it from moving past boundary
            dx = -dx; //Reveres it 
        }
        //left side
        else if (x + dx < 1 + rad){
            x= 1+rad; //ball bounces from the center so it adjusts for this by adding one and rad to pad out the radius of the ball plus one pixel.
            dx = -dx; //Inverters its movement so it will bounce
        }
        //makes the ball move
        else{
            x += dx; // if its not hitting anything it keeps adding dx to x so it will move. 


        }



        //friction
        if(y == this.getHeight()-rad -1){
            dx *= friction; //every time the ball hits the bottom dx is decreased by 10% by multiplying by .9
            //Keeps it from micro bouncing for ever
            if (Math.abs(dy) < 4){ // if the speed of y (dy) is less than .4 it is set to 0

                dy= 0;

            }

            /**if (Math.abs(dx) < .00000000000000000001){ // if the speed of x (dx) is less than .00000000000000000001 it is set to 0, this value doesn't seem to matter
                dx = 0;


            }**/
        }


        //makes sure it doesn't go off screen y wise
        //down
        if (y > this.getHeight() - rad -0){ // TODO Check how getHieght is measured.
            y= this.getHeight() -rad -0;
            //makes ball loose speed.
            dy *= engloss;
            dy = -dy;

        }
        else {
            //velocity
            // tc makes grav smaller. Total of which is added to dy. To increase velocity as the ball goes down.
            dy += grav *tc;
            //gravity
            //new dy is decreased by tc + .5 multiplied by gravity and tc squared. This makes the ball bounce lower every time based on its speed
            y += dy*tc + .5*grav*tc*tc;
        }


        //frame rate
        repaint();
        try {
            Thread.sleep(17);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //end frame rate

}

@Override
public void stop() {

}

@Override
public void destroy() {

}

@Override
public void update(Graphics g) {
    //keeps it from flickering... don't know how though
    if(i == null){
        i = createImage(this.getSize().width, this.getSize().height);
        gTwo = i.getGraphics();
    }
    gTwo.setColor(getBackground());
    gTwo.fillRect(0, 0, this.getSize().width, this.getSize().height);

    gTwo.setColor(getForeground());
    paint(gTwo);

    g.drawImage(i, 0, 0, this);

    //do some thing with setDoubleBuffered


}

@Override
public void paint(Graphics g) {
    g.setColor(Color.BLUE);
    g.fillOval(x-rad, y-rad, rad*2, rad*2);
}




}
import java.applet.applet;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.util.Random;
导入javax.swing.JOptionPane;
//关键材料
导入java.awt.KeyEventDispatcher;
导入java.awt.KeyboardFocusManager;
导入java.awt.event.KeyEvent;
公共类StartingPoint扩展小程序实现可运行{
//按键
私有静态布尔wPressed=false;
公共静态布尔值isWPressed(){
已同步(StartingPoint.class){
返回wPressed;
}
}
//圆的位置
int x=0;
int y=0;
//换岗
双dx=2;
双dy=2;
//对于圆的大小
int rad=11;
//更新图像()
私人形象一;
私人图形gTwo;
//物理学
双重力=15;
双倍损失=.65;
双tc=0.2;
双摩擦=.9;
@凌驾
公共void init(){
//设置窗口大小
设置大小(800600);
}
@凌驾
公开作废开始(){
//“this”指的是实现的run方法
Thread threadOne=新螺纹(此);
//这将运行()
threadOne.start();
}
@凌驾
公开募捐{
字符串输入=JOptionPane.showInputDialog(“重力有多大?”);
grav=Double.parseDouble(输入);
//设置帧速率
while(true){
//确保它不会从x方向离开屏幕
//右侧
如果(x+dx>this.getWidth()-rad-1){
x=this.getWidth()-rad-1;//阻止它越过边界
dx=-dx;//尊敬它
}
//左侧
否则如果(x+dx<1+rad){
x=1+rad;//球从中心反弹,因此它通过添加一个和rad来调整球的半径加上一个像素。
dx=-dx;//反转其运动,使其反弹
}
//使球移动
否则{
x+=dx;//如果它没有命中任何东西,它会继续将dx添加到x中,这样它就会移动。
}
//摩擦
如果(y==this.getHeight()-rad-1){
dx*=摩擦力;//每次球碰到底部时,dx乘以.9会减少10%
//让它永远不会出现微弹跳
如果(Math.abs(dy)<4){//如果y(dy)的速度小于.4,则设置为0
dy=0;
}
/**如果(Math.abs(dx)<.00000000000000000001){//如果x(dx)的速度小于.00000000000001,则将其设置为0,该值似乎无关紧要
dx=0;
}**/
}
//确保它不会从屏幕上消失
//向下
如果(y>this.getHeight()-rad-0){//TODO检查gethight是如何测量的。
y=this.getHeight()-rad-0;
//使球速度变慢。
dy*=engloss;
dy=-dy;
}
否则{
//速度
//tc使重力变小。总重力加在dy上,以增加球下落时的速度。
dy+=重力*tc;
//重力
//新的dy减少了tc+.5乘以重力和tc的平方。这使得球每次反弹的速度都更低
y+=dy*tc+.5*grav*tc*tc;
}
//帧速率
重新油漆();
试一试{
睡眠(17);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
//结束帧速率
}
@凌驾
公共停车场(){
}
@凌驾
公共空间销毁(){
}
@凌驾
公共空间更新(图g){
//不让它闪烁…不知道怎么做
如果(i==null){
i=createImage(this.getSize().width,this.getSize().height);
gTwo=i.getGraphics();
}
setColor(getBackground());
gTwo.fillRect(0,0,this.getSize().width,this.getSize().height);
setColor(get前台());
油漆(gTwo);
g、 drawImage(i,0,0,this);
//使用setDoubleBuffered做一些事情
}
@凌驾
公共空间涂料(图g){
g、 setColor(Color.BLUE);
g、 圆角(x-rad,y-rad,rad*2,rad*2);
}
}

您应该记住swing线程模式,仅对swing方法使用swing事件线程:@DavidG How-so?我在swing中使用的唯一东西是JOptionPane,我尝试在没有它的情况下运行程序,但我仍然有相同的问题。问我没什么意义,我所做的只是编辑掉你在问题中的污蔑…@DavidG噢,谢谢。1)为什么要编写小程序?如果是老师指定的,请参考。2) 为什么要使用AWT?请参阅,了解放弃AWT使用组件而支持Swing的许多好理由。