# 相机，网格碰撞和重力

## 我怎样才能做到这一点 ？

1 - 定义并应用重力

``````scene.gravity = new BABYLON.Vector3(0, -9.81, 0);
``````

``````camera.applyGravity = true;
``````

2 - 定义椭圆体

babylon.js相机上的椭球属性默认为大小（0.5,1,0.5），但更改值会使您更高，更大，更小，更薄，这取决于调整后的轴。在下面的例子中，我们将使相机的椭球比默认的大一点：

``````//Set the ellipsoid around the camera (e.g. your player's size)
camera.ellipsoid = new BABYLON.Vector3(1, 1, 1);
```
```

finalPosition = position - vec3(0, ellipsoid.y, 0) + ellipsoidOffset

3 - 应用碰撞

``````// Enable Collisions
scene.collisionsEnabled = true;
camera.checkCollisions = true;
``````

``````ground.checkCollisions = true;
box.checkCollisions = true;
``````

4 - 对象与对象的碰撞

``````var speedCharacter = 8;
var gravity = 0.15;

character.ellipsoid = new BABYLON.Vector3(0.5, 1.0, 0.5);
character.ellipsoidOffset = new BABYLON.Vector3(0, 1.0, 0);

var forwards = new BABYLON.Vector3(parseFloat(Math.sin(character.rotation.y)) / speedCharacter, gravity, parseFloat(Math.cos(character.rotation.y)) / speedCharacter);
forwards.negate();
character.moveWithCollisions(forwards);
// or
var backwards = new BABYLON.Vector3(parseFloat(Math.sin(character.rotation.y)) / speedCharacter, -gravity, parseFloat(Math.cos(character.rotation.y)) / speedCharacter);
character.moveWithCollisions(backwards);
``````

## ArcRotateCamera

ArcRotateCamera还可以检查碰撞，但不是沿着障碍物滑动，当碰撞附加时，此相机不会移动。

``````camera.collisionRadius = new BABYLON.Vector3(0.5, 0.5, 0.5)
```
```