Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 防止摄像头移动';通过';帧对象_Html_Three.js_Aframe_Webvr - Fatal编程技术网

Html 防止摄像头移动';通过';帧对象

Html 防止摄像头移动';通过';帧对象,html,three.js,aframe,webvr,Html,Three.js,Aframe,Webvr,假设我有一个圆柱体模型,我将其加载到我的webvr场景中。如何使对象成为实体?也就是说,用户(摄像机视图)不能在对象内占据任何位置或“穿过”对象。我怎样才能做到这一点 <a-scene> <a-assets> <a-asset-item id="cube-obj" src="cube.obj"></a-asset-item> </a-assets> <a-entity id="cameraWrapper" po

假设我有一个圆柱体模型,我将其加载到我的webvr场景中。如何使对象成为实体?也就是说,用户(摄像机视图)不能在对象内占据任何位置或“穿过”对象。我怎样才能做到这一点

<a-scene>
  <a-assets>
    <a-asset-item id="cube-obj" src="cube.obj"></a-asset-item>
  </a-assets>
  <a-entity id="cameraWrapper" position="0 2 10" rotation="0 0 0">
    <a-camera near="0.1" user-height="0" id="camera" listener></a-camera>
  </a-entity>
  <a-entity obj-model="obj: #cube-obj; mtl: #cube-mtl" scale="1 1 1" rotation="-90 0 0"></a-entity>
  <a-plane position="0 4 4" rotation="-90 0 -90" width="4" height="4" color="#7BC8A4"></a-plane>
  <a-sky color="#ECECEC"></a-sky>
</a-scene>

这取决于您计划支持哪些设备,以及您如何允许用户支持这些设备 在场景中导航。对于大多数虚拟现实体验,请遵循最佳实践,仅限 根据用户的运动按比例移动相机。如果用户 在roomscale空间中向前移动,相机被“阻挡”,这是一个非常糟糕的问题 经验对于大多数VR应用程序,最好使用 , 设计场景以避开障碍物,或探索更具创意的场景 在世界各地移动用户的方式

用于使用gamepad或WASD控件的非VR桌面体验,或用于VR场景 如果摄像头位于车辆内部,则可以添加 到 防止通过障碍物移动


^我会尽快将其添加到A-Frame常见问题解答或文档中。这已被添加到。这里有一个,还有一个。

自从我来到这里寻找一个更简单的答案(不是处理自定义对象,而是标准的AFrame对象)以来,也许这不是您想要的。但是如果你只是要求“防止相机穿过一帧物体”,也许这会对你有所帮助

在下面的示例中,我使用管理物理。 我从中复制了名为kinema body(请参见嵌入脚本)的
运动学body
组件,因为它被标记为“已弃用”,因此将来可能会消失

您将看到如何在场景中移动,但当到达对象时,无法在其中移动。这在桌面和Oculus Go中都适用。诀窍很简单:相机包含在
kinema主体
装备中,对象(平面和长方体)是
静态主体
。当
kinema body
接触
static body
时,它会停止(或者尝试绕着它移动,具体取决于它的移动方式)


