让球弹起并最终静止

发布于 2024-12-29 10:09:03 字数 2130 浏览 0 评论 0原文

我编写了一些代码来使用方向传感器在屏幕上移动球。我想让球在撞击屏幕底部时弹起,有点像在重力作用下。有人可以帮助我在现有代码中实现物理原理吗?翻转速度似乎不起作用。这是我的球课:

package perseus.gfx.test;

import everything

public class Ball extends View  {   
RectF lol;
Paint paint, lpaint;
Bitmap bitmap;
Canvas canvas;
private float ballx = 150; 
private float bally = 140;
private double speedx = 0;  
private double speedy = 0; //ignore  
private double accx, accy=0;
private float rad = 20;
private float mult = 0.5f;
private double xv, yv, xS, yS;
int width, height;
int xmax, ymax;
int xmin, ymin;

public Ball(Context context) {
    super(context);
    lol = new RectF();
    paint = new Paint();
    paint.setColor(Color.CYAN);
    lpaint = new Paint();
    lpaint.setColor(Color.GRAY);                
}
public void moveBall()  {

    xv = accx * mult;
    yv = accy * mult;

    xS = xv * mult;
    yS = yv * mult;

    ballx -= xS;
    bally -= yS;

    // Collision detection
    if (ballx + rad > xmax) {

         ballx = xmax-rad;
    }         
    else if (ballx - rad < 0) {

         ballx = rad;
    }
    if (bally + rad > 2*ymax/3) //Shouldn't take up the whole screen 
    {  

        bally = 2*ymax/3 - rad;
    } 

    else if (bally - rad < 0) {

         bally =  rad;
    }                           

    try {
        Thread.sleep(20);
    }   catch(InterruptedException e) {}

    invalidate();   
}
@Override
public void onMeasure(int widthM, int heightM)
{
    width = View.MeasureSpec.getSize(widthM);
    height = View.MeasureSpec.getSize(heightM);
    xmax = width-1;
    ymax = height-1;
    xmin = 0;
    ymin = 0;
    setMeasuredDimension(width, height);
    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);


}
@Override
public void onDraw(Canvas canvas)
{
    canvas.drawBitmap(bitmap, 0, 0, paint);
    lol.set(ballx - rad, bally-rad, ballx + rad, bally+rad);
    canvas.drawLine(0, 2*height/3, width, 2*height/3, lpaint);
    canvas.drawOval(lol, paint);
    canvas.drawText(xv + " " + yv, 0, height/2, lpaint);
    canvas.save();
    moveBall();
    canvas.restore();

}
}

I've written some code to move a ball around the screen using an orientation sensor. I wanted to get the ball to bounce when it hits the bottom of the screen, sort of like under gravity. Could somebody help out with implementing the physics in my existing code? Flipping the velocity doesn't seem to work. Here's my ball class:

package perseus.gfx.test;

import everything

public class Ball extends View  {   
RectF lol;
Paint paint, lpaint;
Bitmap bitmap;
Canvas canvas;
private float ballx = 150; 
private float bally = 140;
private double speedx = 0;  
private double speedy = 0; //ignore  
private double accx, accy=0;
private float rad = 20;
private float mult = 0.5f;
private double xv, yv, xS, yS;
int width, height;
int xmax, ymax;
int xmin, ymin;

public Ball(Context context) {
    super(context);
    lol = new RectF();
    paint = new Paint();
    paint.setColor(Color.CYAN);
    lpaint = new Paint();
    lpaint.setColor(Color.GRAY);                
}
public void moveBall()  {

    xv = accx * mult;
    yv = accy * mult;

    xS = xv * mult;
    yS = yv * mult;

    ballx -= xS;
    bally -= yS;

    // Collision detection
    if (ballx + rad > xmax) {

         ballx = xmax-rad;
    }         
    else if (ballx - rad < 0) {

         ballx = rad;
    }
    if (bally + rad > 2*ymax/3) //Shouldn't take up the whole screen 
    {  

        bally = 2*ymax/3 - rad;
    } 

    else if (bally - rad < 0) {

         bally =  rad;
    }                           

    try {
        Thread.sleep(20);
    }   catch(InterruptedException e) {}

    invalidate();   
}
@Override
public void onMeasure(int widthM, int heightM)
{
    width = View.MeasureSpec.getSize(widthM);
    height = View.MeasureSpec.getSize(heightM);
    xmax = width-1;
    ymax = height-1;
    xmin = 0;
    ymin = 0;
    setMeasuredDimension(width, height);
    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);


}
@Override
public void onDraw(Canvas canvas)
{
    canvas.drawBitmap(bitmap, 0, 0, paint);
    lol.set(ballx - rad, bally-rad, ballx + rad, bally+rad);
    canvas.drawLine(0, 2*height/3, width, 2*height/3, lpaint);
    canvas.drawOval(lol, paint);
    canvas.drawText(xv + " " + yv, 0, height/2, lpaint);
    canvas.save();
    moveBall();
    canvas.restore();

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美煞众生 2025-01-05 10:09:03

所以关键是在 moveBall() 的每一步中添加一点摩擦力,去除一点点加速度(负值!)。例如

    float friction = -0.001;

    xv = accx * mult + friction;
    yv = accy * mult + friction;

,然后相应地调整可变摩擦力以满足您的需要。对于碰撞,您需要反转速度,例如在底部弹跳:

    bally = -bally;

So the key is to just add a bit of friction, just remove a tiny bit of acceleration (negative!) at each step in moveBall(). E.g.

    float friction = -0.001;

    xv = accx * mult + friction;
    yv = accy * mult + friction;

Then adjust the variable friction accordingly to suit your needs. For the collision you need to invert the velocity, e.g. bounce on bottom:

    bally = -bally;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文