Crossing non collider Capsules

Attached is a picture of hitbox and hurtbox, both of which are composed of a vector3 start, vector3 end, and a radius. https://ibb.co/cPKY2v

I wanted to know what is an inexpensive (processing power wise) to determine if the two hitcapsules are colliding.

I mean in the way that if they were not capsules, but just bubbles I could just say :
if (Vector3.distance(me.pos, other.pos) < me.radius + other.radius)
print(“is colliding”);

Is there something that can be done that way (as in not using unity’s built in physics colliders, but just math stuff) to figure out if the hitboxes are colliding?

Are you intentionally not wanting them to be capsules, but rather a sequence of spheres?

Cause there is capsule overlap logic (if you want it to be capsules), and there is sphere overlap logic (if you want them to be a sequence of spheres).

In the case of the sphere’s you’d just have to loop over all spheres for one, and test them against all spheres of the other.

Sphere collision logic is stupid simple. You just check the distance between the 2 spheres, and if it’s smaller than the 2 radiuses combined… then you’re overlapping. (use square distance, and square radius for faster performance)

bool Overlapping(Sphere a, Sphere b)
{
    float rsum = a.radius + b.radius;
    float dist = Distance(a.position, b.position);
    return dist <= rsum;
}

Capsule intersection is slightly more complicated, it’s very similar to Sphere intersection, with a twist.

It’s essentially, define a line segement from sphere to sphere of each capsule. Then measure the distance between both line segments (an algorithm in itself), and if that is less than the sum of the radii, you’re overlapping.

bool Overlapping(Capsule a, Capsule b)
{
    float rsum = a.radius + b.radius;
    float dist = Distance(a.segment, b.segment);
    return dist <= rsum;
}

Thing is you have to define the algorithm for Distance between line segments.

So… homework assignment for you?

sigh no that was the single reason I made this thread, I already know about all the other stuff in your post.

And no, the gizmos are a representation of a capsule not a sequence of spheres. Because if a character is made of several lengths of spheres and suddenly a full figure with all the limbs is 200 spheres, that sounds inefficient because everyone of them needs to check collision one at a time. As opposed to making up characters out of capsules, like in super smash bros melee, where characters were usually just 12 capsules for body, and 4 capsules for attacks.

How about capsule to sphere collision?

So wait… you know the algorithm for capsule intersection?

You knew that you needed the line segment distance algorithm?

But you don’t know HOW to do a line segment distance algorithm?

And you thought it’d be better to ask us, while failing to mention you know everything BUT the line segment distance algorithm… and feel that asking us, as opposed to you know… googling it since it’s a well known algorithm that has existed for quite some time and isn’t some guarded secret.

There’s an old joke about that…
http://lmgtfy.com/?q=line+segment+distance+algorithm

I always kept this picture around for it:

I’m sorry, I fail to believe that you knew all that since you failed to mention any of it… and srsly, how freaking hard it is to just ‘google’ your answer. You’d rather sit around and wait for someone else to do all the work for you, rather than you know… find the answer… which would take far less time, and just ever so slightly more effort.

Do your homework, I will not just handfeed you your answer if you already know so much.

You say that, but in the OP I myself presented a bit of code that does circle-to-circle collision as an example of what I know that I’m not looking for, somehow I guess you didn’t notice that since you basically made a script that operates on that same technique.

As for how I knew about the line-segment thing. I have a history of making shooter games before I made the move to Unity, and a lot of them express bullets not as instant traveling rays but as glowing and moving beams, they used line-segments with thickness against spheres(the player hitbox) to determine if they were hitting the enemy

My first game when I was in highschool:

And an experimental 3rd person shooter I did for my brother when I was trying to get him into game design:

Still refuse to believe it, now that I’ve given you evidence?

I did google it too, by the way. But here is the thing, since I started learning coding with GML, and with that most of the stuff I used was raw trig, when I made the move to c# for Unity, I started out trying to make collision systems that winded up taking many more calculations than they did because I didn’t use/know how to use (at the time) stuff like unity’s Vector3 and Quaternion.

Yea I could just plug in the first thing that shows up on google’s search for line segment collision, but just because it works out in the end doesn’t say much for how efficient it is for Unity.

Hence, I created this topic, where there exist people who I am to assume should have a better understanding of making scripts that is efficient for unity.

This IS the unity scripting part of the forum after all, I fail to see why expecting a helpful “unity script” in the responses is unreasonable.

You do know there’s a lot more in my post than just circle circle intersection.

OK, and my point still stands. You didn’t bring it up, yet you knew EXACTLY what you needed to learn, and instead of mentioning it… or even researching it yourself… you decided to indirectly ask about it.

Just because you made a game doesn’t mean you know the math required. Lots of people on these forums make games every day and don’t know basic algebra.

Again… this is a math question. And if you knew what you needed… why not ask “hey, what is the best way to write a line segment distance formula?”

It’s not that, it’s that you didn’t ask your actual question. And instead asked a more vague version of it.

And when I gave you a direction to go… giving you the basic outline of how the algorithm works. And stating now you may want to do the last little bit of leg work for yourself.

Cause I’m a “teach a man to fish” not a “give a man a fish” kind of helper.

And your response was an indignant sigh…

That’s what this is about.

Now I COULD just take that line segment distance formula and put it into the form of a Distance method for you. But why would I do that now? What incentive do we have to do that? You have your sources, you know what’s needed. All you literally need to do is write a few lines of code based on some college level maths that are readily available out there. If we write that for you… we are depriving you of that experience.

