Search in sources :

Example 6 with CapsuleCollisionShape

use of com.jme3.bullet.collision.shapes.CapsuleCollisionShape in project jmonkeyengine by jMonkeyEngine.

the class TestPhysicsCharacter method simpleInitApp.

@Override
public void simpleInitApp() {
    // activate physics
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    // init a physical test scene
    PhysicsTestHelper.createPhysicsTestWorldSoccer(rootNode, assetManager, bulletAppState.getPhysicsSpace());
    setupKeys();
    // Add a physics character to the world
    physicsCharacter = new CharacterControl(new CapsuleCollisionShape(0.5f, 1.8f), .1f);
    physicsCharacter.setPhysicsLocation(new Vector3f(0, 1, 0));
    characterNode = new Node("character node");
    Spatial model = assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");
    model.scale(0.25f);
    characterNode.addControl(physicsCharacter);
    getPhysicsSpace().add(physicsCharacter);
    rootNode.attachChild(characterNode);
    characterNode.attachChild(model);
    // set forward camera node that follows the character
    camNode = new CameraNode("CamNode", cam);
    camNode.setControlDir(ControlDirection.SpatialToCamera);
    camNode.setLocalTranslation(new Vector3f(0, 1, -5));
    camNode.lookAt(model.getLocalTranslation(), Vector3f.UNIT_Y);
    characterNode.attachChild(camNode);
    //disable the default 1st-person flyCam (don't forget this!!)
    flyCam.setEnabled(false);
}
Also used : CharacterControl(com.jme3.bullet.control.CharacterControl) Spatial(com.jme3.scene.Spatial) BulletAppState(com.jme3.bullet.BulletAppState) Vector3f(com.jme3.math.Vector3f) Node(com.jme3.scene.Node) CameraNode(com.jme3.scene.CameraNode) CameraNode(com.jme3.scene.CameraNode) CapsuleCollisionShape(com.jme3.bullet.collision.shapes.CapsuleCollisionShape)

Example 7 with CapsuleCollisionShape

use of com.jme3.bullet.collision.shapes.CapsuleCollisionShape in project jmonkeyengine by jMonkeyEngine.

the class BetterCharacterControl method getShape.

/**
     * Gets a new collision shape based on the current scale parameter. The
     * created collisionshape is a capsule collision shape that is attached to a
     * compound collision shape with an offset to set the object center at the
     * bottom of the capsule.
     *
     * @return
     */
protected CollisionShape getShape() {
    //TODO: cleanup size mess..
    CapsuleCollisionShape capsuleCollisionShape = new CapsuleCollisionShape(getFinalRadius(), (getFinalHeight() - (2 * getFinalRadius())));
    CompoundCollisionShape compoundCollisionShape = new CompoundCollisionShape();
    Vector3f addLocation = new Vector3f(0, (getFinalHeight() / 2.0f), 0);
    compoundCollisionShape.addChildShape(capsuleCollisionShape, addLocation);
    return compoundCollisionShape;
}
Also used : CompoundCollisionShape(com.jme3.bullet.collision.shapes.CompoundCollisionShape) Vector3f(com.jme3.math.Vector3f) CapsuleCollisionShape(com.jme3.bullet.collision.shapes.CapsuleCollisionShape)

Example 8 with CapsuleCollisionShape

use of com.jme3.bullet.collision.shapes.CapsuleCollisionShape in project jmonkeyengine by jMonkeyEngine.

the class TestSweepTest method simpleUpdate.

@Override
public void simpleUpdate(float tpf) {
    float move = tpf * 1;
    boolean colliding = false;
    List<PhysicsSweepTestResult> sweepTest = bulletAppState.getPhysicsSpace().sweepTest(capsuleCollisionShape, new Transform(capsule.getWorldTranslation()), new Transform(capsule.getWorldTranslation().add(dist, 0, 0)));
    for (PhysicsSweepTestResult result : sweepTest) {
        if (result.getCollisionObject().getCollisionShape() != capsuleCollisionShape) {
            PhysicsCollisionObject collisionObject = result.getCollisionObject();
            fpsText.setText("Almost colliding with " + collisionObject.getUserObject().toString());
            colliding = true;
        }
    }
    if (!colliding) {
        // if the sweep is clear then move the spatial
        capsule.move(move, 0, 0);
    }
}
Also used : Transform(com.jme3.math.Transform) PhysicsSweepTestResult(com.jme3.bullet.collision.PhysicsSweepTestResult) PhysicsCollisionObject(com.jme3.bullet.collision.PhysicsCollisionObject)

Example 9 with CapsuleCollisionShape

use of com.jme3.bullet.collision.shapes.CapsuleCollisionShape in project jmonkeyengine by jMonkeyEngine.

the class TestWalkingChar method createCharacter.

private void createCharacter() {
    CapsuleCollisionShape capsule = new CapsuleCollisionShape(3f, 4f);
    character = new CharacterControl(capsule, 0.01f);
    model = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    //model.setLocalScale(0.5f);
    model.addControl(character);
    character.setPhysicsLocation(new Vector3f(-140, 40, -10));
    rootNode.attachChild(model);
    getPhysicsSpace().add(character);
}
Also used : CharacterControl(com.jme3.bullet.control.CharacterControl) Vector3f(com.jme3.math.Vector3f) CapsuleCollisionShape(com.jme3.bullet.collision.shapes.CapsuleCollisionShape)

Example 10 with CapsuleCollisionShape

use of com.jme3.bullet.collision.shapes.CapsuleCollisionShape in project jmonkeyengine by jMonkeyEngine.

