Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (290)
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 712 times)
0 Members and 1 Guest are viewing this topic.
Offline P0jahn

Junior Member


Projects: 1



« Posted 2012-12-03 12: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

JGO Knight


Medals: 36



« Reply #1 - Posted 2012-12-03 13: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

Junior Member


Projects: 1



« Reply #2 - Posted 2012-12-03 16: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

JGO Knight


Medals: 36



« Reply #3 - Posted 2012-12-03 18: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

Junior Member


Projects: 1



« Reply #4 - Posted 2012-12-03 18: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

JGO Knight


Medals: 36



« Reply #5 - Posted 2012-12-03 19: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

Junior Member


Projects: 1



« Reply #6 - Posted 2012-12-03 19: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  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (60 views)
2013-05-17 21:29:12

alaslipknot (69 views)
2013-05-16 21:24:48

gouessej (99 views)
2013-05-16 00:53:38

gouessej (98 views)
2013-05-16 00:17:58

theagentd (107 views)
2013-05-15 15:01:13

theagentd (98 views)
2013-05-15 15:00:54

StreetDoggy (144 views)
2013-05-14 15:56:26

kutucuk (167 views)
2013-05-12 17:10:36

kutucuk (166 views)
2013-05-12 15:36:09

UnluckyDevil (175 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.085 seconds with 21 queries.