[SOLVED] How can I set up a camera that moves smoothly at sub-pixel increments?

D

Danimtz

Guest
So I posted this on reddit, but I might as well post it here to see if anybody can help. Basically I'm trying

I'm trying to make a small top down shooter game in which the camera follows the player and the mouse position similar to Enter the Gungeon(This is a unity game) or Nuclear throne in Gamemaker Studio 2.

My first attempt I tried to simply create a view that follows an object. Then in that object's step event having this code:

x=mean(mouse_x,obj_Player.x);
y=mean(mouse_y,obj_Player.y);

However when I move the mouse around the camera always moves at increments of 1 pixel. Here is an example of what I mean

I have done research on how other pixel games acomplish a smoother camera:

  • Steam post by the developer of Enter the Gungeon explaining how the camera works in their game(I know this game is made in Unity, however the concept is the same).

  • This article from Gamasutra about scrolling in 2D games gives some ideas on how to do it. At one point in the article it mentions how Hyper Light Drifter achieves this smooth camera by rendering the game at 480x270 resolution but then having the camera scroll at full resolution.
The last thing I have tried is to use surfaces to achieve this. By having my application_surface be resized to 480x270:

surface_resize(application_surface,480,270)

Then when drawing the surface scaling it by a factor of 4 to give a 1920x1080 resolution. Then I tried to set up a new surface on top of the application_surface at 1920x1080 and have the camera scroll smoothly there. My reasoning is that each pixel in the application_surface should equal into 4 smaller pixels in the new surface, and should be able to allow the camera to move 4 times per pixel instead of just 1 pixel at a time.

I am fairly new to gamemaker so I'm not sure I am doing this correctly. Any help on how to to get this working would be greatly appreciated.
 
Okay. So first, here's how to "enable" subpixels.

You need to resize your application surface to be bigger than your view. As long as your game window is larger than your view, that's it.

Once you've done that, moving your view at non-whole numbers will automatically take advantage of sub-pixels IF you are moving your camera manually. Do NOT use the "object following" setting in the room editor. You need to position view_xview and view_yview in code so that it uses non-whole numbers.

I have seen some strange issues where this wouldn't work, but that was a very isolated issue.
 
D

Danimtz

Guest
Okay. So first, here's how to "enable" subpixels.

You need to resize your application surface to be bigger than your view. As long as your game window is larger than your view, that's it.

Once you've done that, moving your view at non-whole numbers will automatically take advantage of sub-pixels IF you are moving your camera manually. Do NOT use the "object following" setting in the room editor. You need to position view_xview and view_yview in code so that it uses non-whole numbers.

I have seen some strange issues where this wouldn't work, but that was a very isolated issue.

I have spent days trying to figure out how to get the camera to move off the pixel grid. This is the first time it has actually worked. Now I will just have to figure out how to get it to move while following the cursor/player and moving at subpixel increments manually. I'll figure it out.

I cannot thank you enough Pixelated_Pope.

EDIT:

https://gyazo.com/eb2041fae5e2c750d3ecba8b5bdae000

More or less got it working. Mouse position needs adjustments but it works now :D. Thank you very very much.
 
Last edited by a moderator:
C

ChrisBognar

Guest
I have spent days trying to figure out how to get the camera to move off the pixel grid. This is the first time it has actually worked. Now I will just have to figure out how to get it to move while following the cursor/player and moving at subpixel increments manually. I'll figure it out.

I cannot thank you enough Pixelated_Pope.

EDIT:

https://gyazo.com/eb2041fae5e2c750d3ecba8b5bdae000

More or less got it working. Mouse position needs adjustments but it works now :D. Thank you very very much.
I know this is a little late to be asking, but do tiles work properly with sub-pixel movement? When I tried implementing this none of my tiles scaled
 
Top