Exemplo n.º 1
0
 public void endNoChildren() {
   onEnd();
   parent(null);
   valid = false;
   if (uniqueModel != null) uniqueModel.dispose();
   scene.remove(this);
   for (GameObject g : touchingObjects) g.activate();
 }
Exemplo n.º 2
0
 public void parent(GameObject p) {
   parent(p, true);
 }
Exemplo n.º 3
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);
      }
    }
  }