Programming basic Fish Tank

Hey everyone, i am trying to program a basic fish tank and am having trouble with the swimming AI.

It is for a kids game so it doesnt have to be anything too accurate or realistic but what I am trying to do is get the fish to swim toward where its head is facing. I have that working so far with AffineTransform rotations and translations, however when the fish is swimming to the right (0 degrees) and hits the wall, I add 180 degrees to its angle and that causes it to swim back in the other direction.

However the problem with that is the fish image is then upside and I am not sure how to fix that. I am guessing I can just check if the angle is within a certain range (90 - 270?) the image will be flipped vertically?

Any help, advice or suggestions is appreciated.

Don’t rotate, scale:

scale(-1,1)

Or if you’re using Graphics.drawImage

do this:


g.drawImage(image, x, y, x+width, y+height, x+width, y, x, y+height, null);

Basically you are drawing a total of 4 coordinates, where the first is the top left of where you want to draw the image, the second is the bottom right, and then the next two are any transformations you would want to apply, in a way. By swapping the X coordinates in the second one, that ends up flipping horizontally.

Hey guys thanks a lot! Both of your methods were very useful and the game is progressing again ahah, although I am sure I will probably be back with more questions :P.

Thanks again.

For the scale(-1,1) i am noticing that the image flips but not from the center, which makes everything off by a bit. Is there any way to flip the image using the center of the image as its axis?

This is how I am currently drawing the player, and the problem arises when the image is flipped, it literally flips out of the hitbox rectangle like a door on a hinge, rather than flipping within its own bounds (around its center).


        AffineTransform at = new AffineTransform();
        at.translate(x, y);
        if(facingLeft)
            at.scale(-3,3);
        else if(facingRight)
            at.scale(3,3);
        currAnim.getCurrentFrame().draw((Graphics2D)g, at);
        g.setColor(Color.white);
        g.drawRect(rect.x, rect.y, rect.width, rect.height);

Translate it to the center of the image, scale, translate it back.

Hmm how would i translate it to the center of the image? Right now, when I translate it to x,y thats where the image will be drawn, so drawing it x+width/2 and y+height/2 would just move where the image is drawn wouldnt it?

I have solved this temporarily by translating the image back into where it should be. Basically if the image is facingLeft I push the image to the right the length of its width, and then flip it.

I am thinking an easier solution would be to just have an Animation for left and right, but there would be some drawbacks to that right? with the amount of art to create, and also loading in animations for left and right for weapons and all of that.

Hope this helps.


	private void draw(BufferedImage image, Graphics2D g, int x, int y, boolean flip){
		if( flip ){
			int xOffset = x + image.getWidth();
			g.translate(xOffset, 0);
			g.scale(-1, 1);
			g.drawImage(image, 0, y, null);
			g.scale(-1, 1);
			g.translate(-xOffset, 0);
		} else {
			g.drawImage(image, x, y, null);
		}
	}