@Override
  public Element persist(Document doc, Element widgetNode, Widget widget) {
    CylinderWidget cylinder = (CylinderWidget) widget;
    RigidBody cylinderBody = cylinder.getRigidBody();

    widgetNode.appendChild(
        persistParam(
            doc, "location", v3fToString(cylinderBody.getCenterOfMassPosition(new Vector3f()))));
    widgetNode.appendChild(
        persistParam(
            doc,
            "orientation",
            v3fToString(
                RenderableBase.toAngles(
                    cylinderBody.getWorldTransform(new Transform()).getRotation(new Quat4f())))));
    widgetNode.appendChild(
        persistParam(
            doc, "linearVeloctiy", v3fToString(cylinderBody.getLinearVelocity(new Vector3f()))));
    widgetNode.appendChild(
        persistParam(
            doc, "angularVelocity", v3fToString(cylinderBody.getAngularVelocity(new Vector3f()))));
    widgetNode.appendChild(persistParam(doc, "radius", "" + cylinder.getRadius()));
    widgetNode.appendChild(persistParam(doc, "height", "" + cylinder.getHeight()));
    widgetNode.appendChild(persistParam(doc, "mass", "" + (1f / cylinderBody.getInvMass())));
    widgetNode.appendChild(persistParam(doc, "restitution", "" + cylinderBody.getRestitution()));
    widgetNode.appendChild(persistParam(doc, "friction", "" + cylinderBody.getFriction()));

    return widgetNode;
  }
Beispiel #2
0
  public void addObjectsToCollisionDomain() {
    Iterator<Entry<MTComponent, ArrayList<MTTriangleMesh>>> groupIter =
        collisionGroups.entrySet().iterator();

    int collidesWith = 0;
    while (groupIter.hasNext()) {
      collidesWith = ~groupId;

      Entry<MTComponent, ArrayList<MTTriangleMesh>> element = groupIter.next();

      // CollisionShape shape = createMeshShapeFromMTMeshTriangle(element.getValue());
      Iterator<MTTriangleMesh> iter = element.getValue().iterator();

      while (iter.hasNext()) {
        MTComponent comp = iter.next();
        CollisionShape shape = createMeshShapeFromMTMeshTriangle((MTTriangleMesh) comp);

        /*	Transform startTransform = new Transform();
        startTransform.setIdentity();

        Vector3f vec = new Vector3f();
        Vector3D translate = currentMesh.getCenterPointRelativeToParent();
        vec.x = translate.x;
        vec.y = translate.y;
        vec.z = translate.z;

        startTransform.origin.set(vec);*/

        Transform startTransform = new Transform();
        startTransform.setIdentity();
        Matrix mat = element.getKey().getGlobalMatrix();

        Vector3f vec = new Vector3f(0.0f, 0.0f, 0.0f);
        vec.x = mat.m03;
        vec.y = mat.m13;
        vec.z = mat.m23;

        // startTransform.transform(vec);
        // Matrix4f mat = CollisionManager.convertMT4JMatrixToMatrix4f(mesh.get);

        // startTransform.set(mat);
        // startTransform.origin.set(vec);
        // mat4f.m03 = mat4f.m03 + vec.x;
        // mat4f.m13 = mat4f.m13 + vec.y;
        // mat4f.m23 = mat4f.m23 + vec.z;
        // mat4f.setTranslation(vec);

        startTransform.origin.set(vec);

        Vector3f scale = new Vector3f(); // get scale value of global matrix

        Vector3D xVec = new Vector3D(mat.m00, mat.m01, mat.m02);
        Vector3D yVec = new Vector3D(mat.m10, mat.m11, mat.m12);
        Vector3D zVec = new Vector3D(mat.m20, mat.m21, mat.m22);

        scale.x = xVec.length();
        scale.y = yVec.length();
        scale.z = zVec.length();

        float[] scaleVals = new float[3];
        scale.get(scaleVals);

        for (int i = 0; i < 3; i++) // get rotation value by extracting scalation
        {

          try {
            float[] colvals = mat.getRow(i);

            for (int j = 0; j < 3; j++) {
              colvals[j] = colvals[j] / scaleVals[i];
            }
            startTransform.basis.setRow(i, colvals);
          } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }

        // startTransform.set(mat4f);

        float mass = 5f; // fake mass value only needed correctly if used with dynamic engine
        shape.setLocalScaling(scale);
        GImpactMeshShape sh = (GImpactMeshShape) shape;
        sh.updateBound();

        RigidBody body = localCreateRigidBody(mass, startTransform, shape);

        // get Center Of Mass of Triangle Mesh and write it to MTTriangleMesh
        Vector3f vecCOM = new Vector3f();
        body.getCenterOfMassPosition(vecCOM);
        Vector3D vecComMT4J = new Vector3D(vecCOM.x, vecCOM.y, vecCOM.z);
        // mesh.setCenterOfMass(vecComMT4J);
        // mesh.setMass(100.0f);
        // add Object to Collision World

        collisionWorld.addCollisionObject(body, groupId, (short) collidesWith);

        colObjectToComponent.put(body, comp);

        addCollisionObjectToGroup(
            comp.getParent(), body); // save association between collision objects and groups
      }
      groupId = (short) (groupId << 1); // shift groupId so every group has a unique bit value
    }
    SimulatePreDrawAction calcDynamics =
        new SimulatePreDrawAction(collisionWorld, this, scene.getCanvas());
    calcDynamics.setCurrentTimeStep(1.f / 1000000000000.0f);
    scene.registerPreDrawAction(calcDynamics);
  }