Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (580)
Games in Android Showcase (162)
games submitted by our members
Games in WIP (632)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Bouncing Ball - Physics and Collision Detection  (Read 5270 times)
0 Members and 1 Guest are viewing this topic.
Offline submitter

Senior Newbie

« Posted 2007-03-26 19:54:46 »

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.
Offline t_larkworthy

Senior Devvie

Medals: 1
Projects: 1

Google App Engine Rocks!

« Reply #1 - Posted 2007-03-27 02:00:39 »

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  Undecided  Have a look at the game physics forum.

Runesketch: an Online CCG built on Google App Engine where players draw their cards and trade. Fight, draw or trade yourself to success.
Offline submitter

Senior Newbie

« Reply #2 - Posted 2007-03-27 14:25:55 »

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 Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline submitter

Senior Newbie

« Reply #3 - Posted 2007-03-27 19:20:24 »

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.
Offline gideon

Senior Newbie

« Reply #4 - Posted 2007-03-27 20:12:20 »

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.

The towship's adventures
Offline submitter

Senior Newbie

« Reply #5 - Posted 2007-03-27 20:49:01 »

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 Undecided)

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:

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 Smiley
Offline submitter

Senior Newbie

« Reply #6 - Posted 2007-03-27 22:47:58 »

Finally got it working  Grin (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...

Offline tom
« Reply #7 - Posted 2007-03-28 18:12:14 »

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)

Offline Conzar

Junior Devvie

There is nothing common about common sense

« Reply #8 - Posted 2007-04-11 16:39:10 »

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

Also, this site has other great tutorials to get you up and going in java 3d.

Offline submitter

Senior Newbie

« Reply #9 - Posted 2007-04-25 17:19:13 »

Thanks for the help guys, muchly appreciated. Collisions are now working very well Grin
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline keldon85

Senior Devvie

Medals: 1

« Reply #10 - Posted 2007-05-31 07:32:08 »

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.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Waterwolf (21 views)
2015-05-20 15:01:45

chrislo27 (25 views)
2015-05-20 03:42:21

BurntPizza (59 views)
2015-05-10 15:53:18

FrozenShade (45 views)
2015-05-07 09:11:21

TheLopais (208 views)
2015-05-06 13:36:48

TheLopais (192 views)
2015-05-06 13:35:14

TheLopais (198 views)
2015-05-06 13:33:39

TheLopais (214 views)
2015-05-06 13:32:48

TheLopais (214 views)
2015-05-06 13:31:28

ClaasJG (236 views)
2015-04-30 20:33:25
List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!