A-frame 将物理设置为变量

A-frame set physics to a variable

我正在使用 A 帧 (https://aframe.io) 创建场景,我想知道如何使用 A 帧物理组件将场景中的物理设置为可变。应该发生的是我的场景中的物理应该设置为我的变量 x 的值。如何做到这一点?

我的场景代码:https://jsfiddle.net/AidanYoung/1cmgLkrp/2/ 物理组件代码:https://github.com/n5ro/aframe-physics-system

现在我场景中的物理是使用 <a-scene> 标签设置的,当前设置为 -10。我想改变物理以等于变量 x。这是我设置物理的代码部分:

<a-scene background="color: lightblue;"
         renderer="antialias: true; gammaOutput: false;"
         light="defaultLightsEnabled: true;"

         physics="gravity: -10; 
         
         debug: false; restitution: 10;">

当前重力设置为-10。我想将重力设置为变量 x 的值。如何做到这一点?

您可以在物理驱动中设置重力

设置垂直重力

const scene = AFRAME.scenes[0] // or use document.querySelector('a-scene')
scene.systems.physics.driver.world.gravity.y = -50; // set vertical gravity

这里是完整版

您可以运行下面的代码片段,然后按space栏,跳跃阻力会更大。

<html>

<head>
  <script src="https://aframe.io/releases/0.8.2/aframe.min.js"></script>
  <script src="https://unpkg.com/aframe-extras@5.0.0/dist/aframe-extras.min.js"></script>
  <script src="https://unpkg.com/aframe-physics-system@3.3.0/dist/aframe-physics-system.min.js"></script>
  <script src="https://unpkg.com/aframe-animation-component@5.1.2/dist/aframe-animation-component.min.js"></script>
  <script src="https://unpkg.com/aframe-template-component@3.2.1/dist/aframe-template-component.min.js"></script>
  <script src="https://unpkg.com/aframe-gif-shader@0.2.0/dist/aframe-gif-shader.js"></script>
</head>

<body>
  <a-scene do-something background="color: lightblue;" renderer="antialias: true; gammaOutput: false;"
    light="defaultLightsEnabled: true;" physics="debug: false; restitution: 10;">
    <a-assets>
      <a-mixin id="box-bump"
        animation__bump="property: object3D.position.y; from: 0; to: 0.2; dur: 200; dir: alternate; loop: 1; startEvents: click, collide;">
      </a-mixin>
      <script id="pipe" type="text/html">
        <a-entity geometry="primitive: cylinder; segmentsHeight: 1;"
                  material="color: green; roughness: 0.2;"
                  position="0 0.5 0"
                  shadow="receive: true; cast: false;">
          <a-entity geometry="primitive: cylinder; radius: 1.1; height: 1; segmentsHeight: 1;"
                    material="color: green; roughness: 0.2;"
                    position="0 1 0"
                    shadow="cast: true; receive: false;"
                    static-body>
            <a-entity geometry="primitive: circle; radius: 1;"
                      material="color: #010; shader: flat;"
                      position="0 0.502 0"
                      rotation="-90 0 0"></a-entity>
          </a-entity>
        </a-entity>
      </script>
    </a-assets>

    <!-- CAMERA -->
    <a-entity id="rig" position="0 0 5" movement-controls="speed: 0.2;" kinematic-body="enableJumps: true;"
      jump-ability="distance: 3;" tracker>
      <a-entity camera="far: 1000;" wasd-controls="enabled: false;" look-controls="pointerLockEnabled: true;"
        position="0 1.6 0">

        <a-cursor fuse="false" raycaster="objects: [data-interactive]; far: 4;"></a-cursor>

      </a-entity>
    </a-entity>

    <!-- STAGE -->
    <a-entity id="stage">

      <a-image src="#title" npot="true" width="52.8" height="26.4" position="0 34 -50"></a-image>

      <a-entity position="0 3.5 -4">

        <a-box material="shader: standard; roughness: 1; npot: true; src: #brick;" position="-1 0 0" data-interactive
          sound="on: click; src: #brick-sound; poolSize: 10;" static-body></a-box>

      </a-entity>

      <a-box material="shader: standard; roughness: 1; npot: true; src: #brick;" position="1 0 0" data-interactive
        static-body></a-box>
    </a-entity>

    <!-- Pipes -->
    <a-entity template="src: #pipe" position="-5 0 -10"></a-entity>
    <a-entity template="src: #pipe" position="0 0 -10"></a-entity>
    <a-entity template="src: #pipe" position="5 0 -10"></a-entity>

    <a-entity geometry="primitive: box; width: 50; depth: 50; height: 1;"
      material="shader: standard; roughness: 1; src: #bedrock; repeat: 25 25; npot: true;" position="0 -0.5 0"
      static-body></a-entity>
    </a-entity>

  </a-scene>
  <script>
    const scene = AFRAME.scenes[0] // or use document.querySelector('a-scene')
    scene.systems.physics.driver.world.gravity.y = -50; // set vertical gravity
    console.log(scene.systems.physics.driver.world.gravity)
  </script>
</body>

</html>