Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How do you get viewport coordinates only of the visible area when using a fixed aspect ratio

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.

Product Version: UE 4.21
more ▼

asked Feb 06 '19 at 10:56 PM in Blueprint Scripting

avatar image

58 3 9 12

avatar image ThompsonN13 Feb 06 '19 at 11:21 PM

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.

avatar image Rakosman Feb 07 '19 at 01:39 AM

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.

avatar image ThompsonN13 Feb 07 '19 at 03:38 AM

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.

alt text

capture.png (291.9 kB)
avatar image Rakosman Feb 07 '19 at 05:16 AM

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"

alt text

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.)

alt text

Setting up some traces for confirmation (Green is the corrected coordinates)

alt text alt text alt text

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.

viewportfix.png (681.1 kB)
toworldspace.png (328.2 kB)
debuglines.png (615.9 kB)
tootall.png (596.7 kB)
toowide.png (556.8 kB)
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question