Clamping 2 Players to a view

Roman P.

Member
any suggestions to clamping two players to a view? i think i kinda got it but i didn't...
one player seems to be able to drag the other if moving in positive directions

just looking for idea what might be going wrong

my camera's setup so it's centered around two players

GML:
//2 PLAYER CAMERA
between_x = round( (object0.x + object1.x) / 2 );
between_y = round( (object0.y + object1.y) / 2 );

var _x = clamp(between_x - view_width/2, 0, room_width - view_width);
var _y = clamp(between_y - view_height/2, 0, room_height - view_height);
    
camera_set_view_pos(view_camera[0], _x, _y);
player code (both)
Code:
cameraX = camera_get_view_x(view_camera[0]);
cameraW = camera_get_view_width(view_camera[0])
cameraY = camera_get_view_y(view_camera[0]);
cameraH = camera_get_view_height(view_camera[0]);

x = clamp(x, cameraX, cameraX + cameraW);
y = clamp(y, cameraY, cameraY + cameraH);
2 players are just 16x16 sprites that have origin in the center
i feel like something just needs to be offset somewhere but can't figure out what. feels like tried everything lol
 

NightFrost

Member
Many times these things - also depending on type of game of course - have a view that zooms in and out to keep both players visible no matter the distance. Or the screen is split. You've coded a clamp to view position, so it seems to me the dragging you get is what you intended to have. Another alternative would be distance lock; don't allow players walk further from each other than view dimensions minus player sprite dimensions. That is, when player A moves, distance from player B clamps their new position, and vice versa.
 

Roman P.

Member
Well the dragging only happens when moving right or down. Left and up works as intended.

I'm going to give the distance lock a shot. That seems like a simpler solution but might cause some other issues as the camera is locked to "tunnel" segments megaman style.

Thanks. I'll come back with an update!
 
Top