Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 WillNode · Aug 13, 2015 at 02:36 PM · c#collisionrigidbodyvelocitysphere

Predict Spherical Ball Collision Direction

Hi, I got a problem when doing on my project (kind of billiard game)

alt text

assume I have a two ball with different mass. I want the second Ball move on my Target direction using collision forces when collide with first ball. so I create a Target Point containing a vector data which the first ball move on. the target Point of course it has to slight off from the Second ball Center of Mass.

now this is my question.... How much it (the Target Point) is far from second ball's center of mass (assuming both of it has different radius and mass).

here's how I did it :

 void Launch(Vector3 dir, Vector3 dir2){
 //first ball is handling this script
 //dir is Position of Second Ball
 //dir2 is Position of Second Ball has to heading at during collision
 
 //get Angles
 float ang = Vec2Angle (new Vector2 (dir.x, dir.z), new Vector2 (transform.localPosition.x, transform.localPosition.z));
 float ang2 = Vec2Angle (new Vector2 (dir.x, dir.z), new Vector2 (dir2.x, dir2.z));
 
 //Get Angle direction of Second Ball heading at
 float deltaAngle=Mathf.Abs (Mathf.DeltaAngle (ang, ang2))
 //Because we need to target it into Opposite angle...
 deltaAngle += 180;
 //get relative position
 Vector3 rPos= dir2 - dir;
 //determine Target Position offset <- THE PROBLEM
 float offset= Mathf.Sin(deltaAngle*Mathf.Deg2Rad);
 rPos=rPos.normalized * Mathf.Lerp(0.3f,0.6f,offset); //0.3 is the second ball Radius
 
 //Now add it into our Rigidbody Velocity
 dir+=rPos;
 GetComponent<Rigidbody>().velocity=(dir-transform.localPosition).normalized*40;
 }

 public static float Vec2Angle (Vector2 dir, Vector2 pivot)
 {
 Vector2 s = dir - pivot;
 if (s.x > 0)
     return Vector2.Angle (Vector2.up, s);
 else
     return 360 - Vector2.Angle (Vector2.up, s);
 }

the current code isn't work properly (it slightly off from my targeted direction).

if you have questions about this, just ask. I'm really appreciate for your time here.

picture1.png (50.4 kB)
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

1 Reply

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

Answer by sparkzbarca · Aug 13, 2015 at 06:45 PM

Distance = ball1 radius + ball two Rafius

Create a Ray with origin ball two, direction - target direction so it's opposite direction.

Use Ray.getpoint(distance) to get the point to aim the moving ball one at.

Mass doesn't matter per say, like it effects where the balls end up after imPact but

not if they do unless you under shoot

Comment
Add comment · Show 3 · 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 WillNode · Aug 14, 2015 at 12:34 AM 0
Share

well, indeed i didn't think about it. took me a while to realize it. previously already using it but everytime it goes miss (collision not detectable at extreme degree). taking per frame look and now I realize it goes right, setting my rigidbody as continous dynamic and now it work correctly. thanks for it....

avatar image sparkzbarca · Aug 14, 2015 at 08:02 AM 0
Share

That's not practical for a large number of balls, also remember to try to keep speeds realistic for good collisions well that look real. If your missing the collision I imagine they are going very fast if not you might be better cheating a bit still.

Do a simple distance check if distance to Aimpoint is less than say .005 shove the ball toward the other ball a bit to force the collision. Better than the penalty from continuous dynamic

avatar image rayos · Aug 08, 2016 at 07:18 PM 0
Share

I dont undestand this solution :S can you put a short lines of code to look a example?

Distance = between 2 balls position? after ray of ball number 2 position, direction-target direction? I dont undestand this above line

thanks :D

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

26 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

Related Questions

Need help with scripting bug fix. pleease 0 Answers

void OnCollisionEnter(Collider other) { if (Rigidbody.velocity.magnitude > 5) {...} - In "Rigidbody.velocity.magnitude" asks to give link to the object??? 2 Answers

Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer

Dragging object out of position 2 Answers

Transferring velocity from kinematic to a rigid body 0 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