jMonkeyEngine - 如何获得碰撞的身体部位

时间:2014-12-30 17:11:31

标签: character jmonkeyengine collision

我正在jMonkeyEngine制作游戏,其中2个角色一起战斗。我希望该程序获取有关简单身体部位碰撞的信息。例如,如果我给角色打了一拳,程序就会知道身体部位。我知道jMonkey可以给我关于骨架的信息,但是几何之间的碰撞。我的想法是创建一组对象作为角色并在jME中获取几何。这是个好主意吗?我在Blender中创建对象。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用更简单的几何体来近似您的形状,并将其用于碰撞。对于一个角色,我可以使用圆柱体和辅助类BetterCharacterControl

private BetterCharacterControl characterControl;


@Override
public void simpleUpdate(float tpf) {

    characterControl.setGravity(planetAppState.getGravity());

    // Get current forward and left vectors of model by using its rotation
    // to rotate the unit vectors
    Vector3f modelForwardDir = characterNode.getWorldRotation().mult(Vector3f.UNIT_Z);
    Vector3f modelLeftDir = characterNode.getWorldRotation().mult(Vector3f.UNIT_X);

    // WalkDirection is global!
    // You *can* make your character fly with this.
    walkDirection.set(0, 0, 0);
    if (leftStrafe) {
        walkDirection.addLocal(modelLeftDir.mult(5));
    } else if (rightStrafe) {
        walkDirection.addLocal(modelLeftDir.negate().multLocal(5));
    }
    if (forward) {
        walkDirection.addLocal(modelForwardDir.mult(5));
    } else if (backward) {
        walkDirection.addLocal(modelForwardDir.negate().multLocal(5));
    }
    characterControl.setWalkDirection(walkDirection);

    // ViewDirection is local to characters physics system!
    // The final world rotation depends on the gravity and on the state of
    // setApplyPhysicsLocal()
    if (leftRotate) {
        Quaternion rotateL = new Quaternion().fromAngleAxis(FastMath.PI * tpf, Vector3f.UNIT_Y);
        rotateL.multLocal(viewDirection);
    } else if (rightRotate) {
        Quaternion rotateR = new Quaternion().fromAngleAxis(-FastMath.PI * tpf, Vector3f.UNIT_Y);
        rotateR.multLocal(viewDirection);
    }
    characterControl.setViewDirection(viewDirection);

    if (walkDirection.length() == 0) {
        if (!"Idle".equals(animationChannel.getAnimationName())) {
            animationChannel.setAnim("Idle", 1f);
        }
    } else {
        if (!"Walk".equals(animationChannel.getAnimationName())) {
            animationChannel.setAnim("Walk", 0.7f);
        }
    }
}    

您可以使用collisionshape

private CylinderCollisionShape shape;

使用jme3的帮助程序类来获取碰撞数据

CollisionResults results = new CollisionResults();
        // System.out.println("1 #Collisions between" + ufoNode.getName()
        // + " and " + jumpgateSpatial.getName() + ": " + results.size());
        ufoNode.collideWith((BoundingBox) jumpgateSpatial.getWorldBound(),
                results);
        // System.out.println("2 #Collisions between" + ufoNode.getName()
        // + " and " + jumpgateSpatial.getName() + ": " + results.size());
        CollisionResults results2 = new CollisionResults();
        // Use the results
        if (results.size() > 0 && playtime > 50000) {
            System.out.println("playtime" + playtime);
            System.out.println("#Collisions between" + ufoNode.getName()
                    + " and " + jumpgateSpatial.getName() + ": "
                    + results.size());
            // how to react when a collision was detected
            CollisionResult closest = results.getClosestCollision();
            System.out.println("What was hit? "
                    + closest.getGeometry().getName());
            System.out
                    .println("Where was it hit? " + closest.getContactPoint());
            System.out.println("Distance? " + closest.getDistance());
            ufoControl
                    .setPhysicsLocation(jumpGateControl2.getPhysicsLocation());
            System.out.println("Warped");
        } else {
            // how to react when no collision occured
        }
        if (results2.size() > 0) {
            System.out.println("Number of Collisions between"
                    + ufoNode.getName() + " and " + moon.getName() + ": "
                    + results2.size());
            // how to react when a collision was detected
            CollisionResult closest2 = results2.getClosestCollision();
            System.out.println("What was hit? "
                    + closest2.getGeometry().getName());
            System.out.println("Where was it hit? "
                    + closest2.getContactPoint());
            System.out.println("Distance? " + closest2.getDistance());
        }