well
I figured it out! Kinda!
play in browser
left/right arrow keys to rotate camera around scribble
How does it work? Let me learn you how.
tl;dr: RAYCASTING
long answer:
the camera has its own rotation value. we'll call it "rotation".
take the x and y position of the character you want to follow (yes, 3D would use Z, but a lot of the time the Z of the camera would be level with the object. if not, use similar methods as I use for x and y). we'll call these variables, "x" and "y".
figure out the maximum distance you want the camera to be able to go from the object. We'll call this variable, "max_d". the actual distance, we'll call that just simply "d".
now, for some code. I'll explain it.
while(d<max_d)
{
d++;
xx = x + Math.sin(rotation * Math.PI / 180) * d;
yy = y + Math.cos(rotation * Math.PI / 180) * d;
if(walls.are_touching(xx,yy)) break;
}
what does this do?
the "while" loop stops everything until the condition is fulfilled (until the distance is equal to the maximum distance). xx and yy are the variables we're using to figure out where on the grid the camera will be placed. then, after every time it adds one more pixel of distance between the subject and the camera, we see if the position [xx,yy] on the grid is inside of any of the walls (the walls.are_touching function is up to you to figure out -- whether you use a software like game maker and it comes with one, or you're programming from scratch and have to make your own). if the new position of the camera is inside the walls, then we "break" the loop -- make it stop where it's at. there you go -- the camera stops right at the walls!
now, just draw your camera's position at xx and yy.