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 (408)
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  
  Camera zooming on specific point  (Read 498 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 23
Projects: 3


One for all!


« Posted 2013-02-12 14:17:11 »

I'm using LibGDX, and I'd very much like to zoom in on something specific in my game.
Using the OrthographicCamera, changing the zoom field works wonders, but it zooms in on the center of the screen.

I'd like to move the camera while zooming, so that the point I'm zooming in on, remains on the same window-coordinate.

This would require some magic, figuring out how much I need to move the camera and in what direction, based on a point.

Offline Mads

JGO Ninja


Medals: 23
Projects: 3


One for all!


« Reply #1 - Posted 2013-02-12 15:09:14 »

This is my attempt at it. Needless to say, it doesn't work as expected.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void zoomMaintainPoint(int x, int y, float zoom, OrthographicCamera camera) {
      Vector3 pointToMaintain = new Vector3(x, y, 0);
      camera.project(pointToMaintain);
      camera.zoom = zoom;
      Vector3 pointNewPos = new Vector3(pointToMaintain);
      camera.unproject(pointNewPos);
      int deltaX = (int) (pointNewPos.x - pointToMaintain.x);
      int deltaY = (int) (pointNewPos.y - pointToMaintain.y);
      camera.translate(deltaX, deltaY);
   }

Offline cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #2 - Posted 2013-02-12 16:03:28 »

I'm as clueless as you, and I also need this :/

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Games published by our own members! Check 'em out!
Try the Free Demo of Droid Assault
Offline Roquen

JGO Ninja


Medals: 66



« Reply #3 - Posted 2013-02-12 16:48:09 »

Orthographic projections don't have a real zoom...you can only scale & translate.
Offline Mads

JGO Ninja


Medals: 23
Projects: 3


One for all!


« Reply #4 - Posted 2013-02-12 18:35:52 »

Orthographic projections don't have a real zoom...you can only scale & translate.

I'm quite aware. I think you can see through that flaw in my statement, to help me though. Smiley
I think you know what I meant with the original question.

Online Mickelukas

JGO Ninja


Medals: 39
Projects: 2


Java guru wanabee


« Reply #5 - Posted 2013-02-12 20:35:42 »

Quote
final Point2DFloat before = getMouseCoordInGame();
updateViewport();
final Point2DFloat after = getMouseCoordInGame();
Camera.setCameraPos2D(Camera.getLookingAt().getX() + before.getX() - after.getX(), Camera.getLookingAt().getY() + before.getY() - after.getY());

This is the code in the State of Profit map that handles that. In general, check where the mouse was (in the game, not in the applet), do the zoom, check where it is after and move the camera the difference. Smiley

Mike

Offline Mads

JGO Ninja


Medals: 23
Projects: 3


One for all!


« Reply #6 - Posted 2013-02-12 21:31:28 »

That is what I was doing, and I'm confident I can make it work now.
I think my only problem was that I did not update the camera after zooming it. Obviously, project/unproject isn't going to work, if it's not updated.

Thanks again.

Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

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

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

UnluckyDevil (208 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.111 seconds with 21 queries.