Java Code Examples for com.jme3.scene.Geometry#addControl()

The following examples show how to use com.jme3.scene.Geometry#addControl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HelloPhysics.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** This method creates one individual physical cannon ball.
 * By defaul, the ball is accelerated and flies
 * from the camera position in the camera direction.*/
 public void makeCannonBall() {
  /** Create a cannon ball geometry and attach to scene graph. */
  Geometry ball_geo = new Geometry("cannon ball", sphere);
  ball_geo.setMaterial(stone_mat);
  rootNode.attachChild(ball_geo);
  /** Position the cannon ball  */
  ball_geo.setLocalTranslation(cam.getLocation());
  /** Make the ball physical with a mass > 0.0f */
  ball_phy = new RigidBodyControl(1f);
  /** Add physical ball to physics space. */
  ball_geo.addControl(ball_phy);
  bulletAppState.getPhysicsSpace().add(ball_phy);
  /** Accelerate the physical ball to shoot it. */
  ball_phy.setLinearVelocity(cam.getDirection().mult(25));
}
 
Example 2
Source File: TestAttachDriver.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void setupFloor() {
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key = new TextureKey("Interface/Logo/Monkey.jpg", true);
    key.setGenerateMips(true);
    Texture tex = assetManager.loadTexture(key);
    tex.setMinFilter(Texture.MinFilter.Trilinear);
    mat.setTexture("ColorMap", tex);

    Box floor = new Box(100, 1f, 100);
    Geometry floorGeom = new Geometry("Floor", floor);
    floorGeom.setMaterial(mat);
    floorGeom.setLocalTranslation(new Vector3f(0f, -3, 0f));

    floorGeom.addControl(new RigidBodyControl(new MeshCollisionShape(floorGeom.getMesh()), 0));
    rootNode.attachChild(floorGeom);
    getPhysicsSpace().add(floorGeom);
}
 
Example 3
Source File: TestBatchNodeTower.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void onAction(String name, boolean keyPressed, float tpf) {
            if (name.equals("shoot") && !keyPressed) {
                Geometry bulletg = new Geometry("bullet", bullet);
                bulletg.setMaterial(mat2);
                bulletg.setShadowMode(ShadowMode.CastAndReceive);
                bulletg.setLocalTranslation(cam.getLocation());
                RigidBodyControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1);
//                RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, 1);
                bulletNode.setLinearVelocity(cam.getDirection().mult(25));
                bulletg.addControl(bulletNode);
                rootNode.attachChild(bulletg);
                getPhysicsSpace().add(bulletNode);
            }
        }
 
Example 4
Source File: TestLodStress.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void simpleInitApp() {
        DirectionalLight dl = new DirectionalLight();
        dl.setDirection(new Vector3f(-1,-1,-1).normalizeLocal());
        rootNode.addLight(dl);

        Node teapotNode = (Node) assetManager.loadModel("Models/Teapot/Teapot.mesh.xml");
        Geometry teapot = (Geometry) teapotNode.getChild(0);
        
//        Sphere sph = new Sphere(16, 16, 4);
//        Geometry teapot = new Geometry("teapot", sph);

        Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        mat.setFloat("Shininess", 16f);
        mat.setBoolean("VertexLighting", true);
        teapot.setMaterial(mat);
        
       // show normals as material
        //Material mat = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");

        for (int y = -10; y < 10; y++){
            for (int x = -10; x < 10; x++){
                Geometry clonePot = teapot.clone();
                
                //clonePot.setMaterial(mat);
                clonePot.setLocalTranslation(x * .5f, 0, y * .5f);
                clonePot.setLocalScale(.15f);
                
                LodControl control = new LodControl();
                clonePot.addControl(control);
                rootNode.attachChild(clonePot);
            }
        }

        cam.setLocation(new Vector3f(8.378951f, 5.4324f, 8.795956f));
        cam.setRotation(new Quaternion(-0.083419204f, 0.90370524f, -0.20599906f, -0.36595422f));
    }
 
Example 5
Source File: PhysicsTestHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * creates a box geometry with a RigidBodyControl
 * @param assetManager
 * @return
 */
public static Geometry createPhysicsTestBox(AssetManager assetManager) {
    Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
    Box box = new Box(0.25f, 0.25f, 0.25f);
    Geometry boxGeometry = new Geometry("Box", box);
    boxGeometry.setMaterial(material);
    //RigidBodyControl automatically uses box collision shapes when attached to single geometry with box mesh
    boxGeometry.addControl(new RigidBodyControl(2));
    return boxGeometry;
}
 
Example 6
Source File: TestBrickTower.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void initFloor() {
    Box floorBox = new Box(Vector3f.ZERO, 10f, 0.1f, 5f);
    floorBox.scaleTextureCoordinates(new Vector2f(3, 6));

    Geometry floor = new Geometry("floor", floorBox);
    floor.setMaterial(mat3);
    floor.setShadowMode(ShadowMode.Receive);
    floor.setLocalTranslation(0, 0, 0);
    floor.addControl(new RigidBodyControl(0));
    this.rootNode.attachChild(floor);
    this.getPhysicsSpace().add(floor);
}
 
Example 7
Source File: TestBatchNodeTower.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void addBrick(Vector3f ori) {
    Geometry reBoxg = new Geometry("brick", brick);
    reBoxg.setMaterial(mat);
    reBoxg.setLocalTranslation(ori);
    reBoxg.rotate(0f, (float)Math.toRadians(angle) , 0f );
    reBoxg.addControl(new RigidBodyControl(1.5f));
    reBoxg.setShadowMode(ShadowMode.CastAndReceive);
    reBoxg.getControl(RigidBodyControl.class).setFriction(1.6f);
    this.batchNode.attachChild(reBoxg);
    this.getPhysicsSpace().add(reBoxg);
    nbBrick++;
}
 
Example 8
Source File: PhysicsTestHelper.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * creates a sphere geometry with a RigidBodyControl
 *
 * @param assetManager for loading assets
 * @return a new Geometry
 */
public static Geometry createPhysicsTestSphere(AssetManager assetManager) {
    Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
    Sphere sphere = new Sphere(8, 8, 0.25f);
    Geometry boxGeometry = new Geometry("Sphere", sphere);
    boxGeometry.setMaterial(material);
    //RigidBodyControl automatically uses sphere collision shapes when attached to single geometry with sphere mesh
    boxGeometry.addControl(new RigidBodyControl(2));
    return boxGeometry;
}
 
Example 9
Source File: HelloPhysics.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** This method creates one individual physical brick. */
public void makeBrick(Vector3f loc) {
  /** Create a brick geometry and attach to scene graph. */
  Geometry brick_geo = new Geometry("brick", box);
  brick_geo.setMaterial(wall_mat);
  rootNode.attachChild(brick_geo);
  /** Position the brick geometry  */
  brick_geo.setLocalTranslation(loc);
  /** Make brick physical with a mass > 0.0f. */
  brick_phy = new RigidBodyControl(2f);
  /** Add physical brick to physics space. */
  brick_geo.addControl(brick_phy);
  bulletAppState.getPhysicsSpace().add(brick_phy);
}
 
Example 10
Source File: TestBatchNodeTower.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
        public void onAction(String name, boolean keyPressed, float tpf) {
            if (name.equals("shoot") && !keyPressed) {
                Geometry bulletg = new Geometry("bullet", bullet);
                bulletg.setMaterial(mat2);
                bulletg.setShadowMode(ShadowMode.CastAndReceive);
                bulletg.setLocalTranslation(cam.getLocation());
                RigidBodyControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1);
//                RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, 1);
                bulletNode.setLinearVelocity(cam.getDirection().mult(25));
                bulletg.addControl(bulletNode);
                rootNode.attachChild(bulletg);
                getPhysicsSpace().add(bulletNode);
            }
        }
 
Example 11
Source File: TestIssue1283.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add a thin wall to the scene and physics space.
 */
private void addWall() {
    float thickness = 0.1f;
    Box wallMesh = new Box(10f, 10f, thickness);
    Geometry geometry = new Geometry("wall", wallMesh);
    rootNode.attachChild(geometry);
    geometry.setMaterial(wallMaterial);

    float mass = 0f; // static rigid body
    RigidBodyControl physicsControl = new RigidBodyControl(mass);
    geometry.addControl(physicsControl);

    physicsControl.setRestitution(0.8f);
    physicsSpace.add(physicsControl);
}
 
