How to fix this glitch in my 2D platformer?

Started by
3 comments, last by r1ckparker 11 years, 5 months ago
Hi, guys. I just recently finished a level editor for my 2D platformer. I am now working on a program that will actually play the levels that I have created. However, once I started making the camera system, I began to have problems. I want the camera to constantly keep the player in the center of the screen. The screen is 1280 pixels wide. I am trying to always keep the player's x value(represented by player\x#) at 640. So, optimally, the player will always stay at that position. As the player moves, cameraX will keep increasing, and we can scroll the map. I am programming in Blitz Plus. Here's my code for trying to keep the player at the center of the screen:



player\xCameraBoundingBox# = player\x# + 640
If player\xCameraBoundingBox# > 1280 ;if this box has passed the edge of the screen
cameraX = cameraX + (player\xCameraBoundingBox# - 1280) ;add it to camera x
player\x# = player\x# - (player\xCameraBoundingBox# - 1280)
EndIf



However, this poses some problems. Sometimes when the player tries to make a jump, they will be "pushed" back and fall to their death. However, if I don't tell the game to keep the player at the center of the screen, the player will eventually be able to run off the screen, or they will be pressed against the edge of the screen and will not be able to see anything that is coming.

Do you guys have any ideas on how to fix this problem? Any ideas would be appreciated. Thanks.

Here's a link to an article that shows what kind of camera system I am trying to make. I am trying to make a platform-locked camera system.
http://soherearemyid...er-cameras.html
I am the man who will become the pirate king!
Advertisement
Wouldn't it make more sense to let the player move freely, and then simply render the world with world coordinates offset in such a way that the player would always end up drawn in the middle of the screen? This way you avoid ugly "stay in the center" hacks (and as a bonus, this feature can be easily toggled on and off)

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Bacterius, sorry if this is a stupid question, but could you please post some pseudocode? Because if I just keep letting player\x# increase, wouldn't the player eventually run off the edge of the screen?
I am the man who will become the pirate king!
If the player is allowed to run off the edge of the map, how is that the camera's fault? The camera should be strictly an observer: it observes the player's position and uses that position value to center its view upon. If you allow the camera to modify the player's position, rather than leaving that up to the player's logic and the constraints on motion provided by the level setup and geometry, then you are just asking for trouble. The camera should not be an active participant in game logic. It's just a watcher.
I think his problem is that the world is in 2d so you have to calculate where on screen you should be drawing everything

So the players's x would increase when you press 'right', so playerx could be 3000 for example (the player is 3000 units to the right of his starting point)

therefore the player would always be drawn at screen co-ordinate 640 but the world would be offset around him - the draw window (what is drawn on screen) would be playerx-640 to playerx+640

so you would look up your level map and draw everything from 3000-640 to 3000+640.

This topic is closed to new replies.

Advertisement