Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (522) Games in Android Showcase (127) games submitted by our members Games in WIP (590) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Keep both players in shot  (Read 1003 times) 0 Members and 1 Guest are viewing this topic.
wookoouk

Senior Newbie

 « Posted 2012-01-12 21:17:09 »

If I want to keep both player in the screen at all times no matter how far apart, how can I do this with GLOrtho?

I am trying to calculate the distance between them +the player width but im not sure how to tell the camera to display that width and center (distance between players /2)

theagentd

« JGO Bitwise Duke »

Medals: 361
Projects: 2
Exp: 8 years

 « Reply #1 - Posted 2012-01-12 21:45:35 »

 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  26  27 `double minX = Math.min(player1.x, player2.x) - 10;double minY = Math.min(player1.y, player2.y) - 10;double maxX = Math.max(player1.x, player2.x) + 10;double maxY = Math.max(player1.y, player2.y) + 10;double dx = maxX-minX;double dy = maxY-minY;double aspectRatio = dx / dy;double WANTED_ASPECT_RATIO = (double)screenWidth/screenHeight;if(aspectRatio < WANTED_ASPECT_RATIO){    double centerY = minY + dy*0.5;    double correction = dx*0.5 * WANTED_ASPECT_RATIO;    minY = centerY - correction;    maxY = centerY + correction;}else{    double centerX = minX + dx*0.5;    double correction = dy*0.5 / WANTED_ASPECT_RATIO;    minX = centerX - correction;    maxX = centerX + correction;}glOrtho(minX, maxX, maxY, minY, -1, 1);`

COMPLETELY UNTESTED. IF IT WORKS, I'LL BE SURPRISED.

Myomyomyo.
wookoouk

Senior Newbie

 « Reply #2 - Posted 2012-01-12 21:55:59 »

VERY quick and detailed reply! but this zooms the camera in about 1000% and puts the camera in the top right LEFT. I will fiddle with it and get back to you

Thanks
theagentd

« JGO Bitwise Duke »

Medals: 361
Projects: 2
Exp: 8 years

 « Reply #3 - Posted 2012-01-12 22:36:33 »

Geh! I knew it! =S

To describe what I was trying to do:

1. calculate a bounding box around the units (first 4 lines)
2. fix the aspect ratio
3. glOtho...

Myomyomyo.
wookoouk

Senior Newbie

 « Reply #4 - Posted 2012-01-12 22:44:13 »

Ahhh! you little genius!

How would you focus on one player, aka, zoom in 300% and offset the cam over the player. Dont have to code if you dont want too, Im just trying to get my head round it so that I can learn and not just follow blindly

.....as I am typing I can see that my glOrtho is in my init method.....so not being updated by player movement.....shit.
theagentd

« JGO Bitwise Duke »

Medals: 361
Projects: 2
Exp: 8 years

 « Reply #5 - Posted 2012-01-12 22:49:56 »

Hmm. It should be easier to just setup glOrtho with the screen resolution and then translate and scale to the right place.

Just glTranslate to the players coordinates. You might want to center the screen on him too by subtracting half the screen width/height from the translation coordinates. It should be easier to center on the point in between the players (the average of the two players' positions), and then scale based on how far away they are from each other...

Myomyomyo.
wookoouk

Senior Newbie

 « Reply #6 - Posted 2012-01-12 22:55:21 »

Damm, didnt know I could use scale and translate, I should be able to use the same as I have used in a slick2D game:

g.scale(1.5f, 1.5f);
g.translate(-x + windowWidth / 2 - 120, -y + windowHeight / 2 - 120);

...Im a numpty, thanks for your help, maby all this will make as much scene as it does to you.

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.
 trollwarrior1 (28 views) 2014-11-22 12:13:56 xFryIx (70 views) 2014-11-13 12:34:49 digdugdiggy (49 views) 2014-11-12 21:11:50 digdugdiggy (43 views) 2014-11-12 21:10:15 digdugdiggy (37 views) 2014-11-12 21:09:33 kovacsa (61 views) 2014-11-07 19:57:14 TehJavaDev (65 views) 2014-11-03 22:04:50 BurntPizza (63 views) 2014-11-03 18:54:52 moogie (79 views) 2014-11-03 06:22:04 CopyableCougar4 (78 views) 2014-11-01 23:36:41
 theagentd 29x basil_ 27x HeroesGraveDev 23x BurntPizza 22x Riven 19x Spasi 18x princec 17x KevinWorkman 13x kpars 11x SHC 11x gouessej 10x Grunnt 10x Nate 9x Longarmx 9x LiquidNitrogen 8x CopyableCougar4 8x
 Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-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