Example 12
Source File: TestWalkingChar.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void addBrick(Vector3f ori) {
    Geometry reBoxg = new Geometry("brick", brick);
    reBoxg.setMaterial(matBullet);
    reBoxg.setLocalTranslation(ori);
    reBoxg.addControl(new RigidBodyControl(1.5f));
    reBoxg.setShadowMode(ShadowMode.CastAndReceive);
    this.rootNode.attachChild(reBoxg);
    this.getPhysicsSpace().add(reBoxg);
}
 
Example 13
Source File: TestBrickWall.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void addBrick(Vector3f ori) {

        Geometry reBoxg = new Geometry("brick", brick);
        reBoxg.setMaterial(mat);
        reBoxg.setLocalTranslation(ori);
        //for geometry with sphere mesh the physics system automatically uses a sphere collision shape
        reBoxg.addControl(new RigidBodyControl(1.5f));
        reBoxg.setShadowMode(ShadowMode.CastAndReceive);
        reBoxg.getControl(RigidBodyControl.class).setFriction(0.6f);
        this.rootNode.attachChild(reBoxg);
        this.getPhysicsSpace().add(reBoxg);
    }
 
Example 14
Source File: TestBatchNodeTower.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void addBrick(Vector3f ori) {
    Geometry reBoxg = new Geometry("brick", brick);
    reBoxg.setMaterial(mat);
    reBoxg.setLocalTranslation(ori);
    reBoxg.rotate(0f, (float)Math.toRadians(angle) , 0f );
    reBoxg.addControl(new RigidBodyControl(1.5f));
    reBoxg.setShadowMode(ShadowMode.CastAndReceive);
    reBoxg.getControl(RigidBodyControl.class).setFriction(1.6f);
    this.batchNode.attachChild(reBoxg);
    this.getPhysicsSpace().add(reBoxg);
    nbBrick++;
}
 
Example 15
Source File: TestIssue1120.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void attachTestObject(Mesh mesh, Vector3f position, float mass) {
    Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    Geometry g = new Geometry("mesh", mesh);
    g.setLocalTranslation(position);
    g.setMaterial(material);

    RigidBodyControl control = new RigidBodyControl(new GImpactCollisionShape(mesh), mass);
    g.addControl(control);
    addObject(g);
}
 
Example 16
Source File: TestBrickWall.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void addBrick(Vector3f ori) {

        Geometry reBoxg = new Geometry("brick", brick);
        reBoxg.setMaterial(mat);
        reBoxg.setLocalTranslation(ori);
        //for geometry with sphere mesh the physics system automatically uses a sphere collision shape
        reBoxg.addControl(new RigidBodyControl(1.5f));
        reBoxg.setShadowMode(ShadowMode.CastAndReceive);
        reBoxg.getControl(RigidBodyControl.class).setFriction(0.6f);
        this.rootNode.attachChild(reBoxg);
        this.getPhysicsSpace().add(reBoxg);
    }
 
Example 17
Source File: TestRagdollCharacter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void initWall(float bLength, float bWidth, float bHeight) {
    Box brick = new Box(Vector3f.ZERO, bLength, bHeight, bWidth);
    brick.scaleTextureCoordinates(new Vector2f(1f, .5f));
    Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key.setGenerateMips(true);
    Texture tex = assetManager.loadTexture(key);
    mat2.setTexture("ColorMap", tex);
    
    float startpt = bLength / 4;
    float height = -5;
    for (int j = 0; j < 15; j++) {
        for (int i = 0; i < 4; i++) {
            Vector3f ori = new Vector3f(i * bLength * 2 + startpt, bHeight + height, -10);
            Geometry reBoxg = new Geometry("brick", brick);
            reBoxg.setMaterial(mat2);
            reBoxg.setLocalTranslation(ori);
            //for geometry with sphere mesh the physics system automatically uses a sphere collision shape
            reBoxg.addControl(new RigidBodyControl(1.5f));
            reBoxg.setShadowMode(ShadowMode.CastAndReceive);
            reBoxg.getControl(RigidBodyControl.class).setFriction(0.6f);
            this.rootNode.attachChild(reBoxg);
            this.getPhysicsSpace().add(reBoxg);
        }
        startpt = -startpt;
        height += 2 * bHeight;
    }
}
 
