Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 lochan_vishwanath · Jan 26, 2021 at 07:51 AM · performancenewbiepoolphysics 2d

Trying to create a '2D 8 Ball Pool' game - Trajectory Prediction

I am trying to create a game similar to (Pocket Run Pool) https://www.youtube.com/watch?v=ewlAE6NQnsI&t=215s a 2D pool game that is much simpler than a 3D one I need help with creating code for trajectory prediction something similar to this https://docs.unity3d.com/Manual/physics-multi-scene.html

How do I go about doing this?

alt text

pocket-run-pool-2.png (274.8 kB)
Comment
Add comment · Show 2
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 logicandchaos · Jan 26, 2021 at 12:32 PM 0
Share

you need to use trigonometry and line renderer. Will have to do some research.

avatar image RealFolk · Jan 26, 2021 at 12:45 PM 0
Share

I reccomend using an alternate physics scene. Its simpler than using math and can give you much more accurate results.

This link should get you started. https://youtu.be/4VUmhuhkELk

1 Reply

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

Answer by jackmw94 · Jan 26, 2021 at 12:47 PM

To find the ball/wall that you're going to hit, use the "CircleCast" method in Physics2D. Make sure the radius is the same as your white ball. For this to work, all your balls and cushion sides should have colliders on.

This will give you your collision point. The raycast hit that this returns will have a "normal" vector - this is the vector stating the direction perpendicular to the collider edge at the point where you hit it. So in your image, you can visualise the normal if you draw an arrow from the centre of the 8-ball to the centre of the dashed ball, this will point straight out from the 8-ball surface at the point where the collision occurs.

This normal will be the opposite to the direction in which you'll move the ball you just hit, i.e. -normal will be the white arrow you circled, coming out the 8-ball.

You can find the next trajectory of the white ball by reflecting your initial white ball movement across this normal.

 Vector2 bouncedWhiteBallDirection = Vector2.Reflect(initialWhiteBallDirection, hit.normal);


When writing behaviour with vectors, I strongly recommend using gizmos to show you the directions and positions you're using visually in the scene view. In your monobehaviours you can implement the function OnDrawGizmos then inside that you have access to all the Gizmos methods. For example:

 private void OnDrawGizmos()
 {
     Gizmos.color = Color.red;
     Gizmos.DrawLine(whiteBallTransform.position, whiteBallTransform.position + initialWhiteBallDirection);
 }


This would draw a red line in the direction of "initialWhiteBallDirection" out from the white ball position.

Let me know if you have any questions about this!

Comment
Add comment · Show 6 · 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 lochan_vishwanath · Jan 27, 2021 at 07:47 AM 0
Share

Will the white arrow (circled in red) change it's angle as the direction of the cue is changed?

avatar image jackmw94 lochan_vishwanath · Jan 27, 2021 at 08:47 AM 0
Share

That’s the idea :) The circle cast will either detect a ball and then you can check the collision normal to find this angle or it will hit a wall in which case there’s no other ball to move, just the next cue ball direction. The white arrow changes as the collision point changes

avatar image lochan_vishwanath · Jan 27, 2021 at 04:25 PM 0
Share

Problems: 1.radius of the circle and white ball not matching values being used in inspector = 0.52 (also represents DrawSphere radius) 2. How can I change the length of the normal dynamically as shown in the gameplay video?

alt text

Solution so far (Image Attached) Code:

 public class PhysicsTest : $$anonymous$$onoBehaviour
 {
     GameObject currentHitObject;
 
     float maxDistance = float.PositiveInfinity;
     public float radius;
     public Layer$$anonymous$$ask layer$$anonymous$$ask;
 
     private Vector3 origin, direction;
     private Vector2 hitdistance;
 
     private float currentHitDistance;
 
     private void Update()
     {
         origin = transform.position;
         direction = transform.up;
 
         RaycastHit2D hit = Physics2D.CircleCast(origin, radius, direction, float.PositiveInfinity, layer$$anonymous$$ask, float.NegativeInfinity, float.PositiveInfinity);
         if(hit)
         {
             currentHitObject = hit.transform.gameObject;
             currentHitDistance = hit.distance;
 
             hitdistance = hit.normal * -1;
         }
         else
         {
             currentHitObject = null;
             currentHitDistance = maxDistance;
         }
     }
 
     private void OnDrawGizmosSelected()
     {
         Gizmos.color = Color.blue;
         Debug.DrawLine(origin, origin + direction * currentHitDistance);
         Debug.DrawLine(currentHitObject.transform.position, hitdistance * 5);
         Gizmos.DrawSphere(origin + direction * currentHitDistance, radius);
     }
 
 }

avatar image jackmw94 lochan_vishwanath · Jan 27, 2021 at 05:12 PM 0
Share

I'm not too sure what you mean with your first point - where did you find the 0.52 value? :)

Your second point is an interesting one:

What we want here is to scale the line in accordance with how "in line" the initial cue ball direction and the inverse of the normal are - this can be found using the dot product of the two. Here is the dot product in action: https://miro.medium.com/max/984/1*GiXSS_fhvPLsKI50ofdrnA.gif

It's a scalar value, so only a single number, unlike the vectors we use to create it. It's the magnitude of the vector created by the projection of one vector onto another.

 float normalScale = Vector2.Dot( direction.normalized, -hit.normal );


This code will find you the dot product for your initial direction and the hit ball's direction. You can then use this to scale your hitDistance.

avatar image lochan_vishwanath jackmw94 · Jan 28, 2021 at 02:36 PM 0
Share

I am facing weird behaviour -> https://gph.is/g/Z8oOXbb any reason this is happening? am adding force in -> direction = transform.up;

         if(hit)
         {
             currentHitObject = hit.transform.gameObject;
             currentHitDistance = hit.distance;
 
             hitdistance = hit.normal * -1;
             GetComponent<Rigidbody2D>().AddForce(0.1f * direction, Force$$anonymous$$ode2D.Impulse);
         }
Show more comments

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

130 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Activating GameObject(s) is slow 2 Answers

Pool/Cache Unity colors when used by many objects 0 Answers

Optimal way to check repeatedly close objects 0 Answers

Object Pooling, have some question marks 0 Answers

Most performance friendly way to "unrender" a GameObject? 6 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