Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sidar · Jun 15, 2011 at 11:38 AM · linetriangleintersect

Line > triangle intersection

(I know unity has a physics component, this is for a school assignment)

I'm trying to implement these methods for mesh collision but Its failing.

http://fwheel.net/gdw/wiki/index.pl?LineSegmentTriangleIntersection http://fwheel.net/gdw/wiki/index.pl?ParametricPlaneEquation

I do think I misunderstood something from either of the two tutorials. And I can't figure out what exactly.

My code so far: (Obviously this isn't about speed or efficiency, it just needs to work =) )

 int index = 0;
     for (int i = 0; i < faces.Length; i += 3) {

         {

             Vector3 A = vertices[faces[i]];
             Vector3 B = vertices[faces[i+1]];
             Vector3 C = vertices[faces[i+2]];

             Vector3 Adelta = B - A;
             Vector3 Bdelta = C - A;

             Vector3 n = normals[index];
             Vector3 nNormalized = n;
             nNormalized.Normalize ();
             Vector3 negativeN = -(nNormalized);

             for (int j = 0; j < ballArray.Length; j++) {

                 Ball b = ballArray[j].GetComponent<Ball> ();
                 Vector3 u = (b.transform.position + (nNormalized * b.radius)) - b.transform.position;
                 Vector3 v = A - b.transform.position;
 
                 float nu = Vector3.Dot(u,nNormalized);
                 float nv = Vector3.Dot(v,nNormalized);

                 if(nu == 0)
                 {
                     continue;
                 }

                 float r = (nv/nu);
 
                 if(r >= 0 && r <= 1)
                 {
                     Vector3 interSectionPoint = b.transform.position + r * u;
                     Vector3 w = interSectionPoint - A;            

                     float sParamTop = (Vector3.Dot(Adelta,Bdelta) * Vector3.Dot(w,Bdelta)) - (Vector3.Dot(Bdelta,Bdelta) * Vector3.Dot(w,Adelta));

                     float tParamTop = (Vector3.Dot(Adelta,Bdelta) * Vector3.Dot(w,Adelta)) - (Vector3.Dot(Adelta,Adelta) * Vector3.Dot(w,Bdelta));

                     float dominator = (Vector3.Dot(Adelta,Bdelta) * Vector3.Dot(Adelta,Bdelta)) - (Vector3.Dot(Adelta,Adelta) * Vector3.Dot(Bdelta,Bdelta));            

                     float s = sParamTop/dominator;
                     float t = sParamTop/dominator;
                     if( s>=0 && t>=0 && s+t<=1)

                     {
                         //print("true");
                         b.reflect(n);
                     }
                 }
                 continue;
             }        
             index++;
         }
     }


Thank you for reading

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jun 15, 2011 at 01:24 PM

Ok, I'm a bit confused about your line direction...
As far as i understand the normal array contains the face-normals (which could be calculated easily). Your testing line segment start at the balls position and goes along the facenormal of the face you're testing. That doesn't make much sense to me.

This line is funny:

 Vector3 u = (b.transform.position + (nNormalized * b.radius)) - b.transform.position;

It's the same as

 Vector3 u = nNormalized * b.radius;

I would recommend to rename your variable so they help to understand what they actually hold.

u is your line direction vector (the vector between start and end point)

Since your line direction is always the same as the face normal, this:

 float nu = Vector3.Dot(u,nNormalized);

equals this:

 float nu = b.radius;

That's a really strange case of intersection. Do you actually want a line-triangle or a sphere-triangle intersection?


edit

I've uploaded my test scene if you'r interested ;)

UnityPackage

You can drag the sphere around during playmode when you switch to the scene-view. If there's an intersection point with the plane it will show as yellow 3D-cross

If the barycentric coordinates are within 0..1 (so it's inside the triangle) it will show the cross in magenta

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sidar · Jun 15, 2011 at 01:35 PM 0
Share

I'm rather confused myself. I want to check if a line intersects with a triangle. Since we are talking about "spheres" representing the balls, I guess you could say I would want sphere-triangle intersection. But since I'm working with the radius I thought Ill just check if a vector from the center point of the ball to radius bounds intersect with a triangle. If so I need to reflect it on said face normal.

I need to find those parameters which are 0<= p <= 1.

For the direction vector, I was actually trying to create a vector wich points in the opposite direction ( not in this code i've shown) of the normal and see if that one intersects with the triangle...

The assignment is due tomorrow, and I have no clue how to fix this.

Thanks for the reply though.

avatar image Bunny83 · Jun 15, 2011 at 01:53 PM 0
Share

I just tested the code and it works ;) well as far as it can...

  1. The intersection just tests the line from ball center along the face-normal-vector therefore the ball can intersect at the edge of the triangle but it won't be recognised. If you're inside a "closed" geometry it doesn't matter (eg pool table )

  2. Your line normal should be inverted. You've calculated it but you don't use it.

Define your "u" like this (note the "-"):

 Vector3 u = -nNormalized * b.radius;

That would result in this "nu":

 float nu = -b.radius;
avatar image Bunny83 · Jun 15, 2011 at 02:06 PM 0
Share

I've note some strange cases where it detects a collision where it shouldn't... Something is still wrong :D

avatar image Sidar · Jun 15, 2011 at 02:09 PM 0
Share

$$anonymous$$eh,

I still don't get it. I guess I'll have to retake this assignment next year. I'm just not seeing it anymore.

Thanks for the reply.

avatar image Bunny83 · Jun 15, 2011 at 02:15 PM 0
Share

Got it :D

Copy&Paste error :D You used sParamTop TWO times. when calculating "t" you should use "tParamTop" ins$$anonymous$$d of "sParamTop"

Show more comments
avatar image
0

Answer by rocki · Jun 15, 2013 at 05:54 PM

This is a great post.

Do you have the final working script. I am making a game and need exactly this test. Would really appreciated it if someone can provide a working version.

Cheers.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sidar · Jun 15, 2013 at 06:19 PM 0
Share

This was like 2 years ago. But if you look at the answer given and the comments you should be able to copy paste the code above with the adjustments from the answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Detect where line intersects outline shape 1 Answer

Draw a line via script and make a solid with it.. 1 Answer

How to create a Mini Map at runtime 2 Answers

Line Renderer and Collider Question 0 Answers

How to move an enemy on a curvy line? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges