1

Most learned friends

I have a sprite that moves around on screen but at the moment it just moves diagonally from left to right and goes off screen and then comes back on the other side.

What I would like it to do is bounce off the edges of the screen in a random fashion but, not being all that clued up on maths, I'm struggling to figure out the coordinates to do this.

Below is what I have so far: (this is an updated code for the Sprite class:

   public class Sprite {

    //x,y position of sprite - initial position (0,50)
   // int [] DIRECTION_TO_ANIMATION_MAP = {3, 1, 0, 2};
private static final int BMP_ROWS = 3;
private static final int BMP_COLUMNS = 4;
private int x = 0;
private int y = 0;
private int xSpeed = 5;//Horizontal increment of position (speed)
private int ySpeed;// Vertical increment of position (speed)
private int currentFrame = 0;
private GameView gameView;
private Bitmap spritebmp;
//Width and Height of the Sprite image
private int bmp_width;
private int bmp_height;
// Needed for new random coordinates.
//private Random random = new Random();


public Sprite(GameView gameView) {
    this.gameView = gameView;
    spritebmp = BitmapFactory.decodeResource(gameView.getResources(),
            R.drawable.running_ninja_sprite);
    this.bmp_width = spritebmp.getWidth() / BMP_COLUMNS;
    this.bmp_height = spritebmp.getHeight() / BMP_ROWS;
    /*Random rnd = new Random(System.currentTimeMillis());
    xSpeed = rnd.nextInt(45)-5;
    ySpeed = rnd.nextInt(25)-5;*/
}

//update the position of the sprite
public void update() {

    //if (x < 0 || x > gameView.getWidth() ){ xSpeed = xSpeed * -1;}
    //if (y < 0 || y > gameView.getHeight() ){ ySpeed = ySpeed * -1;}
    if (x > gameView.getWidth() - bmp_width - xSpeed || x + xSpeed < 0) {
        xSpeed = -xSpeed;
    }
    x = x + xSpeed;
    if (y > gameView.getHeight() - bmp_height - ySpeed || y + ySpeed < 0) {
        ySpeed = -ySpeed;
    }
    y = y + xSpeed;
    currentFrame = ++currentFrame % BMP_COLUMNS;
    //y = random.nextInt(gameView.getWidth());
    //wrapAround(); //Adjust motion of sprite.
}

public void draw(Canvas canvas) {
    update();
    int srcX = currentFrame * bmp_width;
    int srcY = 1 * bmp_height; //getAnimationRow()
    Rect src = new Rect(srcX, srcY, srcX + bmp_width, srcY + bmp_height);
    Rect dst = new Rect(x, y, x + bmp_width, y + bmp_height);
    //Draw sprite image
    canvas.drawBitmap(spritebmp, x, y, null);


}

/*private int getAnimationRow() {
    double dirDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
    int direction = (int) Math.round(dirDouble) % BMP_ROWS;
    return DIRECTION_TO_ANIMATION_MAP[direction];
}*/

public void wrapAround() {
    //Code to wrap around
    if (x < 0) x = x + gameView.getWidth(); //increment x whilst not off screen
    if (x >= gameView.getWidth()) { //if gone of the right sides of screen
        x = x - gameView.getWidth(); //Reset x
    }
    if (y < 0) y = y + gameView.getHeight();//increment y whilst not off screen
    if (y >= gameView.getHeight()) {//if gone of the bottom of screen
        y -= gameView.getHeight();//Reset y
    }
}

    // Checks if the sprite was touched

public boolean wasItTouched(float ex, float ey) {
    boolean touched = false;
    if ((x <= ex) && (ex < x + bmp_width) &&
            (y <= ey) && (ey < y + bmp_height)) {
        touched = true;
    }
    return touched;
    }
    }

It now does bounce off the edge but from top left, diagonally to the right and back up in the same direction and continues to do this back and forth. I'd like it to be random in its direction after hitting the edge. Any suggestions? As you can see from the code I have tried a lot of things but the sprite just keeps on doing this continuous back and forth diagonal motion and I'm at a loss as to what I can do.

Thanks

Phil Adams
  • 95
  • 2
  • 16

1 Answers1

1

Just negate your speed every time you hit a wall

if (x < 0 || x > gameView.getWidth() ){ xSpeed = xSpeed * -1;}
if (y < 0 || y > gameView.getHeight() ){ ySpeed = ySpeed * -1;}

/////////////////////////////// Edit

It will fit something like this. You can also remove your wrap function as it is no longer applicable

//update the position of the sprite
public void update() {

    x = x + xSpeed;
    y = y + xSpeed;
    bounce();

}

private void bounce(){
    if (x <= 0 || x >= gameView.getWidth() ){ xSpeed = xSpeed * -1;}
    if (y <= 0 || y >= gameView.getHeight() ){ ySpeed = ySpeed * -1;}

}
Canoe
  • 589
  • 5
  • 8
  • I've updated my question with what I've tried. Maybe you could suggest the next steps to take? – Phil Adams Mar 30 '17 at 19:35
  • @PhilAdams edited. This gets you 90% of the way there. There's still a bit of work to be done to ensure it bounces perfectly at the boundary, but i will leave that up to you to figure out. – Canoe Mar 30 '17 at 20:28
  • Errr thanks but that's why I came here because I can't figure it out. Like I said, my maths isn't my strong point so stuff like this bamboozles me a little so I don't think I will be able to figure it out. But thanks anyway – Phil Adams Mar 30 '17 at 20:31
  • Nothing wrong with coming here to understand. We are not here to do the work for you though. – Canoe Mar 30 '17 at 20:34
  • I've updated my question with a revision of the Sprite class. A lot more code but the same result. It just goes diagonally from top left to bottom right and back up in the same direction, etc – Phil Adams Mar 31 '17 at 21:20