Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Programming basic Fish Tank  (Read 2172 times)
0 Members and 1 Guest are viewing this topic.
Offline Swattkidd7

Junior Member





« Posted 2010-07-18 01:22:50 »

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.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-07-18 01:44:12 »

Don't rotate, scale:

scale(-1,1)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #2 - Posted 2010-07-19 19:10:25 »

Or if you're using Graphics.drawImage...

do this:

1  
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.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Swattkidd7

Junior Member





« Reply #3 - Posted 2010-07-19 22:07:38 »

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 Tongue.

Thanks again.
Offline Swattkidd7

Junior Member





« Reply #4 - Posted 2010-08-02 08:37:00 »

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).

1  
2  
3  
4  
5  
6  
7  
8  
9  
        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);
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2010-08-02 12:51:35 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Swattkidd7

Junior Member





« Reply #6 - Posted 2010-08-03 05:04:19 »

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.
Offline zoto

Senior Member


Medals: 4



« Reply #7 - Posted 2010-08-03 07:21:00 »

Hope this helps.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   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);
      }
   }
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (20 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!