Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

Implementing better code design for a camera feature in a 2D game

This topic is 2150 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

Here is my game prototype.

 

 

The code of the camera feature in this 2D game is achieved by having offsetting the object's positions depending on what arrow key was pressed from the user.

 

These objects(2 objects right now in the game ie: block and background image) are able to move by adding a KeyListener interface and overriding their keyPressed method with my own logic.

 

The problem is I need to do this for every object that appears on screen except for the character Spiderman in the future as well.

 

Any ideas on a better or an ideal code design to achieve the same effect in my 2D game? The game is coded in Java.

 

On a side note: I have a idea for a code design for the camera but the camera needs to know which object are on screen and offset their position by a certain amount when the user presses a key. Seems like I am overcomplicated the feature by coming up with this idea.

 

P.S. Is a camera feature even suppose to have access to the objects appearing on the screen of the game? Is that the camera job even suppose to have access to a list of game objects in the game?

Edited by warnexus

Share this post


Link to post
Share on other sites
Advertisement

The camera should have its own coordinates. These coordinates will determine where the screen is currently positioned in the game area. When you render an object on screen, you do so in relation to the camera's coordinates. Just subtract the camera's coordinates from the object's coordinates to determine where it should be drawn on screen.

 

In other words, the object's render function needs to know about the camera. The camera doesn't need to know about each object.

Share this post


Link to post
Share on other sites

It seems like you're having a similar issue to the gentleman in this thread - I posted a few posts there about making the objects have "global" (relative to the map) positions, and then calculating the "local" (relative to the camera) positions on-the-fly when drawing the objects.

Share this post


Link to post
Share on other sites

The camera should have its own coordinates. These coordinates will determine where the screen is currently positioned in the game area. When you render an object on screen, you do so in relation to the camera's coordinates. Just subtract the camera's coordinates from the object's coordinates to determine where it should be drawn on screen.

 

In other words, the object's render function needs to know about the camera. The camera doesn't need to know about each object.

Interesting! I never thought about adding an extra parameter from the Camera object to the draw method.

 

Thanks for the feedback!

Share this post


Link to post
Share on other sites

It seems like you're having a similar issue to the gentleman in this thread - I posted a few posts there about making the objects have "global" (relative to the map) positions, and then calculating the "local" (relative to the camera) positions on-the-fly when drawing the objects.

Wouldn't adding a global variable or a constant in Java violate encapsulation of an object?

 

I do like your posts on that thread. A Great example and ideas! Thanks!

Share this post


Link to post
Share on other sites

Global means "world space" in that context, it would be a property of the camera object (world space position) used to render the scene.

Oh I see. So it is more of a convenience.

Share this post


Link to post
Share on other sites

If you tell a computer to draw something, it's going to draw it relative to the screen. Your graphics API requires coordinates relative to the screen to draw things.

Instead of moving hundreds of objects when you really want your character and the camera to move, store all your objects (and the position of your character and the position of your camera) in "global" coordinates. By "global" I mean world-space instead of screen-space.

Don't measure your microwave's coordinates relative to your TV or relative to the refrigerator - choose one specific point in the world (0,0) - the world "origin" - and measure the coordinates of every object, and the player, and the camera, relative to that origin-point.



Because your graphics API still needs to draw objects in screen-space, but your objects are stored in world-space, then whenever you are ready to draw you do a quick calculation to convert the coordinates between one space and another space (the calculations are explained in the other thread).

 

Is the world always the origin? Something like this: Given Point as a object that has two parameters x and y.

 

Point world = Point(0,0);

 

I do like the above example and the another thread you posted! I do feel like I understand it more than yesterday. I'm going to give myself a good night sleep again over this concept. 

Edited by warnexus

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!