Slick2D Camera movement

Started by
11 comments, last by Zaphyk 9 years, 3 months ago

yeah i know slick2d is dead and i should use libGDX but i had trouble setting up it so i decided to use slick, anyway is a test project which was already started and i am gonna use libGDX on the real one. Lets get to the topic, which is the way of making a camera in a 2d enviroment, the camera should follow the player like this.
O7wBudW.png

I´ve searched and i found a way which was moving the map instead but this gave me a few questions.
Is moving the map the only way?
What happens if the game is multiplayer?

Advertisement

The concept of a "camera" is an illusion; it's essentially moving the world in the opposite direction, to give the illusion that the "camera" is moving.

If there is multiplayer, you have to decide what is important to you, and how many viewports you want.

Sorry, i dont understand

Which part are you having trouble understanding?


If there is multiplayer, you have to decide what is important to you, and how many viewports you want.

This one


If there is multiplayer, you have to decide what is important to you, and how many viewports you want.

This one

Different games have different priorities for what's shown on screen. For some games, it makes sense to have the "camera" focused on the first player only, for others, maybe the center of the line segment between the two players, and for others, still, the map would be scaled to fit the screen so that all players are visible at all times.

Alternatively, if you decide to have two viewports instead of one (splitscreen), you can draw the map twice, and each half of the screen is centered on each respective player.

It all depends on your game.

Okay so now i understand it. But if i move the world instead how i´d detect when it reaches its end or when the player collisions something, some pseudocode?Another thing in libGDX there something called OrtographicCamera what is it and how it works?

Okay so now i understand it. But if i move the world instead how i´d detect when it reaches its end or when the player collisions something, some pseudocode?Another thing in libGDX there something called OrtographicCamera what is it and how it works?

Usually a camera isn't like an actual camera it doesn't have code that records the world or something, instead it acts more like data, telling you where you are currently "looking" in the 2d/3d world, you can then use that information to offset your drawing based on that fact.

I'm not super familiar with slick but if it gives you some kind of "sprite" class with a position and draws it onscreen then you essentially want to take the camera's position and draw objects relative to it. I.e. if your screen is 100x100 across and there is a sprite at 10, 0(based on a top left origin) then you would take its position and subtract the camera position from it. So if the camera is at 5, 0 then we draw the sprite at 10-5, 0-0. So it ends up at 5, 0 relative to the screen.

You can also let the 3d camera do the work if they give you access to it, if you ask to draw a sprite at 10, 0 then the library may have some kind of "view" class that manipulates the view matrix, that would automatically translate sprites for you based on the position you set the view to(i.e. you set the view to 5, 0 and draw the sprite at 10, 0 then the underlying rendering code will draw it as if it were 5 from the left of the window.)

I mentioned the first method because you'll end up having to use the first method anyway if you're drawing something like tiles. Usually you want to take the camera position and try to figure out what objects are visible to the camera and only ask to draw those, that way the renderer doesn't have to go through the trouble of starting to draw them and then culling them because they are outside the viewport.

As for your question, if the camera is a class or the view is a class you can clamp the position you set the view or camera to so that it won't go outside the bounds of your virtual world.

So i should create a camera class containing its x, y ,width, height and whenever i render should check if is inside its bounds before doing it. right? I didint get the part about collision, if i am always moving the map instead of the player how would i detect when the player is in front of a rock or at the boundaries of the map?

It isn't quite moving the map. You are moving the world. This includes the player, and every other actor in the scene. Handle all of your collision logic and other such events as if the world is all there is, and don't think about the camera. Then, when all is said and done, draw the world offset by the camera's position. The drawing of the world and the logic of the entities within should and must be separate.

This topic is closed to new replies.

Advertisement