I am looking for a way to accomplish something similar to GetViewportSize but only for the actual visible area. I.e., the camera bounds rather than the screen bounds.
It is important in my game that the players not see beyond the intended area, thus a fixed aspect ratio. The player uses the mouse to indicate where the pawn should move to. I accomplish this using the mouse coordinates fed into a LinePlaneIntersection node to get the corresponding world location of the mouse. In order to keep the player from moving the pawn off screen I need to constrain the coordinates fed into the LPI to the visible area. However, constraining it requires knowing where the bounds are, hence my question.
why not just constrain the aspect ratio of the camera? then you could use nodes like “get hit result under cursor” or “deproject screen to world” or you could run your own trace from the camera to the direction of the cursor.
The aspect ratio is fixed. The problem is that the black bars are part of the viewport, so the result if the mouse is in the black bar is a location that is not visible.
sorry i was thinking only the area shown by the camera was a valid world space. anyway i came up with another solution its not very elegant but maybe itll give you some ideas to work from.
what i did in the picture below was to get the players screen resolution (assuming fullscreen here), then divide the X axis by the aspect ratio (since screens are generally wider than tall). the result we then get is the size of the camera display , IE viewable area - black bars (well the correct resolution of that area). from there we know that the camera is centered on the screen so we do a little math to get the center of the player screen then add and subtract half the camera resolution, this give us the actual X coordinates of the camera. last we just run that into a inrange node to tell us if the cursors location (value in) is within the min and max, if the cursors location is outside the range we know its over a black bar.
theres probably a better or simpler method but i dont know it as of yet. hope this helps a bit though good luck.
Ah, yeah math… that makes sense! I fleshed it out a bit and here is the result for anyone viewing this question who might be interested.
Calculating the “camera viewport”
Converting screen location to worldspace. Hit result under cursor is fine too if you are looking for a point in the world geometry, but this way will allow you to control the plane that it hits - useful if you always want the pawn to move to the mouse location and not geometry that isn’t necessarily inline (e.g., a wall or platform.)
Setting up some traces for confirmation (Green is the corrected coordinates)
Note that you’ll want to cache the mouse location and only update it when it’s within the correct bounds so when it leaves the view area (or the window) it won’t cause any problems.