1

I am making a game in jMonkeyEngine where 2 characters fight together. I want that program to fetch information about collisions with simple body parts. Example, if I give punch for a character the program has knowledge about the body part. I know that jMonkey can give me information about skeleton, but collisions are between geometries. My idea is to create a group of the objects as a character and get geometry in jME. Is it a good idea? I create objects in Blender.

Eeji
  • 1,648
  • 3
  • 17
  • 22

1 Answers1

0

You can try approximate your shapes with a geometry that is simpler and use that for collisions. For a character it works for me to use a cylinder and the helper class 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);
        }
    }
}    

You can use a collisionshape

private CylinderCollisionShape shape;

And use jme3's helper classes to get the collision data

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());
        }
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424