Пример #1
0
  void updateServerElements() {
    for (RigidBody rb : rigidBodies) {
      ServerElement element = rigidBodyToServerElement.get(rb);
      if (rb != null && rb.getMotionState() != null) {
        rb.getMotionState().getWorldTransform(tmpTrans);
        element.setTransform(tmpTrans);
      }
      if (!rb.isActive()) {
        rb.activate();
      }
      // log.debug(" "+element.getPosition());
    }

    for (Integer id : serverGameArea.getGameState().getDynamicElements().keySet()) {
      ServerElement elem = serverGameArea.getGameState().getDynamicElements().get(id);
      elem.getCollidees().clear();
    }

    Integer nbManifold = dynamicsWorld.getDispatcher().getNumManifolds();
    for (Integer i = 0; i < nbManifold; i++) {
      PersistentManifold pm = dynamicsWorld.getDispatcher().getManifoldByIndexInternal(i);
      RigidBody rb0 = (RigidBody) pm.getBody0();
      RigidBody rb1 = (RigidBody) pm.getBody1();

      ServerElement elem0 = rigidBodyToServerElement.get(rb0);
      ServerElement elem1 = rigidBodyToServerElement.get(rb1);

      if ((elem0 instanceof ServerBullet) || (elem1 instanceof ServerBullet)) {
        log.info("coll " + elem0 + " " + elem1);
      }

      elem0.addCollidee(elem1);
      elem1.addCollidee(elem0);
    }
  }
Пример #2
0
  public void transform(Matrix4f mat, boolean updateLocal) {
    activate();

    Transform t = new Transform();
    t.set(mat);

    Vector3f v = new Vector3f();
    for (int i = 0; i < 3; ++i) {
      t.basis.getColumn(i, v);
      v.normalize();
      t.basis.setColumn(i, v);
    }

    body.setWorldTransform(t);

    // required for static objects:
    body.getMotionState().setWorldTransform(t);
    if (body.isInWorld() && body.isStaticOrKinematicObject()) {
      scene.world.updateSingleAabb(body);
      for (GameObject g : touchingObjects) g.activate();
    }
    //

    updateChildTransforms();

    if (parent != null && updateLocal) {
      updateLocalTransform();
    }
  }
 public void update(float deltaTime) {
   rigidBody.getMotionState().getWorldTransform(trans);
   this.getPosition().set(trans.origin.x, trans.origin.y, trans.origin.z);
   //		Quat4f rotate = trans.getRotation(new Quat4f());
   //		float[] rotation = new float[] { 0, 0, 0, 0 };
   //		rotation = GPTransLation.translateQuatToAXYZ(rotate);
   //		this.setRotation(rotation);
 }
Пример #4
0
  private void updateRigidBodys() {
    for (RigidBody rb : rigidBodies) {
      ServerElement element = rigidBodyToServerElement.get(rb);
      if (rb != null && rb.getMotionState() != null) {
        if (element.isUpdatedByUser()) {

          /*rb.getMotionState().getWorldTransform(tmpTrans);
          tmpTrans.origin.x=element.getTransform().origin.x;
          tmpTrans.origin.y=element.getTransform().origin.y;
          tmpTrans.origin.z=element.getTransform().origin.z;
          */
          rb.getMotionState().setWorldTransform(element.getTransform());
          rb.setMotionState(rb.getMotionState());
          element.resetUpdatedByUser();
        }
      }
    }
  }
