【问题标题】:jMonkeyEngine - how to get a body part which was collidingjMonkeyEngine - 如何获得碰撞的身体部位
【发布时间】:2014-12-30 17:11:31
【问题描述】:

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

【问题讨论】:

    标签: character jmonkeyengine collision


    【解决方案1】:

    您可以尝试使用更简单的几何形状来近似您的形状,并将其用于碰撞。对于一个角色,我可以使用圆柱体和辅助类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);
            }
        }
    }    
    

    你可以使用碰撞形状

    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());
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 2023-04-07
      • 2018-07-28
      • 1970-01-01
      相关资源
      最近更新 更多