2d Physics

I’m working on a small game as a learning experience for jME (previously used xith3d) and had a question about collision detection. The game is top down 2d (think pong) but I’m using jME so I can later add neat effects (maybe with the partcle engine) Anyway, all collision detection will only have to be done in 2 dimensions. As of right now I see three ways to handle collision detection.


  1. Roll my own


  2. Use the jME built in physics


  3. use ODE



    I’d like to avoid coding it myself if at all possible so does the jME collision detection handle 2d collision detection fast or will it just do the same thing as with 3d collision detection. Also, does ode handle 2d collision fast or does it just do 3d collision detection. Of course I’m no expert but it seems to me that you can really simplify a lot if you only need to check for collisions in 2 dimensions and I don’t want to use jME or ODE to do that if there isn’t a way to have them ignore the third dimension.



    Thanks in advance for any advice!



    ~Shochu

ODE will handle collision detection and collision results (i.e. the physics of the ball bouncing off the paddle). jME collision will handle the collision just fine, but you’ll have to handle velocity changes and such.



You have enough tools that you are not going to need to roll your own, the question then remains, do you want to try to create a realistic (ODE) pong game, or a traditional pong game?

If you want to see a copy of pong in jme look at jmetest.sound.PongRevisited (The name may be incorrect but it is similar)

I realize this might not be the best place to ask but here it goes anyway … The game I’m working on is 3d (obviously since I’m using jME) but all movement is restricted to the xy plane for in game objects. Can ODE handle this restriction where you only want 2d collision detection etc? I’ve been looking through the ODE documentation and it seems like you can only hack this kind of behavior by creating “flat” ODE objects and making sure you only apply forces in 2 dimensions so the objects can never move in the z direction.

Sorry if this is way off but couldn’t you just crank up gravity so that any impulse in the z direction is counteracted? What collision would you have that would cause objects to fly upwards?

is there any solution to this problem with ode? (problem with ignoring the z-axis)

I have never used ODE with jME, but with Python. Though if ODE for Java is structured as ODE for Python, you can simply not pass "up axis" (in jME Y axis, if I remember well) translations from the results returned by ODE to jME, and handle only X and Z coordinates.

does somebody now if jme-physics 2 has this method to disable the y-axis?

You can simply lock an axis in the update method by manually setting the spatial's axis (for this you'd probably want to use Z if it's a 2D game).



So in your update method you can do something like:

spatial.getLocalTranslation().z = 0.0f;
spatialPhysics.updateFromSpatial();   // I don't remember what the method for this is or if it even exists in jME-Physics 2 as I am only familiar with the first version

darkfrog said:

spatial.getLocalTranslation().z = 0.0f;
spatialPhysics.updateFromSpatial();   // I don't remember what the method for this is or if it even exists in jME-Physics 2 as I am only familiar with the first version



No need to call any update method like before - so you can omit that updateFromSpatial() line.

A better method would be using a 2D joint in ODE. Someone already made such a thing for the developer ODE version. I'm unsure if it went into the library... but as long as it's not in the jME Physics 2 natives you should be fine with the approach darkfrog mentioned.

nice, thx.

i'll try that and report back :wink: