Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  Camera Calculation  (Read 622 times)
0 Members and 1 Guest are viewing this topic.
Offline tdegroot96

Junior Member


Projects: 1



« Posted 2013-07-10 10:41:08 »

Hey guys!

At the moment one of my calculations got me stuck.
For the scrolling part I use:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public float getX() {
      float x = player.getX() - size.getX() / 2;
      float maxWidth = (bounds.getWidth() + (size.getX() / 2)) / 2;

      if (x < bounds.getX())
         x = bounds.getX();
      if (x > maxWidth)
         x = maxWidth;

      return x;
}

public float getY() {
      float y = player.getY() - size.getY() / 2;
      float maxHeight = (bounds.getHeight() + (size.getY() / 2)) / 2;

      if (y < bounds.getY())
         y = bounds.getY();
      if (y > maxHeight)
         y = maxHeight;

      return y;
}


getX() and getY() are both offsets I use for everything to scroll.
Now I'm struggling with the camera limit. For getX() the maxWidth calculation works perfectly fine. So I thought that I could use the getX() calculation for the getY() calculation as well. But no, it doesn't work. It stops too early with scrolling. The max Camera Y I'm having right now is 1792. The max Camera X is 1920.
I've tried alot of things but can't get this to work.. Could anyone help me with this one?

Thanks in advance!

Github link:
https://github.com/Desmaster/Nemesis/blob/master/src/nl/tdegroot/games/nemesis/Camera.java
Offline tdegroot96

Junior Member


Projects: 1



« Reply #1 - Posted 2013-07-10 18:17:16 »

Anyone?
Offline Redocdam

Senior Member


Medals: 17



« Reply #2 - Posted 2013-07-10 20:11:03 »

Alright, after doing some guess work on what you're looking to accomplish. It seems like you're trying to stop the camera when it reaches the border by taking half the screen width or height from your defined border.

Going with this assumption, if you're passing 1920 as your screen width, then your screen height should be 1080.
So I think your camera Y should be 1080 instead of 1792, unless you have some sort of weird resolution.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tdegroot96

Junior Member


Projects: 1



« Reply #3 - Posted 2013-07-10 20:25:50 »

The map is 3200 * 3200 pixels. The camera Y should actually be higher than 1792..
Plus, I don't know why it doesn't work for getY while it does for getX
Offline Redocdam

Senior Member


Medals: 17



« Reply #4 - Posted 2013-07-11 00:00:31 »

I see, you're saying the result is 1792 and not a value you're using to determine camera position.

Alright, after doing a bit of back solving, it looks like your viewport (window size) is 1280x768.
So now we can start plugging some values into your calculations.

Scanning through your logic, it appears the max values are causing the problems. So let's run the values through and see what happens.

Max Width
(3200 + (1280/2)) /2
(3200 + 640) /2
3840/2 = 1920

Max Height
(3200 + (768/2)) /2
(3200 + 384) /2
(3584)/2 = 1792

From the above, it appears the way you're calculating the max limits is the error.
The reason it's working with the width is because 3200 - 1920 = 1280. This means you're using the edge of the screen as the camera position instead of the center.
So in sticking with that coordinate system, you should just be subtracting the viewport x and y from the map size to find your limits.
 
To make sure I didn't derive something incorrectly from your code, I suggest plugging in 2432 for the maxHeight value as a test.
Offline tdegroot96

Junior Member


Projects: 1



« Reply #5 - Posted 2013-07-11 01:25:14 »

Thank you very much for pointing this out! Smiley
I got ideas for a new camera class, gonna work a bit more with offset.

Thanks! Cheesy
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.

xsi3rr4x (63 views)
2014-04-15 18:08:23

BurntPizza (61 views)
2014-04-15 03:46:01

UprightPath (74 views)
2014-04-14 17:39:50

UprightPath (57 views)
2014-04-14 17:35:47

Porlus (75 views)
2014-04-14 15:48:38

tom_mai78101 (100 views)
2014-04-10 04:04:31

BurntPizza (160 views)
2014-04-08 23:06:04

tom_mai78101 (255 views)
2014-04-05 13:34:39

trollwarrior1 (208 views)
2014-04-04 12:06:45

CJLetsGame (215 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!