The unstoppable bouncing ball

Hey all, welcome to a brand new week!

Here’s a challenge for the physics-savvy of you around: I’d like to have a ball bounce on walls, without ever stopping, while keeping a constant speed (think about the “bouncing ball of mass destruction” in Men in Black).

Sounds awful simple… hell, this kind of bouncing ball/box is the often the first tutorial in a 2D engine, and we’ve all seen it a hundred times on screen-savers etc.
But I want it 3D, and I want it with physics, for various reasons, like: what if my spherical ball now becomes a rugby ball? What if the objects the ball collides with are moving and I want them to apply a lifting effect? (not sure of the term, I’m a very poor tennis player.)

[Really, the above says it all about what I need, but I’ll elaborate on what I’ve tried for those interested.]

Anyway I’ve been struggling with the physics engine for weeks now and I just can’t get it right.

I started with a sphere+RigidBody+ConstantForce, with a relative forward force, but since the ball’s constantly spinning the rebounds are just very random, sometimes the ball’s trajectory is straight, sometimes it’s curvy and it will hit the same wall repeatedly… not really bouncy.
So I tried to freeze its rotation, but that’s bad, it kinda breaks the whole physics thing and the engine acts weird.
Another reason this wasn’t satisfactory is that I had little control on the ball’s speed (remember, I want it constant), and it was decelerating/accelerating on each rebound.

So I took out the ConstantForce to control the ball myself, setting its velocity and using Vector3.Reflect on collisions to make it bounce.
It kinda did the trick, but since then I’ve learnt that “setting velocity on a non-kinematic rigidbody is baaad”.
Plus I wasn’t really using the physics engine for anything else that collision-detection => try using Vector3.Reflect on a rugby ball, it’s disappointing.

I also tried to play around with physics materials, but let’s be honest, I just don’t understand what I’m doing and trying different combinations is just endless and leads me nowhere.

The funny thing is, a lot of “ball physics” threads have appeared on the forum these days, but I’ve read them all and while it helped understanding more about the physics engine, it didn’t solve my problem.
At least I came to realize that I had wrong expectations about the physics engine, and that my unstoppable bouncing ball kinda breaks the whole “realistic physics” idea.
For example, it’s not physically possible to have a 100% constant speed, the ball’s gotta have an acceleration of some sort. But if I control it wisely, I can make it so that this acceleration is super-fast and the speed maxes out (right?), so that the user won’t see the difference.

Well, here I am. I usually prefer asking precise technical questions instead of this “please make my game for me” question, but I’m just lost and need an expert opinion! Without proper, easy-to-use rebounds, my game just goes down the drain.

Well, you’ve touched on the paradox, now you need to really define what you truly want to see in your results.

How should a constant speed rugby ball bounce? If it is constant speed I don’t understand what effect you want to happen to clue you into it being a different material. :-/

Maybe what you want is the shape to effect what direction it bounces?

Instead of using a constant force, would you not just give it an initial velocity and let the physics engine take care of the rest? You’d set drag and angular drag to 0, of course, so it wouldn’t slow down, and use a bounciness of 1 on physics materials so it bounces without any loss of energy.

Seems to me the only thing you’d have to script is the “lift” effect for hitting a moving object, since this isn’t part of the physics engine.

–Eric

Precisely, that’s all I need.
The ball doesn’t react to gravity (really… unstoppable), so that’s the only difference I expect from a rugby ball.

Ok, I’ll have to try this. Didn’t know about the initial velocity+no drag.
That’s precisely what I needed, to be set on the right track, because trying all these drag/bounce/friction setting is too confusing!
Although I seem to remember I did try a bounciness of 1 and the ball just bounced at square angles all the time instead of “mirror” angles.

Really, the physics engine doesn’t do that? So if a car hits a wall or a running train, its trajectory will be the same?

I have to go back home now, will try this tomorrow, but if anyone has some more input don’t hesitate!!
Physics still puzzle me sometimes… :stuck_out_tongue:

I can imagine. :slight_smile: (Especially since constant force is meant to accelerate objects, since force is applied every physics frame.)

I’m not sure…looks right to me…