/**
*Kinema身体。
*
*基于运动学体,从帧附加(现在,基本上是一个副本,
*只是因为我读到它在一个框架中被弃用)
*
*   https://github.com/donmccurdy/aframe-extras/blob/master/src/misc/kinematic-body.js
*
*托管动态实体,可移动但不受
*物理引擎。这不是一个真正的运动学体,从我们的意义上说
*让物理引擎有选择地计算与它的碰撞
*将这些碰撞应用于对象。物理引擎无法决定
*元素的位置/速度/旋转。
*
*用于摄影机对象,因为完整的物理模拟将创建
*对玩家来说感觉不自然的动作。两足动物的运动不需要
*很好地转化为刚体物理。
*
*见:http://www.learn-cocos2d.com/2013/08/physics-engine-platformer-terrible-idea/
*以及:http://oxleygamedev.blogspot.com/2011/04/player-physics-part-2.html
*/
常数EPS=0.000001;
AFRAME.registerComponent('kinema-body'{
依赖项:['velocity'],
/*******************************************************************
*模式
*/
模式:{
质量:{默认值:5},
半径:{默认值:1.3},
linearDamping:{默认值:0.05},
enableSlopes:{default:true},
enableJumps:{default:false},
},
/*******************************************************************
*生命周期
*/
init:函数(){
this.system=this.el.sceneEl.systems.physics;
this.system.addComponent(this);
const el=this.el,
data=this.data,
position=(new CANNON.Vec3()).copy(el.object3D.getWorldPosition(new THREE.Vector3());
this.body=新的CANNON.body({
材质:this.system.getMaterial('staticMaterial'),
职位:职位,,
mass:data.mass,
linearDamping:data.linearDamping,
固定旋转:正确
});
这个。body。addShape(
新加农炮。球体(数据。半径),
新加农炮.Vec3(0,data.radius,0)
);
this.body.el=this.el;
this.el.body=this.body;
this.system.addBody(this.body);
if(el.hasAttribute('wasd-controls')){
控制台。警告(“[kinema body]与wasd控件不兼容,请使用移动控件”);
}
},
删除:函数(){
this.system.removeBody(this.body);
this.system.removeComponent(this);
删除此.el.正文;
},
/*******************************************************************
*更新
*/
/**
*检查CANNON.World是否存在碰撞并尝试将其应用于
*元素自动,以玩家友好的方式。
*
*对于水平表面,这里有额外的逻辑。基本要求:
*(1)仅在不接触任何水平面时施加重力。
*(2)移动时,将速度正好投射到一个地面上。
*如果与两个地面(例如地面+坡道)接触,选择
*与流速发生碰撞的那个,如果有的话。
*/
beforeStep:函数(t,dt){
如果(!dt)返回;
const el=this.el;
const data=this.data
const body=this.body;
如果(!data.enableJumps)body.velocity.set(0,0,0);
body.position.copy(el.getAttribute('position'));
},
步骤:(函数(){
常数速度=新的3.Vector3(),
normalizedVelocity=new THREE.Vector3(),
currentSurfaceNormal=new THREE.Vector3(),
groundNormal=新的3.Vector3();
返回函数(t,dt){
如果(!dt)返回;
让body=this.body,
data=this.data,
didclide=false,
高度,地面高度=-无穷大,
地体,
contacts=this.system.getContacts();
dt=Math.min(dt,this.system.data.maxInterval*1000);
groundNormal.set(0,0,0);
velocity.copy(this.el.getAttribute
<!DOCTYPE html>

<html>
  <head>
    <script src="//aframe.io/releases/0.8.2/aframe.min.js"></script>
    <script src="//cdn.rawgit.com/donmccurdy/aframe-extras/v5.0.0/dist/aframe-extras.min.js"></script>
    <script src="//cdn.rawgit.com/donmccurdy/aframe-physics-system/v3.3.0/dist/aframe-physics-system.min.js"></script>
    <script>
/**
 * Kinema body.
 *
 * Based on kinematic-body, from AFrame Extras (for now, basically a copy,
 *   just because I read it is deprecated in AFrame Extras)
 *
 *   https://github.com/donmccurdy/aframe-extras/blob/master/src/misc/kinematic-body.js
 *
 * Managed dynamic body, which moves but is not affected (directly) by the
 * physics engine. This is not a true kinematic body, in the sense that we are
 * letting the physics engine _compute_ collisions against it and selectively
 * applying those collisions to the object. The physics engine does not decide
 * the position/velocity/rotation of the element.
 *
 * Used for the camera object, because full physics simulation would create
 * movement that feels unnatural to the player. Bipedal movement does not
 * translate nicely to rigid body physics.
 *
 * See: http://www.learn-cocos2d.com/2013/08/physics-engine-platformer-terrible-idea/
 * And: http://oxleygamedev.blogspot.com/2011/04/player-physics-part-2.html
 */
const EPS = 0.000001;

AFRAME.registerComponent('kinema-body', {
  dependencies: ['velocity'],

  /*******************************************************************
   * Schema
   */

  schema: {
    mass:           { default: 5 },
    radius:         { default: 1.3 },
    linearDamping:  { default: 0.05 },
    enableSlopes:   { default: true },
    enableJumps:    { default: false },
  },

  /*******************************************************************
   * Lifecycle
   */

  init: function () {
    this.system = this.el.sceneEl.systems.physics;
    this.system.addComponent(this);

    const el = this.el,
        data = this.data,
        position = (new CANNON.Vec3()).copy(el.object3D.getWorldPosition(new THREE.Vector3()));

    this.body = new CANNON.Body({
      material: this.system.getMaterial('staticMaterial'),
      position: position,
      mass: data.mass,
      linearDamping: data.linearDamping,
      fixedRotation: true
    });
    this.body.addShape(
      new CANNON.Sphere(data.radius),
      new CANNON.Vec3(0, data.radius, 0)
    );

    this.body.el = this.el;
    this.el.body = this.body;
    this.system.addBody(this.body);

    if (el.hasAttribute('wasd-controls')) {
      console.warn('[kinema-body] Not compatible with wasd-controls, use movement-controls.');
    }
  },

  remove: function () {
    this.system.removeBody(this.body);
    this.system.removeComponent(this);
    delete this.el.body;
  },

  /*******************************************************************
   * Update
   */

  /**
   * Checks CANNON.World for collisions and attempts to apply them to the
   * element automatically, in a player-friendly way.
   *
   * There's extra logic for horizontal surfaces here. The basic requirements:
   * (1) Only apply gravity when not in contact with _any_ horizontal surface.
   * (2) When moving, project the velocity against exactly one ground surface.
   *     If in contact with two ground surfaces (e.g. ground + ramp), choose
   *     the one that collides with current velocity, if any.
   */
  beforeStep: function (t, dt) {
    if (!dt) return;

    const el = this.el;
    const data = this.data
    const body = this.body;

    if (!data.enableJumps) body.velocity.set(0, 0, 0);
    body.position.copy(el.getAttribute('position'));
  },

  step: (function () {
    const velocity = new THREE.Vector3(),
        normalizedVelocity = new THREE.Vector3(),
        currentSurfaceNormal = new THREE.Vector3(),
        groundNormal = new THREE.Vector3();

    return function (t, dt) {
      if (!dt) return;

      let body = this.body,
          data = this.data,
          didCollide = false,
          height, groundHeight = -Infinity,
          groundBody,
          contacts = this.system.getContacts();

      dt = Math.min(dt, this.system.data.maxInterval * 1000);

      groundNormal.set(0, 0, 0);
      velocity.copy(this.el.getAttribute('velocity'));
      body.velocity.copy(velocity);

      for (var i = 0, contact; contact = contacts[i]; i++) {
        // 1. Find any collisions involving this element. Get the contact
        // normal, and make sure it's oriented _out_ of the other object and
        // enabled (body.collisionReponse is true for both bodies)
        if (!contact.enabled) { continue; }
        if (body.id === contact.bi.id) {
          contact.ni.negate(currentSurfaceNormal);
        } else if (body.id === contact.bj.id) {
          currentSurfaceNormal.copy(contact.ni);
        } else {
          continue;
        }

        didCollide = body.velocity.dot(currentSurfaceNormal) < -EPS;
        if (didCollide && currentSurfaceNormal.y <= 0.5) {
          // 2. If current trajectory attempts to move _through_ another
          // object, project the velocity against the collision plane to
          // prevent passing through.
          velocity.projectOnPlane(currentSurfaceNormal);
        } else if (currentSurfaceNormal.y > 0.5) {
          // 3. If in contact with something roughly horizontal (+/- 45º) then
          // consider that the current ground. Only the highest qualifying
          // ground is retained.
          height = body.id === contact.bi.id
            ? Math.abs(contact.rj.y + contact.bj.position.y)
            : Math.abs(contact.ri.y + contact.bi.position.y);
          if (height > groundHeight) {
            groundHeight = height;
            groundNormal.copy(currentSurfaceNormal);
            groundBody = body.id === contact.bi.id ? contact.bj : contact.bi;
          }
        }
      }

      normalizedVelocity.copy(velocity).normalize();
      if (groundBody && (!data.enableJumps || normalizedVelocity.y < 0.5)) {
        if (!data.enableSlopes) {
          groundNormal.set(0, 1, 0);
        } else if (groundNormal.y < 1 - EPS) {
          groundNormal.copy(this.raycastToGround(groundBody, groundNormal));
        }

        // 4. Project trajectory onto the top-most ground object, unless
        // trajectory is > 45º.
        velocity.projectOnPlane(groundNormal);

      } else if (this.system.driver.world) {
        // 5. If not in contact with anything horizontal, apply world gravity.
        // TODO - Why is the 4x scalar necessary.
        // NOTE: Does not work if physics runs on a worker.
        velocity.add(this.system.driver.world.gravity.scale(dt * 4.0 / 1000));
      }

      body.velocity.copy(velocity);
      this.el.setAttribute('velocity', body.velocity);
      this.el.setAttribute('position', body.position);
    };
  }()),

  /**
   * When walking on complex surfaces (trimeshes, borders between two shapes),
   * the collision normals returned for the player sphere can be very
   * inconsistent. To address this, raycast straight down, find the collision
   * normal, and return whichever normal is more vertical.
   * @param  {CANNON.Body} groundBody
   * @param  {CANNON.Vec3} groundNormal
   * @return {CANNON.Vec3}
   */
  raycastToGround: function (groundBody, groundNormal) {
    let ray,
        hitNormal,
        vFrom = this.body.position,
        vTo = this.body.position.clone();

    ray = new CANNON.Ray(vFrom, vTo);
    ray._updateDirection(); // TODO - Report bug.
    ray.intersectBody(groundBody);

    if (!ray.hasHit) return groundNormal;

    // Compare ABS, in case we're projecting against the inside of the face.
    hitNormal = ray.result.hitNormalWorld;
    return Math.abs(hitNormal.y) > Math.abs(groundNormal.y) ? hitNormal : groundNormal;
  }
});
    </script>
  <body>
    <a-scene physics="debug: true">

      <a-box static-body position="0 0 0" height="3" width="4" color="red"></a-box>
      <a-plane static-body position="0 0 0" rotation="-90 0 0" width="8" height="14" color="#7BC8A4"></a-plane>
      <a-sky color="#ECECEC"></a-sky>

      <a-entity kinema-body="radius: 0.8" movement-controls="fly: false" position="0 0 4" look-controls>
        <a-entity camera position="0 1.6 0" ></a-entity>
      </a-entity>

    </a-scene>
  </body>
</html>