I mean hell if you’re so awesome at making all these bad ass games since highschool. I guess the next natural step in growing your experience base is to learn to write these algorithms in an efficient way.

Maybe take a hack at it, post what you got, and see if anyone knows of a way to make it more efficient.

1 Like

You know, I didn’t want to make this topic. I went into this expecting you (not you specifically, but the kind of person you are saying the kind of stuff you say). But I made the topic anyway, because I had the smallest glimmer of hope that someone far more experienced with Unity than I would have at some point wanted the effects I asked for and maybe already walked that path to have come to an empirical conclusive technique, or at least some useful advice on ways that I might be thinking about getting the effect that turned out to be dead-ends, or otherwise a really bad idea because of how much strain they can put on processing power.

I don’t really see why you keep trying to ham me up, that’s kinda weird. I don’t claim to be to make bad ass games, those games from highschool mostly operate on rudimentary high school trig. I happen to have zero academic programing experience in game design, I am an artist who happens to have an interest in programming. Everything I’ve accomplished is the result of self teaching. All that means is that I’ve picked up a lot of habits when programming which I’ve noticed lack a lot of efficiency. And people who’ve seen my code tend to bring that up to me. I’ve been a throw a bunch of stuff at the wall until something stays on the wall kind of learner my entire life. That comes with the distinct disadvantage of sticking with whatever the first systems I create that happen to get the job done, no matter how inefficiently they execute it.

This is the reason why I don’t believe it’s particularly smart to just latch onto the first things that show up on google search when trying to create an optimal system for the code to get the desired effect.

@ refusal to believe, I call shifting the goal post you’ve decide nothing will convince you on that, no point in going further with that.

@ your point “still standing” No, because you want that to be what the question is about. It’s not. I’ve done a few line-segment with width collision systems in unity in the past to. But I do not trust that the systems I was operating them with are efficient. The last time I tried it, for example, used a transform to represent point A, and giving it a length and radius value. It checked 2 sphere collisions each at the end, and used transform.inversetransformpoint() to check if the other hitbox was within the cylinder between the two spheres. The fact that I’ve made systems that accomplish the effect means that I technically don’t even need anyone’s answer for my current project to work. But I’m here asking about it because I’m sure that a better programmer who’s been using unity for more than the year and a half I have been using it, would be more familiar and thus know of a wiser, better, and more efficient way to get the effect done less expensively processing power wise.

The reason I didn’t just ask for a line-segment to line-segment formula, is because, while I’m aware that that’s one way, I’m open to ideas that hypothetically might be better. What I “need to learn” isn’t the formula for it, what I want to know is ways to get the effect of capsule to capsule (non-collider) collision that are efficient (which is exactly what the OP said).

@ , I’ve been aware of that since your first post, you made it really clear. But it’s a philosophy of teaching I don’t agree with. I’m not a good programmer, but I’ve become a proficient 3D modeler and animator.


I’ve developed techniques in my craft to optimize those processes. So anytime anyone in the past ever asked me about things that they feel they are not adequate at understanding, like how to 3D model cartoon/anime eyes or hair, or good topology for the part of the body where the thigh connects to the hip, clothing folds, how to set up IK handles, optimal ways to create a bone skeleton to attach the model as a skin. I explain to them exactly how to get the effect. Because the fact is that it’s a journey I already walked, and at the moment when someone asks me how they can do it to, I have the power of saving them from spending weeks, months, or years walking through the same path I did to get the effect by just letting them stand on my shoulders to get to the end of the rainbow. And knowing that they now have the control to use a technique I use because I like it, feels good, because they don’t have to screw around trial-and-erroring through frankensteins and just get to having fun being using that new skill to be creative. The fact is that I’ve seen many cases where the more frankenstiens that an artist created without fully realizing the vision they hoped for, the more chances they have to just give-up in frustration.

I remember as a middle school girl just getting into the magic of game design with Game Maker, that the Game Maker Community seemed to operate in that way too, under the logic that the senior programmers have the power to help lift of the noobs using their vast wisdom and knowledge of GML. But I couldn’t sprite, I made 3D stuff, GM is not good at 3D, so I made the move to Unity.

You’re hung up on this “refusal to believe”… I don’t refuse to believe. I don’t know to believe. If you don’t know the answer, you’re lying when you say you do. If you do know the answer, you’re rude for being vague about it, and expecting others to do the leg work for you.

I don’t care which way it is. Both are egregious.

So why don’t you start there. Come to us with THAT, and be like “hey… i don’t trust this to be efficient enough. Does anyone else have any ideas on how to eek out some extra performance?”

Also… maybe follow that with “I profiled it in the unity profiler, I see bottlenecks here and here.”

We don’t know what you know, we can’t read your mind! You want to start vaguely off with “hey, here’s a problem… solve it for me, and if it’s not to my expectations, I’m going to huff/puff/sigh. So you better get it right!” How am I to know that you know the most COMMON method of testing capsule intersection, when you’re asking for a method of doing it!?

Put in some legwork! And we will do the same in return!

You say you’ve done these things… but you’re not showing it to us.

I gave you some legwork in my initial post. Give a little back. That’s all that’s being asked.

For me to give you anything more… I’m lost. So you DO know how to do a shortest distance between line segments. OK, so I don’t have to break down the entire algorithm for you? What then? Write a version of it that is well optimized for you? Why should I be doing this work? How do I know what work I should be doing for you, if you don’t tell us? And again… why should we? You seem very ungrateful…

Listen dude, I was trying to help. You just didn’t like it… ok, tootle a loos.

1 Like