• 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 easilyBaffled · Dec 27, 2012 at 01:23 AM · rotationrigidbodyballturning

How to roll a ball along a curved tube.

I am developing a slalom game. I have a ball rolling over a tube, it can rotate all around the edge of the tube. So far the ball will constantly roll forward. The problem is that at some point there will be a turn in the tube I have no clue how to have the ball turn with the turn section. I can't just do look at because it won't smoothly turn and the turn piece can be at any angle. The control script is below and has its own problems here: http://answers.unity3d.com/questions/369543/quirky-unasked-for-rotation-with-ridigidbodies.html I've also included a picture of the turn, in this case the ball will be rolling down and to the right. Any ideas?

 var rot;
 var tube : Transform;
 function Start(){
     rigidbody.AddForce(Vector3.down*9.8);
 }
 function FixedUpdate () {
     rot = Input.GetAxis("Horizontal") * -10;
     if(rot != null && tube != null){
         Debug.Log("working");
         transform.RotateAround(tube.position, Vector3.forward, rot);
     }
     else{
         //rigidbody.AddForce(Vector3.forward*20);
         rigidbody.AddTorque (Vector3.right * 20);
     }
     rigidbody.AddForce((tube.position - transform.position) * 5 * Time.smoothDeltaTime);
     
     
 }
 function OnCollisionEnter(collision : Collision){
     if(collision.transform.tag == "tube"){
         tube = collision.transform;
     }
 }

alt text

screen shot 2012-12-26 at 8.22.24 pm.png (37.7 kB)
Comment
Add comment · Show 20
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 sacredgeometry · Dec 28, 2012 at 07:19 AM 0
Share

Could you make an animation along a curve thats placed in the centre of the tube in your 3d software then write a script that sets where along the animation the gravitational force is based on the balls relative position?

avatar image easilyBaffled · Dec 28, 2012 at 02:35 PM 0
Share

I'm sorry but I just barely scraped this together in $$anonymous$$aya, I have no clue how to make an animation, let alone do any of the rest that you said, could you perhaps dumb it down for me please?

avatar image clunk47 · Dec 28, 2012 at 05:12 PM 2
Share

Check out my pinball project. I had this exact need for one of my tables. Watch the video HERE. Watch for the first 5-10 seconds and you'll see my ball traveling through the metal "tube". How I did this, I placed waypoints inside my tube, and an "enter" and "exit" cube at each end of the tube, which are both triggers. When the 1st trigger is hit, the ball's rigidbody is set to kinematic, and a script kicks in that iterates through my waypoints, and uses Vector3.$$anonymous$$oveTowards to travel to each target waypoint. Once the "exit" trigger is hit, the ball's rigidbody is switched back to non-kinematic. Let me know if this is what you're looking for, and I'll dig up the old project from my backup drive and strip down the code a bit to fit your needs.

avatar image easilyBaffled clunk47 · Dec 28, 2012 at 09:09 PM 0
Share

First I'd like to say, your pinball table looked pretty good I liked the lighting and sound. Second why does the ball have to become kinematic cant the waypoints be triggers on their own. And Third, my tube is going to have a significant amount of more space than yours did, do you think the ball would still act in the right way with the ball being anywhere on the tube? Fourth did that last question make sense?

avatar image clunk47 clunk47 · Dec 30, 2012 at 11:36 PM 1
Share

I had it become kinematic, because I'm not using physics while the ball is in the tube. If I have physics enabled for the ball while it's going through the tube, the ball would try to use gravity and would not travel correctly. Once the ball hits the "exit" trigger, physics are re-enabled so I can play again like normal.

avatar image clunk47 clunk47 · Dec 30, 2012 at 11:38 PM 1
Share

And yes, this would work for your tube, so long as you place enough waypoints. The close the waypoints are together, the more accurate this will be. If you're going to have a bunch of waypoints, you could write up a script that will automatically place them throughout the tube.

