How to calculate surface area of a CapsuleShape2D using the radius and height in GDScript.

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

I know that it has something to do with geometry, and I know it uses the PI number because of the radius, but I don’t know the calculation I have to do. I want to know the shape’s surface area because I need to change it’s vertical size and I know I can do it by getting the surface area square root (correct me if I’m wrong about that). Well any help will be good for me, and I hope it is possible to calculate. It seems totally calculable though.

:bust_in_silhouette: Reply From: jgodfrey

A capsule is really just a rectangle with a 1/2 circle on either end. So, as far as calculating the area, it’d simply be:

  • The area of the rectangle (width x height)
  • The area of a full circle with a diameter matching the width of the rectangle

In Godot’s definition of a CapsuleShape2D you define both a radius and a height value. The radius is the radius of the 1/2 circle on each end. The height is the height of the rectangle between the 2, 1/2 circles. So, a CapsuleShape2D with a height of 0 is essentially just a circle (with a zero height rectangle between the 2 halves).

So, for a real example, let’s say we have a CapsuleShape2D defined as follows:

Radius: 10
Height: 20

So, we have the following primitive shapes

  • 1 complete circle with a radius of 10
  • 1 rectangle with a width of 20 (radius * 2) and height of 20

From that, just calculate the areas of the 2 shapes.

  • For the circle, that’d be PI * radius squared, so 314.6
  • For the rectangle, that’d be width x height, so 400

Add those together to get the area of the capsule → 714.6

Thank you for the help, but I found out that for the capsule shape height follow the vertical size of the thing I was using I just needed to take the thing vertical size and subtract by the double of the capsule shape radius.

Caua_SCP | 2022-10-08 00:06