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
1
Question by Ssiroo · Aug 21, 2016 at 07:30 PM · 3dvector3directionanglenormals

Unity3d find angle/direction of a Ray / Raycasthit

Hello,

I'm trying to make a Billiard game and I wanna calculate the direction on which the Cue Ball(white ball) will be moving after it hits another ball.

alt text

As you can see I wanna calculate the angle/direction in which the RAY hits the ball and the angle/direction on which the raycast will change its direction into. I need the angle to display as a Vector3 variable so I can use it on the linerenderer(3).

I already calculated the direction that the ball that gets hit will go.

If you could help me on this that would be great!

Current code:

 RaycastHit hitz;
 if (Physics.SphereCast(transform.position, 0.8f, location - transform.position, out hitz, Mathf.Infinity, lmm2))
 {

     lineRenderer2 = hitz.collider.GetComponentInChildren<LineRenderer>();
     lineRenderer2.SetVertexCount(2);
 
     if (!Input.GetKey(KeyCode.Mouse0))
         lineRenderer2.SetPosition(0, hitz.point);
     
     if (!Input.GetKey(KeyCode.Mouse0))
        {
            Vector3 start = hitz.point;
            Vector3 end = start + (-hitz.normal * 4);
     
            if (lineRenderer2)
            {
                if (!Input.GetKey(KeyCode.Mouse0))
                    lineRenderer2.SetPosition(1, end);
            }

            if(lineRenderer3)
            {
                   anglelel = Vector3.Angle(hitz.normal, hitz.point);
                   Vector3 cross = Vector3.Cross(hitz.normal, hitz.point);
                  if(cross.y > 0)
                  {
 
                          tzt = Quaternion.AngleAxis(90f, hitz.normal) *realStick.transform.forward;
                  }
                  if (cross.y < 0)
                   {
                           anglelel = -anglelel;
                            tzt = Quaternion.AngleAxis(270f, hitz.normal) * realStick.transform.forward;
                    }
                    Vector3 start2 = hitz.point;
                    Vector3 end2 = start2 + ((tzt) * 5f);
                    lineRenderer3.SetPosition(0, hitz.point);
                    lineRenderer3.SetPosition(1, end2);
                }
         }
 }


Thank you for your time.

Edit:

This part of the code has been changed to this one, currently makign some progress but still, it's not good enough. Before

 if(lineRenderer3)
         {
             Vector3 start2 = hitz.point;
             //THIS IS WHERE I'M CURRENTLY STUCK AT
             Vector3 end2 = start2 + (hitz.point * 0.7f); 
             lineRenderer3.SetPosition(0, hitz.point);
             lineRenderer3.SetPosition(1, end2);
          }

After

 if(lineRenderer3)
 {
      anglelel = Vector3.Angle(hitz.normal, hitz.point);
      Vector3 cross = Vector3.Cross(hitz.normal, hitz.point);
      if(cross.y > 0)
      {
 
           tzt = Quaternion.AngleAxis(90f, hitz.normal) *realStick.transform.forward;
      }
       if (cross.y < 0)
       {
           anglelel = -anglelel;
           tzt = Quaternion.AngleAxis(270f, hitz.normal) * realStick.transform.forward;
        }
        Vector3 start2 = hitz.point;
        Vector3 end2 = start2 + ((tzt) * 5f);
        lineRenderer3.SetPosition(0, hitz.point);
        lineRenderer3.SetPosition(1, end2);
   }
angle.png (24.2 kB)
Comment
Add comment · Show 1
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 Ssiroo · Aug 22, 2016 at 03:19 PM 0
Share

Bump. Sorry for the spam but I really need this.

1 Reply

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

Answer by Bunny83 · Aug 22, 2016 at 05:09 PM

Well, the rules are pretty simple. The ball you hit moves along the hit normal and the ball that hits moves along the tangent of the hit point.

Wikipedia ellastic collision has this animated gif which should explain everything:

So what you have to do is:

  • get the normal vector of your collision

  • calculate the tangent (in 2d simply rotate the normal 90°)

  • Project the original velocity vector onto both normalized directions (normal, tangent)

  • Apply the resulting vector as new velocity vectors for each ball.

This of course assumes that the two balls have the same mass.

This is how you calculate it manually. Keep in mind that the physics system might give you a different result due to some inaccuracies and rotations involved in the collision. Also keep in mind when you calculate it manually that it's possible to get a chain reaction of new collisions when more than 2 balls are involved. This might be difficult to simulate with a limited frame rate / certain step size.

For example a "split" of two balls with a third will result the wrong direction of the cueball if you process one collision at a time.

Comment
Add comment · Show 2 · 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 Bunny83 · Aug 22, 2016 at 05:16 PM 0
Share

In the 3d case you would need to calculate the correct tangent. This can be done by using "Vector3.ProjectOnPlane" and projecting the direction vector between the two balls onto the plane with the hit normal as normal. You might want to take some extra care when the direction is in line with the hit normal. In this case it's a central collision, so the cue ball stops and the other moves in a straight line

If you also need the reflection off a wall it's basically the same as with two balls, just that the impulse that would drive the other ball (which is now the wall) is simply reflected back onto cue ball) So you simply apply both velocities to the ball but reverse the vector that goes "into the wall"

avatar image Ssiroo · Aug 22, 2016 at 07:04 PM 0
Share

Hey, that did the trick. Thank you very much for your help!

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

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get Direction from 2 Vectors - and Apply to Transform 0 Answers

Transform angle to grid position 2 Answers

Why does gravity's vector3 act so weird? 1 Answer

how to find direction between two points 2 Answers

Angle to Direction 1 Answer


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