# Capsule calculations

Hi! I need to do some calculations with capsules not much different from a capsule collider, but i can't use colliders for this one. So the first question is: do you know of any mono or unity class that already has this? Or if there isn't any then how could i create a method that decides whether or not a capsule contains a point? And how about a method about overlapping capsules?

I'm thinking i need 2 points and a radius to describe the capsule. I already have a way of telling if a sphere contains a point and if it overlaps with another sphere if that helps.

So any ideas?

**Answer** by HarshadK
·
Dec 02, 2016 at 12:00 PM

For point inside capsule: check for point inside spheres and point inside cylinder. These are articles with math and code for point inside cylinder.

For capsule-capsule test, here is an article discussing the same: Swept Capsule-Capsule intersection (3D). Even though the article is titled for sweep test, it has info and source on simple capsule-capsule intersection.

**Answer** by JedBeryll
·
Dec 02, 2016 at 02:02 PM

In case anyone else needs this:

```
public class Capsule {
public Vector3 Pos1 { get; set; }
public Vector3 Pos2 { get; set; }
public float Radius { get; set; }
public Vector3 Direction {
get {
return Pos2-Pos1;
}
}
public Vector3 Center {
get {
return (Pos1 + Pos2) / 2f;
}
}
public Capsule (Vector3 p1, Vector3 p2, float radius) {
Pos1 = p1;
Pos2 = p2;
Radius = radius;
}
public Capsule (Vector3 center, Vector3 direction, float length, float radius) {
Vector3 d2 = direction.normalized * length / 2f;
Pos1 = center - d2;
Pos2 = center + d2;
Radius = radius;
}
public bool Contains(Vector3 point) {
if (Sphere.Contains(Pos1, Radius, point) || Sphere.Contains(Pos2, Radius, point)) {
return true;
}
Vector3 pDir = point - Pos1;
float dot = Vector3.Dot(Direction, pDir);
float lengthsq = Direction.sqrMagnitude;
if (dot < 0f || dot > lengthsq) return false;
float dsq = pDir.x * pDir.x + pDir.y * pDir.y + pDir.z * pDir.z - dot * dot / lengthsq;
if (dsq > Radius * Radius) {
return false;
}
else {
return true;
}
}
}
}
public class Sphere {
public Vector3 Center { get; set; }
public float Radius { get; set; }
public Sphere (Vector3 position, float rad) {
Center = position;
Radius = rad;
}
public bool Contains (Vector3 point) {
return Contains (Center, Radius, point);
}
public static bool Contains (Sphere sphere, Vector3 point) {
return Contains (sphere.Center, sphere.Radius, point);
}
public static bool Contains (Vector3 spherePos, float radius, Vector3 point) {
float dist = (spherePos - point).sqrMagnitude;
return dist <= radius * radius;
}
}
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Cross with cubes using for? 0 Answers

Moving sine wave curve 2D 2 Answers

C#. Inappropriate mesh vertex coordinates returned by another class 0 Answers

How to SHOOT an object on a curved path without using Rigidbody 2 Answers

Received an error: RenderTexture's anti-aliasing changed from 2 to 1 due hardware limitations. 0 Answers