Detecting bounds of CapsuleShape2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By epuscasu

I’m having trouble clamping a CapsuleShape2D’s position so it doesn’t leave the viewport (following the Your first game tutorial)

sprite not reaching top of window

The issue is that I can’t seem to properly specify the top / bottom offset (the horizontal clamping works fine, the sprite can touch the left/right sides just fine). What I’m doing (and seems logical):

-clamp x between capsule radius and screen_size.x - capsule radius (this works fine)
-clamp y between (capsule height + capsule radius) and screen_size.y - (capsule height + capsule radius)

position += velocity * delta

position.x = clamp(position.x, 0 + $CollisionShape2D.get_shape().radius, screen_size.x - $CollisionShape2D.get_shape().radius)

position.y = clamp(position.y, 0 + $CollisionShape2D.get_shape().height + $CollisionShape2D.get_shape().radius, screen_size.y - $CollisionShape2D.get_shape().height - $CollisionShape2D.get_shape().radius)

the sprite can’t go higher than where it’s at in the screenshot
(same goes for bottom, there’s a padding).

The CapsuleShape2D Radius/Height is 27 and 15 and matches the sprite used in the tutorial.

:bust_in_silhouette: Reply From: epuscasu

Solved this (thanks to @aux4 via Discord) by halving the height. I was under the impression the capsule’s height was half the capsule cylinder instead of the whole thing.