Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 camera follow multiple characters  (Read 342 times)
0 Members and 1 Guest are viewing this topic.
Offline P0jahn

Senior Devvie


Projects: 3



« Posted 2014-03-09 18:16:41 »

Currently, I use the following code to make the screen follow the main character:

1  
2  
3  
4  
5  
6  
7  
8  
9  
            //focus is the entity the camera should focus on.
            float focusX = focus.currX + focus.width  / 2,
                  focusY = focus.currY + focus.height / 2;
           
            //visibleWidth and visibleHeight is the size of the container/window.
            //width and height is the size of the entire map
            translateX = Math.min(stage.width  - stage.visibleWidth,   Math.max(0, focusX - stage.visibleWidth  / 2));
            translateY = Math.min(stage.height - stage.visibleHeight,  Math.max(0, focusY - stage.visibleHeight / 2));
            //scaleX and scaleY are untouched


This code works fine as long as there is one entity to focus on.

I want a code that supports 2-4 objects. I have seen games that have it. Basically, when the entities are to far from each other, the scaleX and scaleY are decreased to zoom out.
But I am not sure how to code this.

Some help?
Offline Seiya02

JGO Coder


Medals: 3
Projects: 4
Exp: 5 years


No Text Here :x


« Reply #1 - Posted 2014-03-09 18:22:46 »

I would take the distances between all Objects and see which of them is the biggest, then scale the whole scene for that distance (or a bit higher so that both characters would still fit on the screen without getting cut in half) and position it so that the character with the lowest x value is at the left side (for example allways 50 px away from the end of the visible display) and the character with the heighest x value is at the right side of the screen (again with 50 px distance to the end of the display) and do the same with the y coordinates (but maybe try to keep the relations of the Screen scaling the same cause it'll look weird otherwise)
Offline Seiya02

JGO Coder


Medals: 3
Projects: 4
Exp: 5 years


No Text Here :x


« Reply #2 - Posted 2014-03-09 18:46:46 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
         // Camera Setup
         // X
         float minx = e1.x, maxx = e1.x;
         if (e2.x < minx) {
            minx = e2.x;
         }
         if (e3.x < minx) {
            minx = e3.x;
         }
         if (e4.x < minx) {
            minx = e4.x;
         }
         if (e2.x > maxx) {
            maxx = e2.x;
         }
         if (e3.x > maxx) {
            maxx = e3.x;
         }
         if (e4.x > maxx) {
            maxx = e4.x;
         }
         float difx = (maxx - minx) + 100;

         GL11.glScalef(800 / difx, 800 / difx, 1);
         GL11.glTranslatef(-minx + 50, 0, 0);


Does the Job for 4 Entities (Screen 800x600) but this is only based on x coordinates, so none of the entities can go out of the screen left or right, they still can go out top or bot. Called before rendering the entities
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline P0jahn

Senior Devvie


Projects: 3



« Reply #3 - Posted 2014-08-21 20:50:36 »

Time to implement this.

Seiya02: Do you know how to use your code with OrthogonalCamera(LibGDX)?
Also, to add support for an Y axis, just copy the code and change the x variables with y?
Offline Seiya02

JGO Coder


Medals: 3
Projects: 4
Exp: 5 years


No Text Here :x


« Reply #4 - Posted 2014-08-22 07:17:44 »

I'm not working with LibGDX sorry :s
and to add y support you should check if x or y is needed for the scale (if you scale with x and they have a huge difference on y they won't be visible so you have to scale with y)

and the scaling with y is as you said: just change x to y Smiley
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (35 views)
2014-12-15 09:26:44

Mr.CodeIt (26 views)
2014-12-14 19:50:38

BurntPizza (56 views)
2014-12-09 22:41:13

BurntPizza (91 views)
2014-12-08 04:46:31

JscottyBieshaar (51 views)
2014-12-05 12:39:02

SHC (67 views)
2014-12-03 16:27:13

CopyableCougar4 (69 views)
2014-11-29 21:32:03

toopeicgaming1999 (129 views)
2014-11-26 15:22:04

toopeicgaming1999 (120 views)
2014-11-26 15:20:36

toopeicgaming1999 (34 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!