Correct. The physics engine only looks at where objects are every physics frame; it doesn’t take velocity into account.

–Eric

Eric: you’re the man!
My unstoppable bouncing ball works flawlessly, I spent the whole day yesterday trying a billion things, added a trail and particles. Everything… just works!
Correct settings are: no drag, physics material on the ball with bounce to 1 and maximum combine, friction to 0 and minimum combine (although I don’t know if the friction part is necessary, I only tried this setting).

So of course I tried a rugby ball with a mesh collider (I can even make it convex so it’ll still be able to collide with any weird object I can come up with), and it’s just brilliant!

The only weird thing is, it looses velocity on certain collisions, so after a while it’s just damn slow. I checked my settings, they’re just the same as the round ball, no drag, no gravity, no nothing that’s supposed to make it slower.
I fixed it by a simple script that maintains the magnitude (but doesn’t influence the direction so the rebounds feel as good), but I’m just wondering: why would that happen?
When I launch the 2 balls side-by-side with no object to stop them, the rugby ball follows without ever slowing.

Anyway… thanks so much for the help so far :stuck_out_tongue:

That’s a shame! I may not understand the implications, but it seems a weird limitation. Is the full Ageia SDK incapable of it, or is it just the Unity subset?

Anyway that’s no big deal, I guess I can script it rather easily by applying a side force relative to the velocity. But then won’t my ball have a curved trajectory?

That’s something I’ve been wondering for a while, if I AddForce to an object with no drag, how can I later on remove said force? Maybe AddRelativeForce(+force), then AddRelativeForce(-force)? I’m not too sure I understand what AddRelativeForce does, the results always puzzled me; but as I said, since I often don’t know what I’m doing, it’s really hard to grasp the action-reaction cycles.

Hmm, good question! Your solution seems reasonable though. I can’t claim to know anything about the inner workings of the physics engine…I imagine that, as with most things, there’s a trade-off of functionality vs. performance.

As for AddRelativeForce, what it does is add a force using the object’s axes. As opposed to AddForce, which adds a force using the world axes. So if the object’s axes are aligned with the world’s axes, the result would be identical.

–Eric

In my similar situation, bounce energy was lost due to friction whenever the ball was touching TWO surfaces at once, like in a corner. I didn’t have the problem when I used no friction. But I wanted the ball to spin accurately, and with no friction it could not do that.

I ended up with something similar to what you did, a hybrid of “real” physics and scripted action that achieved the desired game play.

Hmm, not sure if that only happens in those situations, I’ll have to check more thoroughly.

My playground for testing is a big hollow cube made up of 9 cubes, and I never saw this deceleration happen with the round ball, even though I once intentionnally shot in into corners: 3 hits at once.
So I was wondering if it had something to do with the mesh collider behaving different than the sphere collider.

I set friction to 0 and this happens. Not sure what friction’s for, why do you say the spinning isn’t accurate?
I don’t care if my round ball spins or not (it has no visual impact), but my rugby ball does spin crazily -which is super cool :).

Anyway, thanks for the input!

Friction is what causes a bouncing ball’s spin to change with each bounce. (My ball was marked, so it mattered.)

As for inaccurate spinning… without friction there was NO spinning :o (At least not generated via physics.)

Ok, cool. Does it also influence its velocity / direction or only the spinning?

It can affect just about anything. Think of this simple case: a ball rolling along the floor until it hits a wall.

Friction doesn’t just make the ball’s spin stop and change direction as the ball bounced back. It also affects the path the ball takes: friction against the wall makes the ball lift up of the floor a bit, “wasting” energy in an upward direction. (And maybe even making the ball catch some air on the way back.)

So any time a spinning ball hits a wall, the spin can pull the ball in a new direction, like the tires of a car on the road. And that takes energy away from the bounce the ball would have had otherwise.

Ok, makes sense.
I guess in my case zero friction is the way to go: if I want the ball to be truly unstoppable, I should just “not care” about all this and only rebound endlessly.

Although I’m aware to resurrect an old thread, but I found a solution to the bouncing ball problem described here: http://forum.unity3d.com/viewtopic.php?t=27380