That's the code I'm using. (Note: it's converting 3f vectors to 2d ones, but you don't have to worry about that). I need the result of the calculation to be between 0 and 1 I.E. 0.5 or something.

If I'm still not explaining right maybe this will help?

My question is: How do I get the distance of a point in a 2d triangle without calculating perpendicular vector's distance? I.E. if the tringle is facing up (y = -1) without any tilt I would need the distance in the triangle without any X.

Still not clear enough. The "top" of the triangle is relative. Think if it in terms of only a point in space and its relation to a triangle in space. Sounds like you want the closest point on the triangle.

OR, if you're talking something in global space where negative on the global Y axis is always down, then you can cast a ray from the point to the triangle along the Y and get that distance.

Yeah, now you're asking for something totally different - sphere/circle and triangle collision.

I think that would go something like first testing if the center of the circle is inside the triangle using our previous method (if so, then obviously you have a collision), then by checking to see if the point is within radius distance of any of the sides (if so, we have a collision).

@Eli Delventhal: how do you prevent a true response with infinitely far spheres that lie in the volume confined by the 3 planes? You have to check for the distance to a 4th plane too: the plane on which the triangle lies.

IMHO the method name 'isPointInTriangle' is incredibly misleading in 3D.

Hi, appreciate more people! Σ ♥ = ¾Learn how to award medals... and work your way up the social rankings!

@Eli Delventhal: how do you prevent a true response with infinitely far spheres that lie in the volume confined by the 3 planes? You have to check for the distance to a 4th plane too: the plane on which the triangle lies.

IMHO the method name 'isPointInTriangle' is incredibly misleading in 3D.

Not sure exactly what you mean, Riven, but I'm sure this has issues in 3D. I was thinking in terms of 2D despite all the vectors being 3D vectors. If you can suggest specific code edits or be more clear that'd be handy for everyone.

Don't create new objects in code that is performance critical enough to get rid of a square root

You're always such a friendly person, Riven. I find the second part of your post to be purposelessly confrontational. This is not going to end up being a bottleneck in his code. First of all, he doesn't use the vector class which has that function; the one I wrote in 30 seconds in an example java class (which you can see above if you were to scroll up). He uses the LWJGL vector class which just so happens forces you to instantiate extra vectors all over the place.

The entire point of posting that code snippet was to show him how to do what he needs to do, because clearly he doesn't understand enough to be worrying about performance at this juncture. I considered not putting the squared magnitude up there (and using regular magnitude instead) because I figured he would get confused - because efficiency isn't the point here. Looks like I should have done that, poor choice on my part.

Instead of your usual spiel of busting into a post and saying "you're wrong, you're wrong," how about to respond to the question I posted above by clarifying what you meant when you pointed out the only actual bug in the code?

Not meant to be so rough on you, I thought that the emoticons revealed that I wasn't too serious.

As per your request, here's an image that shows the problem in 3D:

From this, you can deduce that a sphere->triangle intersection works like this:

ensure that the sphere either lies above or intersects plane A,B,C

ensure that the sphere intersects plane D

Note that there is no need to calculate 'is on same side', which btw. contains a lot of redundant calculations, when you do it 3 times. The planes A,B,C already (should) have normals pointing towards the triangle.

Your code would have returned true for all spheres intersecting in the 'triangle volume' and those intersecting with the triangle edges.

Hi, appreciate more people! Σ ♥ = ¾Learn how to award medals... and work your way up the social rankings!

Not sure what you mean. Points do not have a direction, so they can't face anything. And that code you pasted doesn't have any normals in it. You shouldn't need to redo anything to fix that bug - instead how you make your player react to collisions needs to be adjusted.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org