Bouncing Ball - Physics and Collision Detection

Hey, I’m pretty new to Java3D and what I want to do is have the ability for a player in my 3D FPS game be able to throw a ball at a given angle and given speed and then for the ball to bounce realistically off of the ground and the walls and eventually come to a stop.

I have no idea where to begin with this but I know I’m going to need some physics and collision detection in there.

Does anyone have any resources I could refer to for help with carrying out this task? Perhaps you know someone who has done it before with a guide on a website somewhere? I’ve tried googling but havn’t really found much useful.

Many thanks.

You could try JOODE, it does all that kinda stuff for you. Docs a bit flimsy but it works the same as ODE (which has better documentation), plenty of test cases though in the repository. I could do with a user to force me to write some tutorials :-\ Have a look at the game physics forum.

OK I think this might be more complicated than I initially thought, so I’ve decided to start with a simple ball bouncing up and down in the center of the scene.

Putting the physics to one side for a moment, what would be the best way to animate such a bouncing ball? The first method that comes to mind is to simply calculate the ball’s position every 5ms (say) based on the principles of physics in such a scenario (in this case mainly gravity), and every 5ms draw the ball in its new position. I don’t really know much about animation in Java3D at the moment so would this be an OK method? If not, what better methods are there?

Thanks :slight_smile:

After reading the Sun Java3D Tutorials (and kind of understanding) I think I have to use behaviors? Do I need to create my own behavior or can I use PositionInterpolator?

There doesn’t seem to be much documentation about this anywhere on the Internet. I’ve not had any luck in finding much anyway.

The ‘animation’ of a moving ball is usually (if not always) done by calcuting the ball’s new position every frame and drawing it at the appropriate position. I don’t know anything about Java3D, but I’m pretty sure you wouldn’t need anything special like a PositionInterpolator that you mentioned.

Judging from your questions, you seem quite new to game programming in general (please forgive me if I’m wrong). Might I suggest to start with simple 2D games first? Implementing a small packman game would teach you most of the basics you’d also need for 3D and can be done in less than a week’s time. Any 3d shooter would certainly cost you months or more likely years to finish, depending on how much you start with.

You’d be suprised how incredibly complicated physics-systems can be, something like ODE (or the java port/rewrite JOODE) cost a number of people a few years to write. I’m not trying to discourage you though, but in this case it might be more educational and rewarding to start small.

Thanks for your reply gideon.

You’re right when you say I’m new to game programming! Unfortunately I don’t have the time to study 2D game programming as I’m doing this work as part of a project at university (with the deadline approaching quicker than I would like :slight_smile:

I understand the basic principle you have described for creating a simple falling ball but I can’t figure out how to animate this in Java3D. Please see Code for Episode 1 at the following link:

http://wiki.showmedo.com/index.php/PythonThompsonVPythonSeries

Here a falling ball has been implemented (quite simply and painlessly) using VPython.

Calculating the position of the ball every 5ms (say) is trivial; what I can’t figure out is how to display the ball in the scene every 5ms.

I am still researching and it seems I may have to use a custom made Behavior class using the WakeupOnTimeElapsed wakeup criteria. Can anyone confirm if this is what is needed?

Thanks for the help :slight_smile:

Finally got it working ;D (ball falling to the ground)

I had to create my own Behavior class; pretty simple once you know how, doh!

Now I can start to think about having the ball fall from the sky and bounce in 1 dimension (Y-axis) to a stop. This of course means thinking about collision detection.

I noticed one of the wakeup criteria is WakeupOnCollisionEntry - does anyone know if this can be used in this situation? So that the ball would fall and once it detected a collision with the ground the falling (downward) behavior would halt and the new upward behavior would begin…

Cheers

Use WakeupOnElapsedFrames(0) to run flat out and get a callback every frame. Use System.currentTimeMillis or System.nanoSomething to calculate the elapsed time between frames. Use this time with the physics. Ball position can be calculated as


newBallx = oldBallx + velocity.x * time
newBally = oldBally + velocity.y * time
newBally = oldBally + velocity.y * time

Don’t think you can use WakeupOnCollisionEntry since you want to detect collision before collision. You can use the picking package for collision detection. Although any real collision detection would be complicated for a newbie.

For a simple bouncing ball you could just check the bally and set velocity sign.


if ballx < 0
   ball.vely = -Math.abs(ball.vely)

Well, I wouldn’t say that collision detection is necessarily too complicated for a NOB depending on their mathmatical knowledge (for instance, 3 variable calculus is much more complex then collision detection). Anyways, here is a good site that goes through how collision detection and avoidance works in java 3d

http://java3d.j3d.org/tutorials/collision/index.html

Also, this site http://www.j3d.org has other great tutorials to get you up and going in java 3d.

Thanks for the help guys, muchly appreciated. Collisions are now working very well ;D

Oh gosh, with moving balls you use basic vector maths. You do not check every frame, it’s just not the “proper” way to do it … you basically rotate the line by the movement vector (using the UVN rotation system, then it’s all easy from there. Check if the point of collision is within the movement of the circle - this will check to see if the circle moved through the line.

Then transform your line into a fat line, the circles into points, and do a simple check to see whether the points are within the fat line; so pm me if I don’t provide you with a solution within 36 hours of this post.