GameMaker Help with simple 2d camera rotation

V

voltorben

Guest
I’m building a game where the player should be able to rotate the camera and the gravity will shift to the new orientation.

But I’m having some problems with my rotated camera.

My room has a viewport of 320x240 px (20x15 tiles)
Viewports are enabled.

My camera object in the room is setup like this:

o_camera, create:
Code:
camera = camera_create_view(0, 0, 320, 240, 0, o_player, 3, 3, 120, 120);
view_camera[0] = camera;
This camera renders my room correctly and follows the player, and stops the camera at the edges of the room.

However, when I change the code to use a rotated camera instead, like this:

o_camera, create:
Code:
camera = camera_create_view(0, 0, 320, 240, 90, o_player, 3, 3, 120, 120);
view_camera[0] = camera;

The camera also renders the room correctly and follows the player somewhat, but not all the way to the edge of the room: The camera stops 2.5 tiles from the original E and W edges of the room (and the player will disappear behind the camera), and the camera shows an extra 2.5 tiles on the original N and S edges (of the room background).

As you might have noticed the number 2.5 is not random - it’s half the difference of the w and h tiles: (20-15)/2 = 2.5

What am I doing wrong here? Have I misunderstood how I should be using the camera/view dimensions?
Or the rotation?


I feel like this should be very simple. I also have a half working solution with a matrix_build_lookat and matrix_build_projection_ortho camera, but the matrices are giving me a headache, especially how I’d go about interpolating the xup and yup of matrix_build_lookat when rotating the camera (so if you have advice for that, I’m all ears).

Any help or ideas appreciated :)
 

Dupletor

Member
It feels like a scale problem. Try swapping 320 with 240.
I think it would work if the camera were the size of the room, therefore you can rescale it the way you need for it to work. xD
 
V

voltorben

Guest
It feels like a scale problem. Try swapping 320 with 240.
I think it would work if the camera were the size of the room, therefore you can rescale it the way you need for it to work. xD
I did try to swapping the two values, but that just resulted in a stretched view.
Yeah, that's probably true - or just have a 1:1 view :p Unfortunately not so elegant.

Anyways, thanks for your input :)
 
V

voltorben

Guest
OK - I figured an alternative way to make it work: Ditching the build-in camera follow.
Interestingly enough it kind of gives some insight as to why the build-in camera follow does not work (at least as I expected).

So to follow a target and make the camera not go over the edges in the room at the normal angle, I do like this:
Code:
var camX = clamp(target.x, camW/2, room_width - camW/2);
var camY = clamp(target.y, camH/2, room_height - camH/2);
camera_set_view_pos(view_camera[0], camX - camW/2, camY - camH/2);
But when i switch to a 90 deg camera it gives the exact same result as the build-in follow function described in my OP...

However, if i switch the camW/2 and camH/2 on the 90 and 270 degree rotations, it will clamp correctly!
Code:
var camX = clamp(target.x, camH/2, room_width - camH/2);
var camY = clamp(target.y, camW/2, room_height - camW/2);
camera_set_view_pos(view_camera[0], camX - camW/2, camY - camH/2);
Now i just have to replicate the speed and border settings to my custom camera follow (should be pretty simple) and i have my rotating 2d camera!

Wondering if this is a bug, or if it's just me that has a weird use case, and haven't wrapped my head around how it should really work?

I'm still interested in using the build-in camera follow function, if anyone has the answer :)
 
Last edited by a moderator:
Top