Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (611)
Games in Android Showcase (172)
games submitted by our members
Games in WIP (657)
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  
  Make the stage move when the main character move  (Read 2207 times)
0 Members and 1 Guest are viewing this topic.
Offline P0jahn

Senior Devvie

Projects: 3

« Posted 2012-12-03 11:02:17 »

Imagine playing Super Mario Bros on a PC emulator in windowed mode, and the size of the window was equal to the size of the stage. That would make the window extremely wide and it would totally suck, wouldnt it? That is a problem I am having. The size of the JFrame is equal to the size of the stage.
What I am trying to do is to make the size of the JFrame smaller than the size of the stage and make the stage move when the main character moves, so he always is in the middle of the window, except if "he" is at the right-most or left-most at the map.

The entire stage is a hardware accelerated BufferedImage(meaning it is not modified after its creation). The stage is painted on the JFrame with java.awt.Graphics drawImage(img, x, y, w, h, null) method. Putting negative values on x and y should nudge the image to the left, making it looks like the main character is in the middle. Here is my code, works but very buggy:

   public void draw(Graphics2D g) 
      int cord1 = getXCorrdinate();
      int cord2 = getYCorrdinate();
      g.drawImage(, cord1, cord2, stage.width, stage.height, null);

   private int getXCorrdinate()
      int testcord =  (ourChar.posX - (stage.visibleWidth / 2)) + ourChar.width / 2;
      int testcord2 = (ourChar.posX + (stage.visibleWidth / 2)) + ourChar.width / 2;
      if (testcord < 0)//Make sure its not overlapping at the left-most
         return 0;
      else if (testcord2 > stage.width)//Make sure its not overlapping at the right-most
         return ~(testcord - testcord2 % stage.width);
      else//It is not overlapping.
         return ~testcord;

stage.width and stage.height is the full dimension of the stage, we do not have to worry about these.
Lets look at cord1(and ignore cord2 for now, one problem at the time).
ourChar is the main character, and stage.visibleWidth is the width of the JFrame(and not the stage).

So whats wrong with these codes: the main character is not in the middle.

I hope my text was easy to understand. The code is a different matter thought Smiley
Offline Danny02
« Reply #1 - Posted 2012-12-03 12:03:30 »

you shouldn't create a image as big as your hole game world.

Just draw to your screen as normal(without an bufferedimage), but use a affine transformation(google how to do it with java2d) to move your view.

for example if you want to have your main char always in the middle of the display:
just do this:
affine.transform(-(player.x - halfScreenWidth), 0);

to get the desired effect at the start and end of the "stage" add a min and max

affine.transform(-(min(stageWidth-screenWidth, max(0, player.x - halfScreenWidth))), 0);
Offline P0jahn

Senior Devvie

Projects: 3

« Reply #2 - Posted 2012-12-03 15:19:01 »

Thanks, I will read about affine transformation.
So, using your approach, I will be using this method to render?, java.awt.geom.AffineTransform)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2012-12-03 17:43:57 »

there are probably people here with more experience in java2d then me, but this should be fine
I think there should also be something like Graphics2d.setTransformation(x)

just give it a try, it's so much faster just to google a bit and try everything until it works then waiting for someones answer on a forum Wink
Offline P0jahn

Senior Devvie

Projects: 3

« Reply #4 - Posted 2012-12-03 17:54:12 »

I am googling too. What transform method are you referring at? There is no transform method can make your suggestion work(ie no one takes two ints/floats etc).
Offline Danny02
« Reply #5 - Posted 2012-12-03 18:02:44 »

you have to master your google skills then, young padawan

found after aprox. 0.032ns
public void paint(Graphics g) {
   AffineTransform transformer = new AffineTransform();
   Graphics2D g2d = (Graphics2D)g;
   // draw to g2d.
Offline P0jahn

Senior Devvie

Projects: 3

« Reply #6 - Posted 2012-12-03 18:13:59 »

Oh I get how it works now. And I also got it to work Smiley
Final question, "(-(min(stageWidth-screenWidth, max(0, player.x - halfScreenWidth))), 0);"
By "min" and "max", of what? Sorry for lacking common sense.
I would google this, but I am pretty sure I cant find an interpretation of your snippset on google :p

Edit: errrr I am pretty sure they are from java.lang.Math?
Well then, again, thanks Smiley

Edit2: Got it 100% working Cheesy Btw, nice calculations. I would never be able to come up with such math solutions.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Roquen (12 views)
2015-08-29 11:30:54

GamerC4 (23 views)
2015-08-22 20:38:50

GamerC4 (22 views)
2015-08-22 20:37:18

GamerC4 (26 views)
2015-08-22 20:37:01

Kefwar (30 views)
2015-08-22 18:07:24

GamerC4 (24 views)
2015-08-22 01:00:24

GamerC4 (37 views)
2015-08-22 01:00:17

GamerC4 (24 views)
2015-08-22 00:57:35

GamerC4 (25 views)
2015-08-22 00:56:59

BurntPizza (29 views)
2015-08-21 01:38:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 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‑
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!