| Heli Merc: why Shiva3D's objects can't be dynamic colliders |
|
| Tuesday, 04 January 2011 18:14 |
|
Shiva3D provides two methods by which an object to detect collisions, colliders and sensors. Setting an object as a collider basically makes the mesh sensitive to collisions, giving what can be considered pixel perfect collision detection. This setting also allows Shiva's dynamics controller to detect collisions and react accordingly, which is the basis for providing a gravity simulation. Sensors on the other hand are very course, providing collision detection within a rectangular or spherical shape, which is positioned so that it overlaps the object. Because of the shape limitation, unless the object is similar, the collision area isn't going to be perfect. Multiple sensors can be used to cover as much of the volume of the object as possible. The all relates to the method I'm currently using detect collisions between weapons and enemies. During each frame, a ray is calculated for each active bullet that projects the movement of the bullet during the next frame. This ray is then fired into the scene using scene.getFirstHitCollider(), which returns the first object that the ray hits, if any. Obviously, since I was successfully using this method to detect bullet to enemy collisions, it'd work for bullet to player collisions? Bad move. Setting the helicopter set as a collider and assigning it a dynamics controller caused it to go berserk, gyrating all over the screen as though it was hitting dozens of invisible objects. Calling dynamics.getLastCollisionContactCount() returned 16, which was impossible as the helicopter wasn't actually touching anything. Or was it? After thinking about it for a bit, I realised what was going on. Basically, setting an object to be a collider in Shiva must create what is effectively an invisible object that overlaps the position of the real one. Since dynamics are sensitive to colliders, I can only assume that the dynamics engine was detecting collisions with the invisible sensor object. An object that has a dynamics controller, can't simultaneously be a collider. And no, you can't cheat by overlaying an invisible helicopter on top of the real one because the dynamics controller will still detect it. The solutionBecause I'm using ray to object collision detection for weapons (rather than object to object collisions), the simplest solution was to create a clone of the helicopter's object and make it a collider, but disable dynamics. Then, at the start of each frame, the helicopter clone is moved so that it matches the rotation and position as the real one, except, offset by 50 units on the Y axis so it's off the top of the screen, making it both invisible to the camera and nowhere near the real helicopter which would cause problems with the dynamics controller. During each frame where rays are fired into the scene for all the active bullets, a second ray is fired for each bullet, offset by 50 on the Y axis, to detect bullet collision with the player's helicopter. This neatly bypasses the limitation with colliders and dynamics while still maintaining the physics simulation.
To illustrate the point, the above image shows a blue wire-frame helicopter that is the invisible version of the player, used for collision detection. Normally, this clone is off the top of the screen, I've just moved it down so it's in the shot. Oh, and the vehicles are pink because that's how Shiva identifies objects that are colliders when the game is viewed in anything less than runtime mode. blog comments powered by Disqus |

