Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (714) Games in Android Showcase (214) games submitted by our members Games in WIP (787) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Keeping two objects on the screen?  (Read 1476 times) 0 Members and 1 Guest are viewing this topic.
CyanPrime
 « Posted 2009-05-16 01:19:24 »

I'm trying to make it so that no matter how far apart two objects are they'll both remain on screen.
This is what I got so far:

float distance = (float) ((Math.sqrt((p1.x - p2.x) + (p1.y - p2.y))));
float camx = (float)((p1.x + p2.x) * 0.5);
float camy = (float)((p1.y + p2.y) * 0.5);
float camz = (float) (distance * 5);

What is the math I need so the Z zooms in and out correctly?
lhkbob

JGO Knight

Medals: 32

 « Reply #1 - Posted 2009-05-16 04:05:16 »

One that should help is that distance should be:
 1 `float distance = (float) Math.sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) );`

To calculate the distance needed, try this:
 1  2  3  4  5 `float width = Math.abs(p1.x - p2.x) + PADDING;float camx = (float) ( (p1.x + p2.x) * .5);float camy = (float) ( (p1.y + p2.y) * .5);float camz = (float) (.5 * width * Math.tan(.5 * fov));`

The only things I've added are PADDING (which would be a constant that just affects how much space between the edges of the screen you want), and the variable fov.  fov is the field of view angle that you're using when rendering.  If you're using JOGL, it's one of the values used in gluPerspective(), probably 60 degrees.

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Rule (59 views) 2017-03-19 12:43:22 Rule (51 views) 2017-03-19 12:42:17 Rule (62 views) 2017-03-19 12:36:21 theagentd (67 views) 2017-03-16 05:07:07 theagentd (72 views) 2017-03-15 22:37:06 theagentd (68 views) 2017-03-15 22:32:18 theagentd (66 views) 2017-03-15 22:31:11 ral0r2 (127 views) 2017-03-03 11:52:41 ral0r2 (125 views) 2017-03-03 11:42:24 Archive (312 views) 2017-02-27 19:41:49
 basil_ 19x philfrei 13x Apo 11x dime26 10x Archive 9x FrozenShade 9x bornander 7x KaiHH 7x FabulousFellini 7x SteveSmith 7x princec 7x Riven 6x CJC 6x Spasi 6x orangepascal 5x Ecumene 5x
 List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05SF/X Librariesby SkyAphid2017-03-02 06:38:56SF/X Librariesby SkyAphid2017-03-02 06:38:32SF/X Librariesby SkyAphid2017-03-02 06:38:05SF/X Librariesby SkyAphid2017-03-02 06:37:51
 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