- Home /

# Need help with the physics and angles of gerbils in a plastic ball..

Ok, so I know that's an odd question, but the problem is simple.

I have an inverted sphere, with an object inside it, resting at the very bottom. So if the sphere is at 0,0,0 and the radius is 100, then the object is resting at 0,-100,0. Think of this "object" as a Gerbil, or a Hamster.

Now, here's where I'm having trouble with the math.

If the Gerbil moves along the "ground" (along the xz plane), I want the sphere to move with him. Now that would be as easy as setting the position of the sphere, relative to the Gerbil. But I also want the sphere to have *rotated* by the appropriate amount in 3d space.

So rather than try to move the sphere, I want to rotate it in such a way that the gerbil is always on the bottom.

I'm having trouble figuring out the math to calculate the delta angle on the edge of the sphere, based on the delta position that the Gerbil has moved.

Any ideas?

EDIT: To clarify - I need the sphere to be anchored at 0,0,0 - and the hamster's movement just rotates the sphere. Hamster controls the sphere, the sphere does not control the hamster.

Are you trying to do this with Unity physics (Rigidbody), or are you doing a direct manipulation of the transform? The angle change is easy to figure out assu$$anonymous$$g perfect rotation. The circumference of the circle with be 2*PI*R. Given a specific amount of ground travel the angle will be 360.0 * movement / circumference. If this is 3D, you can use the direction of the cross product of the gerbil direction with Vector3.up to get the axis of rotation.

Basically, I have a player controller inside a giant sphere. As the player walks in any direction, I want the sphere to rotate under him so his feet are always on the "lowest" point of the sphere.

So the player could look "UP" and see something cool on the far opposite side of the sphere that would appear upside down to him, and begin heading in that direction, and eventually it would meet him. It's a person walking on the "inside" of the world. Journey to the centre of the earth, kind of thing.

**Answer** by glenrhodes
·
Apr 06, 2014 at 03:21 PM

So I solved this by letting Unity do the work. Basically, I created a separate "Simulator sphere" with a rigidbody, and placed it elsewhere in the world, on a massively large plane.

Then, I place the camera at the bottom of the large "world" sphere. Then, I look at the Input.GetAxis to see which way the player is moving. I then AddForce to the "simulator sphere", and it rolls according to the forces.

I then set the rotation of the world sphere to be equal to the rotation of the "Simulator sphere". So it effectively feels like you're controlling the ball that you're inside of, and works perfectly as expected, even though the world sphere never moves, it just rotates.

You feel like you're walking around this "hollow earth" world, but really, it's just rotating to match what a sphere elsewhere in space is doing in response to your key presses. Works quite nicely.

**Answer** by perchik
·
Apr 03, 2014 at 03:09 PM

If I was doing this, here's how I'd approach it:

In 2d, as your gerbil moves forward, he's running on the edge of a circle, which is an arc of the circle. Given the arc and the radius, you can find the angle pretty easily.

In 3D, there is always some 2D circle in the same plane as the gerbil (if you think about the gerbil as a flat plane; but the same thing should work if you just consider the gerbil's midline). So then it's a matter of finding two things: first, the angle the circle traveled and second, the axis that that circle is centered on relative to the sphere.

The first part is easy, uses the same math from above. The second part is more interesting/complex. It seems to me that there's some rotation around the vertical axis that will describe the circle, since the gerbil is always on the ground. So I'd figure out what that is, (probably by getting angle between gerbil.right and ball.right or something like that), then do the math to figure out how far the ball needs to move in that direction. Rotate the ball by the first angle, around the vertical axis, then rotate by the second angle around the right axis.

Maybe. Who knows.

Thank you for this. I love the diagrams especially...

I'm trying to figure out the last part... "probably by getting angle between gerbil.right and ball.right"... I tried rotating by "player.transform.forward * velocity" just to see what would happen.. and it basically worked.... but at some angles, really weird things happened.. the sphere around you started rotating very oddly.. sideways, fast, slow, wobbly.. this is breaking my brain.

You want to do an Quaternion.AngleAxis() rotation for rotating the ball. You can find the axis by:

```
var axis = Vector3.Cross(gerbil.forward, Vector3.up);
```

Hmm, that sort of worked -- but it still seems that as you get further away from the "zero" point of rotation, the weirder the rotation gets. At a certain point in the sphere, the rotation ends up going perpendicular to your .forward.. and then as you work your way back around to the bottom of the sphere, it comes back to normal again..

Yeah, so I'd probably do it as two separate rotations,

first I'd do something like

```
float dir =Vector3.Angle(player.forward, ball.transform.forward);
ball.rotate(ball.transform.up, dir);
```

(so now the balll faces the same way as the player)

then

```
float distPerStep = //whatever this number is...how far each step moves the player forward
float ballRadius =//radius
float theta = disPerStep/ ballRadius
ball.rotate(ball.transform.right, theta); //this should spin the ball so that the player is down again.
```

I think this will work, but there's probably a way to do it that only has to do one Rotate call.

### Your answer

### 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

### Related Questions

Simulating the graphics of a rolling 3D ball in a 2D game 2 Answers

Ball jump on collision problem 1 Answer

Strange sphere/physics rolling behaviour when scaled 1 Answer

Ball Doesn't Roll Down Hill - Physics Problem 3 Answers

How do I calculate the arc of a throw, given two positions and an angle of attack? 0 Answers