Example 18
Source File: TestBatchedTower.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void onAction(String name, boolean keyPressed, float tpf) {
            if (name.equals("shoot") && !keyPressed) {
                Geometry bulletg = new Geometry("bullet", bullet);
                bulletg.setMaterial(mat2);
                bulletg.setShadowMode(ShadowMode.CastAndReceive);
                bulletg.setLocalTranslation(cam.getLocation());
                RigidBodyControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1);
//                RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, 1);
                bulletNode.setLinearVelocity(cam.getDirection().mult(25));
                bulletg.addControl(bulletNode);
                rootNode.attachChild(bulletg);
                getPhysicsSpace().add(bulletNode);
            }
        }
 
Example 19
Source File: HelloPhysics.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Make a solid floor and add it to the scene. */
public void initFloor() {
  Geometry floor_geo = new Geometry("Floor", floor);
  floor_geo.setMaterial(floor_mat);
  floor_geo.setLocalTranslation(0, -0.1f, 0);
  this.rootNode.attachChild(floor_geo);
  /* Make the floor physical with mass 0.0f! */
  floor_phy = new RigidBodyControl(0.0f);
  floor_geo.addControl(floor_phy);
  bulletAppState.getPhysicsSpace().add(floor_phy);
}
 
Example 20
Source File: TestColorApp.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
    public void simpleInitApp() {
        // Lights
        DirectionalLight sun = new DirectionalLight();
        Vector3f sunPosition = new Vector3f(1, -1, 1);
        sun.setDirection(sunPosition);
        sun.setColor(new ColorRGBA(1f,1f,1f,1f));
        rootNode.addLight(sun);
 
        //DirectionalLightShadowFilter sun_renderer = new DirectionalLightShadowFilter(assetManager, 2048, 4);
        DirectionalLightShadowRenderer sun_renderer = new DirectionalLightShadowRenderer(assetManager, 2048, 1);
        sun_renderer.setLight(sun);
        viewPort.addProcessor(sun_renderer);
        
//        FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
//        fpp.addFilter(sun_renderer);
//        viewPort.addProcessor(fpp);
        
        rootNode.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
 
        // Camera
        viewPort.setBackgroundColor(new ColorRGBA(.6f, .6f, .6f, 1f));
        ChaseCamera chaseCam = new ChaseCamera(cam, inputManager);
 
 
        // Objects
        // Ground Object
        final Geometry groundBoxWhite = new Geometry("Box", new Box(7.5f, 7.5f, .25f));
        float[] f = {-FastMath.PI / 2, 3 * FastMath.PI / 2, 0f};
        groundBoxWhite.setLocalRotation(new Quaternion(f));
        groundBoxWhite.move(7.5f, -.75f, 7.5f);
        final Material groundMaterial = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        groundMaterial.setColor("Diffuse", new ColorRGBA(.9f, .9f, .9f, .9f));
        groundBoxWhite.setMaterial(groundMaterial);
        groundBoxWhite.addControl(chaseCam);
        rootNode.attachChild(groundBoxWhite);
 
        // Planter
        Geometry planterBox = new Geometry("Box", new Box(.5f, .5f, .5f));
        final Material planterMaterial = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        planterMaterial.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg"));
        planterBox.setMaterial(groundMaterial);
        planterBox.setLocalTranslation(10, 0, 9);
        rootNode.attachChild(planterBox);
 
        // Action!
        inputManager.addMapping("on", new KeyTrigger(KeyInput.KEY_Z));
        inputManager.addMapping("off", new KeyTrigger(KeyInput.KEY_X));
 
        inputManager.addListener(new AnalogListener() {
            @Override
            public void onAnalog(String s, float v, float v1) {
                if (s.equals("on")) {
                    groundBoxWhite.setMaterial(planterMaterial);
                }
                if (s.equals("off")) {
                    groundBoxWhite.setMaterial(groundMaterial);
                }
            }
        }, "on", "off");
 
        inputEnabled = true;
    }