Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 1640 times)
0 Members and 1 Guest are viewing this topic.
Offline P0jahn

Senior Duke


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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   public void draw(Graphics2D g) 
   {
      int cord1 = getXCorrdinate();
      int cord2 = getYCorrdinate();
     
      g.drawImage(stage.map, 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 Duke


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?
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#drawRenderedImage(java.awt.image.RenderedImage, 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 Duke


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
1  
2  
3  
4  
5  
6  
7  
public void paint(Graphics g) {
   AffineTransform transformer = new AffineTransform();
   transformer.translate(5,5);
   Graphics2D g2d = (Graphics2D)g;
   g2d.setTransform(transformer);
   // draw to g2d.
}
Offline P0jahn

Senior Duke


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.

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (53 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!