avatar image Fattie clunk47 · Jan 01, 2013 at 10:23 AM 1
Share

@clunk you put so much time in to this amazing you didn't get more votes

avatar image clunk47 clunk47 · Jan 01, 2013 at 01:49 PM 1
Share

@Fattie thank you, Sir, I appreciate your feedback, and hope you have a happy new year :D

avatar image easilyBaffled clunk47 · Jan 13, 2013 at 11:24 PM 0
Share

@clunk, I've tried a few other methods, I'd like to try yours now seeing as you got it to work. If your offer is still on the table could I please see your code snippet

avatar image sacredgeometry · Dec 28, 2012 at 11:27 PM 0
Share

in that case how about just making it stay certain distance from the surface using the mesh and surface normals as a guide for rotation.

avatar image easilyBaffled · Dec 29, 2012 at 10:42 PM 0
Share

well that is how I tried to do it before but I couldn't figure out how to get the normals to give me what I wanted. I thought accessing the vertices or triangles of the curve would help but again count figure out how to get the information. Do you know how to get that stuff?

avatar image AlucardJay · Dec 29, 2012 at 11:02 PM 1
Share

http://answers.unity3d.com/questions/168097/orient-vehicle-to-ground-normal.html

avatar image sacredgeometry · Dec 29, 2012 at 11:06 PM 0
Share

Thanks thats a much better link than one I could find

avatar image easilyBaffled · Dec 31, 2012 at 12:30 AM 0
Share

Almost, almost. Unless I read it wrong, which is a possibility, it is still missing the crucial part of rotating the ball on its y axis to match the bend of the tube. I see the y being moved with user input and curDur but thats done in a different part for me.

avatar image sacredgeometry · Dec 31, 2012 at 04:06 AM 0
Share

alucardjs post explains that you use the balls vector.up i.e. Vector3(0,1,0) with the surface normal to calculate the angle of rotation using basic trigonometry ...

http://www.wikihow.com/Find-the-Angle-Between-Two-Vectors|

...either that or you could just set the balls rotation to be exactly what the normals is which might be a bit more difficult when it comes to interpolating between values to get a smooth transition.

avatar image Fattie · Jan 01, 2013 at 10:08 AM 0
Share

guys, he just wants to do it kinematically - it's just an everyday holder object

(I made a drawing below)

That takes care of "rotating the ball on its y axis..." etc for you, baff.

avatar image easilyBaffled · Jan 06, 2013 at 02:17 AM 0
Share

Well I supposes I could do it kinematically like you & clunk47 suggested, but I have a feeling would be cleaner if I could do it mathematically, it should be just a few lines of code, I just can't figure what they are. I don't need the angle between the normal and the forward, I need to know the angle of the vertex is. If you imagine my image as a wireframe then then I need to know where the wire directly below the ball is pointing.

avatar image Fattie · Jan 07, 2013 at 10:57 AM 0
Share

(note that if you do it "mathematically" that is in fact doing it "kinematically" -- "kinematically" is just a $2 word meaning "animated", ie you personally move it each time. As opposed to the physics engine moving it using momentum, collisions and forces.)

the "wireframe" won't help you

mesh is not even slightly smooth, it is not nearly smooth enough to base animations on. That is a basic of game engineering.

(If the rendering looks smooth on screen that's just because the renderer smooths mesh - that's the whole idea.)

If you want to "do the math yourself" (rather than using the game engine??)...

then you do it just as in my diagram below.

You start at the center point of the "coin". You move the "coin" along the central path. you twist the coin to follow the path.

you move the white ball out the radius, and rotate it around that center point.

Note that if you simply use the game engine (a transform) to do the job, it is doing .. exactly the same calculation!! Heh!

All the best with it

avatar image easilyBaffled · Jan 09, 2013 at 12:25 AM 0
Share

