Author
Topic: Collision detection (Read 4130 times)

I've been trying out jPCT today, and I can't figure out collision detection.

I started with the hello world for Android example.

I added another shape then moved one shape to collide with the other, and listened for collision. At least I think so. I haven't seen any examples or tutorials to do with collision (Can anyone point me to something?). And I'm just picking out methods from the javaDocs that look like they'll do the trick.

It seems to me that the collision modes are reversed in your example code. If the sphere moves towards the cube, the cube should be ....OTHERS and the sphere should be ...SELF. You can also combine both modes by using the | operator.

Anyway, translate() itself doesn't trigger any collision checks. It simply translates. What you actually need is either checkForCollision... in Object3D or checkCollision...in World. All (except for the ray-polygon approach, which i don't cover here) will return a corrected move vector in case of a collision, but they don't do any translation. It's up to you to use this vector to translate an object or destroy it or whatever.Also make sure that World.collisionOffset isn't set too low for your geometry.

One last word about the CollisionEvent: If you can avoid to use it on Android, then do it. It's creates event objects and the more object creations you can avoid, the better. There's a method to query an object if it was part of a collision. That's not very OOish nor very elegant, but faster on Android.

One more question. I see I can check if the cube was the target of the collision, using cube.wasTargetOfLastCollision(); How do I check if the sphere has collided with something/anything ? Without checking every potential target? Or is this not something that can be polled? An acceptable use of CollisionEvent perhaps?

I would say that an object collided with something if the returned translation vector doesn't equal the initial one. That may not be 100% true, but it should cover 99.99% of all cases. Or just go with the CollisionEvent. If it proves to be a bottleneck because of too much garbage being produced, we can still look for an improved solution.