Collisions and FlyByCamera

Hello all,

I am trying to do basic collision detection with a FlyCam and trying to not go through walls or objects.

Here https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:collision_and_intersection it says

Non-physical collision detection is interesting because it uses less computing resources than physical collision detection. The non-physical calculations are faster because they do not have any side effects such as pushing other objects or bumping off of them. Tasks such as mouse picking are easily implemented using mathematical techniques such as ray casting and intersections. Experienced developers optimize their games by finding ways to simulate certain (otherwise expensive physical) interactions in a non-physical way.

So I want to use as little “Bullet Physics” as possible.

Which brings me to the FlyCam

In the Hello Collision https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_collision

It shows how to do a lot of things using the physics engine, but also mentions this.

The Physics-Controlled Player

A first-person player is typically invisible. When you use the default flyCam as first-person cam, it does not even test for collisons and runs through walls. This is because the flyCam control does not have any physical shape assigned. In this code sample, you represent the first-person player as an (invisible) physical shape. You use the WASD keys to steer this physical shape around, while the physics engine manages for you how it walks along solid walls and on solid floors and jumps over solid obstacles. Then you simply make the camera follow the walking shape’s location – and you get the illusion of being a physical body in a solid environment seeing through the camera.

So let’s set up collision detection for the first-person player.

CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);

Again, you create a CollisionShape: This time you choose a CapsuleCollisionShape, a cylinder with a rounded top and bottom. This shape is optimal for a person: It’s tall and the roundness helps to get stuck less often on obstacles.

Supply the CapsuleCollisionShape constructor with the desired radius and height of the bounding capsule to fit the shape of your character. In this example the character is 2*1.5f units wide, and 6f units tall.
The final integer argument specifies the orientation of the cylinder: 1 is the Y-axis, which fits an upright person. For animals which are longer than high you would use 0 or 2 (depending on how it is rotated).
player = new CharacterControl(capsuleShape, 0.05f);

Now you use the CollisionShape to create a CharacterControl that represents the first-person player. The last argument of the CharacterControl constructor (here .05f) is the size of a step that the character should be able to surmount.

Now CharacterControl is deprecated and BetterCharControl doesn’t use a “CollisionShape” as an argument, so I’m curious how we still use this.

Also is there a way to not use the physics and use the “non-physical” collision with the collisionshape? It seems the collision shape is important for the FlyCam, or is there a better way to use the flycam for collisions?

I also am curious if it’s possible to attach a light with this method as well. I have a second viewport/cam which is a down view of my world and I want to use the light as an indicator on that viewport, but since I would be first person in the regular cam I wouldn’t see it.

Thanks for the help.

For faster collision detection, you can use CollisionResults. You can use a box geometry as a non-physical collision shape, and use collideWith() to get the results, calculate the collision force by using the surface normals of the triangles that got collided, and apply it to your cam model by using your own simple physics system (a basic physics object have linearAcceleration, linearVelocity, angularAcceleration, angularVelocity, and mass). Remember that you can only collide spatials with bounding volumes or rays.

If you want to use bullet physics, and are still “curious” of how to use collision shapes, take a lookt at it : https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics