Jump to content
  • Advertisement
Sign in to follow this  
CoryG89

[java] Viewport Vs. World Coordinate Systems

This topic is 2714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Right now my side scroller game demo basically moves my map and all objects around my player, ie. the viewport and the player don't actually move, everything else moves around it.

I would like to change this if I can so that the player and the viewport move around together and the map and all other objects stay stationary. I know that when I draw using my Graphics2D object, I am currently using coordinates starting at the origin (0, 0) located in the top left hand corner of my JPanel extended canvas.

So my question is, how would I be able to map a viewport to move around, and have access to world cooridinates, the 2D playable world being updated and not rendered much bigger than the viewport size. Also having access to a set of viewport cooridnates would be nice.

Does anyone have any tips on how I can implement something along these lines using Graphics2D.

Currently I am using the wrap around background technique, but I would like to change this to a stationary map with moving player and viewport in sync.

Share this post


Link to post
Share on other sites
Advertisement
Right, you say Graphics2D, so I assume 2D here. Well this isn't a complex issue at all, depending on how you are implementing your map (array tilemap maybe?). You simply need to deal with the idea of a moving origin for your viewport, and project the map's space into viewport coords before drawing the object. Is there going to be zoom? If so then we need to deal with scaling, or possibly ignore it. First though, tell me more about your map and I can show you can sample code.

Share this post


Link to post
Share on other sites
I would advise you check into AffineTransform. You can create a camera class which gets the player's position, offsets to center the view, and follows the character.

The main method you want to use is


//foo being your AffineTransform variable
foo.translate(double x, double y);


Make sure you set your graphics transform to your AffineTransform FIRST thing.


g2d.setTransform(foo);
Render(g2d);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!