0

i am trying to implement Deceleration on the bouncy ball. i am able to move the ball up and down but i wanted to add Declaration so when i drop the ball from height it jumps then strike the ground bounce once again and slowly slowly its speed decreases and stop at the end.

Here is My code

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;

public class AnimatedView extends ImageView{
private Context mContext;
int x = -1;
int y = -1;
private int xVelocity = 0;
private int yVelocity = 25;
private Handler h;
private final int FRAME_RATE = 20;


public AnimatedView(Context context, AttributeSet attrs)  {  
    super(context, attrs);  
    mContext = context;  
    h = new Handler();
} 

private Runnable r = new Runnable() {
    @Override
    public void run() {
        invalidate(); 
    }
};

protected void onDraw(Canvas c) {  

    BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);  
    if (x<0 && y <0) {
        x = this.getWidth()/2;
        y = this.getHeight()/2;
    } else {
        x += xVelocity;
        y += yVelocity;
        if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
            xVelocity = xVelocity*-1;
        }
        if ((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0)) {
            yVelocity = yVelocity*-1;
        }
    }
    c.drawBitmap(ball.getBitmap(), x, y, null);  

    h.postDelayed(r, FRAME_RATE);


} 

Hope Anyone Can Help me.

Thanks in Advance

UchihaSasuke
  • 363
  • 2
  • 23

1 Answers1

1

Acceleration is to velocity as velocity is to position. You just need to do what you're doing with your position to your velocity.

The simplest solution is to add

yVelocity += yAccel;

to the top of onDraw where yAccel is a int that you want added to the yVelocity every tick. Based on your velocity values

private int yAccel = -2;

might be appropriate.

edit: You should also add checks to ensure the ball does not go into the ground. I added these in the blocks that reverse the velocity, the following code works for me. If it don't work for you, there might be something wrong with another part of your project.

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;

public class AnimatedView extends ImageView {
    private Context mContext;
    int x = -1;
    int y = -1;
    private int xVelocity = 0;
    private int yVelocity = 25;
    private int yAccel = 2;
    private Handler h;
    private final int FRAME_RATE = 20;

    public AnimatedView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        h = new Handler();
    }

    private Runnable r = new Runnable() {
        @Override
        public void run() {
            invalidate();
        }
    };



    protected void onDraw(Canvas c) {
        yVelocity += yAccel;

        BitmapDrawable ball = (BitmapDrawable) mContext.getResources()
                .getDrawable(R.drawable.cakes);
        if (x < 0 && y < 0) {
            x = this.getWidth() / 2;
            y = this.getHeight() / 2;
        } else {
            x += xVelocity;
            y += yVelocity;
            if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
                xVelocity = xVelocity * -1;
                x = Math.min(this.getWidth() - ball.getBitmap().getWidth(), x);
                x = Math.max(0, x);
            }
            if ((y > this.getHeight() - ball.getBitmap().getHeight())
                    || (y < 0)) {
                yVelocity = (int)(yVelocity * -0.8f);

                y = Math.min(this.getHeight() - ball.getBitmap().getHeight(), y);
                y = Math.max(0, y);
            }
        }
        c.drawBitmap(ball.getBitmap(), x, y, null);

        h.postDelayed(r, FRAME_RATE);

    }
}
  • I tried your solution but i am not able to implement the decrease speed it start increasing speed without touching ground and lastly it hanged. – UchihaSasuke Dec 10 '13 at 06:47
  • I added the full code for the class to the answer. It worked fine when I tested it. – Benjamin Sergent Dec 10 '13 at 07:23
  • now it hitting the ground with some height but it's speed is not decreasing(Deceleration) with each bounce. – UchihaSasuke Dec 10 '13 at 07:34
  • Ah. I missed that requirement, sorry. You can bleed off speed during the bounce by changing `yVelocity = yVelocity * -1;` to `yVelocity = (int)(yVelocity * -0.8f);` I'll update the post accordingly. – Benjamin Sergent Dec 10 '13 at 08:36
  • Wow!!great it work for me.Now i am trying to bounce the ball with my finger and ball move in any direction can you guide me how can i proceed. – UchihaSasuke Dec 10 '13 at 12:07