Пример #5
0
  public void drawSelf(float angle, float x, float y, float z) {
    // 制定使用某套shader程序
    MatrixState.pushMatrix();
    MySurfaceView.init = false;
    Transform trans = body.getMotionState().getWorldTransform(new Transform());
    MatrixState.translate(trans.origin.x, trans.origin.y, trans.origin.z);
    Quat4f ro = trans.getRotation(new Quat4f());
    if (ro.x != 0 || ro.y != 0 || ro.z != 0) {
      float[] fa = SYSUtil.fromSYStoAXYZ(ro);
      if (isNumber(fa[0] + "") && isNumber(fa[1] + "") && isNumber(fa[2] + "")) {

        MatrixState.rotate(fa[0], fa[1], fa[2], fa[3]);
      }
    }

    GLES20.glUseProgram(mProgram);
    MatrixState.rotate(angle, x, y, z);
    // 将最终变换矩阵传入shader程序
    GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
    // 将位置、旋转变换矩阵传入着色器程序
    GLES20.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.currMatrix, 0);
    // 将光源位置传入着色器程序
    GLES20.glUniform3fv(maLightLocationHandle, 1, MatrixState.lightPositionFBRed);
    // 将摄像机位置传入着色器程序
    GLES20.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB);

    // 为画笔指定顶点位置数据
    GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer);
    // 为画笔指定顶点法向量数据
    GLES20.glVertexAttribPointer(maNormalHandle, 4, GLES20.GL_FLOAT, false, 3 * 4, mNormalBuffer);
    // 允许顶点位置数据数组
    GLES20.glEnableVertexAttribArray(maPositionHandle);
    GLES20.glEnableVertexAttribArray(maNormalHandle);

    // 绘制三角形
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
    MatrixState.popMatrix();
  }
Пример #6
0
  public void replaceModel(String modelName, boolean updateVisual, boolean updatePhysics) {
    if (modelName.equals(modelInstance.model.meshParts.get(0).id)) return;

    Model model = null;
    JsonValue mOrigin = null;
    JsonValue mDimNoScale = null;
    for (Scene sce : Bdx.scenes) {
      if (sce.models.containsKey(modelName)) {
        model = sce.models.get(modelName);
        mOrigin = sce.json.get("origins").get(modelName);
        mDimNoScale = sce.json.get("dimensions").get(modelName);
        break;
      }
    }
    if (model == null) {
      throw new RuntimeException("No model found with name: '" + modelName + "'");
    }
    origin = mOrigin == null ? new Vector3f() : new Vector3f(mOrigin.asFloatArray());
    dimensionsNoScale =
        mDimNoScale == null ? new Vector3f(1, 1, 1) : new Vector3f(mDimNoScale.asFloatArray());
    Matrix4 trans = modelInstance.transform;

    if (updateVisual) {
      ModelInstance mi = new ModelInstance(model);
      mi.transform.set(trans);
      modelInstance = mi;
    }

    if (updatePhysics) {
      GameObject compParent =
          parent != null && parent.body.getCollisionShape().isCompound() ? parent : null;
      boolean isCompChild =
          compParent != null
              && !(currBodyType.equals("NO_COLLISION") || currBodyType.equals("SENSOR"));
      if (isCompChild) {
        parent(null);
      }

      Matrix4f transform = transform();
      Vector3f scale = scale();
      String boundsType = json.get("physics").get("bounds_type").asString();
      float margin = json.get("physics").get("margin").asFloat();
      boolean compound = json.get("physics").get("compound").asBoolean();
      body.setCollisionShape(Bullet.makeShape(model.meshes.first(), boundsType, margin, compound));

      if (boundsType.equals("CONVEX_HULL")) {
        Transform startTransform = new Transform();
        body.getMotionState().getWorldTransform(startTransform);
        Matrix4f originMatrix = new Matrix4f();
        originMatrix.set(origin);
        Transform centerOfMassTransform = new Transform();
        centerOfMassTransform.set(originMatrix);
        centerOfMassTransform.mul(startTransform);
        body.setCenterOfMassTransform(centerOfMassTransform);
      }

      transform(transform);
      scale(scale);

      if (body.isInWorld()) {
        scene.world.updateSingleAabb(body);
      } else { // update Aabb hack for when not in world
        scene.world.addRigidBody(body);
        scene.world.updateSingleAabb(body);
        scene.world.removeRigidBody(body);
      }

      if (isCompChild) {
        parent(compParent);
      }
    }
  }