I've been looking at your coin example, and thanks for that, the problem is that you are assu$$anonymous$$g there is a center line in the turns, unfortunatly when I built it I didn't have the for sight to do that. Do you know any way of quick drawing one at instantiation or to calculate one?

avatar image Fattie · Jan 10, 2013 at 10:22 AM 0
Share

No, it's simply impossible to do a tube game (or any similar idea) unless you have a known centerline.

I'm very sorry for the bad news.

2 Replies

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

Answer by Fattie · Dec 29, 2012 at 08:51 AM

Here's how to do it !

alt text

Notice the "coin" ...

The "coin" would be invisible. But make it visible at first, so you can see what you are doing.

The "coin" would be the same diameter as the tube. But at first make it 10% bigger so you can see it clearly. It will peek out of the tube.

Notice the "coin" has the small ball attached to it.

Notice the "FORWARD" direction (arrow) of the "coin".

As the "coin" moves through the tube:

The FORWARD direction must always match the forwards direction in the tube at any differential - in other words, the tangent of the centerline at any point.

(Indeed - this is how you define a tube! By sweeping out a surface using a circle.)

To make the user steer "left and right" just rotate the coin (of course, rotate it around it's FORWARD axis).

Code requirements: need the centerline of the tube. Need a line of code that moves the coin along the centerline. Need a line of code that sets the FORWARD to the current tube tangent.

So that's probably the easiest way to do it! This is flawlessly mechanical - recall that the way you produce a tube is by sweeping out a circle in space - so the SmallBall will always be exactly on the surface. Happy new year!


coinsweepsouttube.jpg (184.4 kB)
Comment
Add comment · Show 8 · 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 Lovrenc · Jan 01, 2013 at 10:12 AM 2
Share

I cant even tell you how excited this post made me even though i dont need this. Great idea and well written.

avatar image Fattie · Jan 01, 2013 at 10:39 AM 0
Share

BTW the real beauty here: see the larger drawing of the coin with the ball. imagine you add a line of code to make the ball rotate correct ("rolling forwards" if you will) as seen in the larger drawing.

Then - incredibly - as you rotate the control coin, it magically makes the rotation of the ball work perfectly 'on' the surface of the tube.

Otherwise it's an nightmare to make it roll properly considering other movement, etc.

Furthermore!!!! Regarding the roll of the white ball "left and right". You trivially add a line of code that makes it rotate (simply based on it's diameter) locked to the rotation of the "coin" - it's that easy. The whole thing is sort of miraculous.

I love stuff like that where you don't have to do any work :-)

avatar image sacredgeometry · Jan 01, 2013 at 12:35 PM 0
Share

Interesting solution I am not sure how reliable it would be if you are relying on colliders to feed the coin down the tube but worth a try :)

avatar image Fattie · Jan 01, 2013 at 02:07 PM 0
Share

Hi @sacred ! this is unrelated to colliders. you do not use an colliders here. It is a kinematic solution.

avatar image tgate · Jan 08, 2013 at 10:17 PM 0
Share

Does anyone have any thoughts on the best way to get the center line of the tube?

Show more comments
avatar image
1

Answer by ChrisBliss · Dec 28, 2012 at 01:33 AM

Can you read out the normal of your collisionpoint and apply a force in the opposite direction? then it should alway stick to the surface instead of rotating around the tube. your asset looks fairly 'highpoly' so it should look smooth enough

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 easilyBaffled · Dec 29, 2012 at 10:51 PM 0
Share

Like I said to sacredgeometry, I am not sure how to access the normal then get its direction. I imagine its something like: access collision/raycast point, access point normal, find its "direction"/perpendicular to the ground. but its really that third part i cant really figure out

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Non-rotating collider fixed to a rolling ball 1 Answer

How can i roll sphere/Ball having rigidbody with Rigidbody properties 0 Answers

Turning a rigidbody that's in motion 2 Answers

Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 3 Answers

Pysics not working as expected 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