I want to collide two sphere objects. How do I detect a collision?
The collideWith method doesn’t work with multiple mesh shapes. I’m not sure how a listener would be added in the physics space realm, and processed. Sorry, but I’ve been around and around with the Wiki, and Google. I’m stuck in a big way.
One comment, I’m taking a class and the ambiguous problem statement talks about collision detection, but I’ve been through the notes, and the ‘method’ is not specified, so I assumed it was direct. But after a significant amount of head butting, I’m surprised that there doesn’t seem to be a direct implementation, but there are some hints about ‘bullet’ physics. Once more into the breach…
I couldn’t find a way to get the dimension of the sphere from the object, so I’m approximating with
a.getWorldBound().intersects(b.getWorldBound()) - reports collisions with gaps between the balls, but the collision physics makes them bounce without contact as well.
Well, it wasn’t stated in the original problem where your spheres were coming from… I assumed you might have created them at some time and knew the radius.
Otherwise, if it’s just a mesh then the bounding shape is likely a box… so bounding shape → bounding shape collision will be box to box.
For a game, it is better to separate your game objects from that which visualizes them. In that case, you’d have some kind of Ball object of your own design and it would know its position and radius. JME would just be rendering a sphere at the ball’s location every frame. (Essentially this is what bullet physics would be doing only it would be doing a lot of other stuff too.)
“Collision detection” on its own is a pretty huge problem (I have a whole book written on just that subject, for example) but sphere to sphere collision is the single easiest type of collision. I can only guess that it’s why your class assignment picked it. if( distance between centers ) < radius1 + radius2 then you know they intersect and the calculation of ‘where’ is even trivial.
For the purposes of my class I used WorldBound – close enough. The thing was, I created multiple spheres in a box and gave them random sizes. I really didn’t want to track the sizes – that was what the game physics was supposed to be for, why rewrite the collision detection functions. But I’m not yet at the level of manipulating collision volumes adroitly, I assumed it was ‘solved’, even for basic spatials.
Well, collideWith wasn’t meant for that. If you want something to do all of the physics for you then there is a physics engine… with its own objects (internally) and its own collision detection, etc… Though for spheres all it’s doing is a radius check.
Sometimes it’s tough to give people advice. We think they want to learn how to write proper games or something but they are really just trying to hack together some quick-and-dirty thing for a class.