the class HelloTerrainCollision method simpleInitApp.

@Override
public void simpleInitApp() {
    /** Set up Physics */
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    //bulletAppState.getPhysicsSpace().enableDebug(assetManager);
    flyCam.setMoveSpeed(100);
    setUpKeys();
    /** 1. Create terrain material and load four textures into it. */
    mat_terrain = new Material(assetManager, "Common/MatDefs/Terrain/Terrain.j3md");
    /** 1.1) Add ALPHA map (for red-blue-green coded splat textures) */
    mat_terrain.setTexture("Alpha", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
    /** 1.2) Add GRASS texture into the red layer (Tex1). */
    Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
    grass.setWrap(WrapMode.Repeat);
    mat_terrain.setTexture("Tex1", grass);
    mat_terrain.setFloat("Tex1Scale", 64f);
    /** 1.3) Add DIRT texture into the green layer (Tex2) */
    Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
    dirt.setWrap(WrapMode.Repeat);
    mat_terrain.setTexture("Tex2", dirt);
    mat_terrain.setFloat("Tex2Scale", 32f);
    /** 1.4) Add ROAD texture into the blue layer (Tex3) */
    Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
    rock.setWrap(WrapMode.Repeat);
    mat_terrain.setTexture("Tex3", rock);
    mat_terrain.setFloat("Tex3Scale", 128f);
    /** 2. Create the height map */
    AbstractHeightMap heightmap = null;
    Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
    heightmap = new ImageBasedHeightMap(heightMapImage.getImage());
    heightmap.load();
    /** 3. We have prepared material and heightmap. 
     * Now we create the actual terrain:
     * 3.1) Create a TerrainQuad and name it "my terrain".
     * 3.2) A good value for terrain tiles is 64x64 -- so we supply 64+1=65.
     * 3.3) We prepared a heightmap of size 512x512 -- so we supply 512+1=513.
     * 3.4) As LOD step scale we supply Vector3f(1,1,1).
     * 3.5) We supply the prepared heightmap itself.
     */
    terrain = new TerrainQuad("my terrain", 65, 513, heightmap.getHeightMap());
    /** 4. We give the terrain its material, position & scale it, and attach it. */
    terrain.setMaterial(mat_terrain);
    terrain.setLocalTranslation(0, -100, 0);
    terrain.setLocalScale(2f, 1f, 2f);
    rootNode.attachChild(terrain);
    /** 5. The LOD (level of detail) depends on were the camera is: */
    List<Camera> cameras = new ArrayList<Camera>();
    cameras.add(getCamera());
    TerrainLodControl control = new TerrainLodControl(terrain, cameras);
    terrain.addControl(control);
    /** 6. Add physics: */
    /* We set up collision detection for the scene by creating a static 
     * RigidBodyControl with mass zero.*/
    terrain.addControl(new RigidBodyControl(0));
    // We set up collision detection for the player by creating
    // a capsule collision shape and a CharacterControl.
    // The CharacterControl offers extra settings for
    // size, stepheight, jumping, falling, and gravity.
    // We also put the player in its starting position.
    CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
    player = new CharacterControl(capsuleShape, 0.05f);
    player.setJumpSpeed(20);
    player.setFallSpeed(30);
    player.setGravity(30);
    player.setPhysicsLocation(new Vector3f(-10, 10, 10));
    // We attach the scene and the player to the rootnode and the physics space,
    // to make them appear in the game world.
    bulletAppState.getPhysicsSpace().add(terrain);
    bulletAppState.getPhysicsSpace().add(player);
}
Also used : CharacterControl(com.jme3.bullet.control.CharacterControl) ArrayList(java.util.ArrayList) Material(com.jme3.material.Material) CapsuleCollisionShape(com.jme3.bullet.collision.shapes.CapsuleCollisionShape) Texture(com.jme3.texture.Texture) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl) ImageBasedHeightMap(com.jme3.terrain.heightmap.ImageBasedHeightMap) AbstractHeightMap(com.jme3.terrain.heightmap.AbstractHeightMap) BulletAppState(com.jme3.bullet.BulletAppState) Vector3f(com.jme3.math.Vector3f) TerrainLodControl(com.jme3.terrain.geomipmap.TerrainLodControl) Camera(com.jme3.renderer.Camera) TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad)

Aggregations

CapsuleCollisionShape (com.jme3.bullet.collision.shapes.CapsuleCollisionShape)11 Vector3f (com.jme3.math.Vector3f)9 CharacterControl (com.jme3.bullet.control.CharacterControl)8 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)8 BulletAppState (com.jme3.bullet.BulletAppState)7 ColorRGBA (com.jme3.math.ColorRGBA)5 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)5 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)5 ScreenshotAppState (com.jme3.app.state.ScreenshotAppState)4 HeightfieldCollisionShape (com.jme3.bullet.collision.shapes.HeightfieldCollisionShape)4 Material (com.jme3.material.Material)4 Node (com.jme3.scene.Node)4 TerrainGridListener (com.jme3.terrain.geomipmap.TerrainGridListener)4 TerrainGridLodControl (com.jme3.terrain.geomipmap.TerrainGridLodControl)4 DistanceLodCalculator (com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator)4 Texture (com.jme3.texture.Texture)4 File (java.io.File)4 TerrainGrid (com.jme3.terrain.geomipmap.TerrainGrid)3 DirectionalLight (com.jme3.light.DirectionalLight)2 PhysicsCollisionObject (com.jme3.bullet.collision.PhysicsCollisionObject)1