This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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

https://docs.godotengine.org/en/3.1/getting_started/step_by_step/your_first_game.html#moving-the-player

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

in Engine by (22 points)

1 Answer

+1 vote
Best answer

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.

by (22 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.