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 (293)
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  
  Keep both players in shot  (Read 714 times)
0 Members and 1 Guest are viewing this topic.
Offline wookoouk

Senior Newbie





« Posted 2012-01-12 22: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)

Online theagentd
« Reply #1 - Posted 2012-01-12 22: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.
Offline wookoouk

Senior Newbie





« Reply #2 - Posted 2012-01-12 22: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 Cheesy

Thanks
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online theagentd
« Reply #3 - Posted 2012-01-12 23: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.
Offline wookoouk

Senior Newbie





« Reply #4 - Posted 2012-01-12 23: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.
Online theagentd
« Reply #5 - Posted 2012-01-12 23: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.
Offline wookoouk

Senior Newbie





« Reply #6 - Posted 2012-01-12 23: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.

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

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 (84 views)
2013-05-17 21:29:12

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

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

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

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

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

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

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

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

UnluckyDevil (187 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.119 seconds with 20 queries.