Java Code Examples for com.jme3.bullet.PhysicsSpace#add()

The following examples show how to use com.jme3.bullet.PhysicsSpace#add() . 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: KinematicRagdollControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected void addPhysics(PhysicsSpace space) {
    if (baseRigidBody != null) {
        space.add(baseRigidBody);
    }
    for (Iterator<PhysicsBoneLink> it = boneLinks.values().iterator(); it.hasNext();) {
        PhysicsBoneLink physicsBoneLink = it.next();
        if (physicsBoneLink.rigidBody != null) {
            space.add(physicsBoneLink.rigidBody);
            if (physicsBoneLink.joint != null) {
                space.add(physicsBoneLink.joint);

            }
        }
    }
    space.addCollisionListener(this);
}
 
Example 2
Source File: DacLinks.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Add all managed physics objects to the PhysicsSpace.
 */
@Override
protected void addPhysics(PhysicsSpace space) {
    Vector3f gravity = gravity(null);

    PhysicsRigidBody rigidBody;
    if (torsoLink != null) {
        rigidBody = torsoLink.getRigidBody();
        space.add(rigidBody);
        rigidBody.setGravity(gravity);
    }

    for (BoneLink boneLink : boneLinkList) {
        rigidBody = boneLink.getRigidBody();
        space.add(rigidBody);
        rigidBody.setGravity(gravity);

        PhysicsJoint joint = boneLink.getJoint();
        space.add(joint);
    }
}
 
Example 3
Source File: PhysicsTestHelper.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * creates the necessary inputlistener and action to shoot balls from the camera
 *
 * @param app the application that's running
 * @param rootNode where ball geometries should be added
 * @param space where collision objects should be added
 */
public static void createBallShooter(final Application app, final Node rootNode, final PhysicsSpace space) {
    ActionListener actionListener = new ActionListener() {

        @Override
        public void onAction(String name, boolean keyPressed, float tpf) {
            Sphere bullet = new Sphere(32, 32, 0.4f, true, false);
            bullet.setTextureMode(TextureMode.Projected);
            Material mat2 = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
            TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
            key2.setGenerateMips(true);
            Texture tex2 = app.getAssetManager().loadTexture(key2);
            mat2.setTexture("ColorMap", tex2);
            if (name.equals("shoot") && !keyPressed) {
                Geometry bulletg = new Geometry("bullet", bullet);
                bulletg.setMaterial(mat2);
                bulletg.setShadowMode(ShadowMode.CastAndReceive);
                bulletg.setLocalTranslation(app.getCamera().getLocation());
                RigidBodyControl bulletControl = new RigidBodyControl(10);
                bulletg.addControl(bulletControl);
                bulletControl.setLinearVelocity(app.getCamera().getDirection().mult(25));
                bulletg.addControl(bulletControl);
                rootNode.attachChild(bulletg);
                space.add(bulletControl);
            }
        }
    };
    app.getInputManager().addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    app.getInputManager().addListener(actionListener, "shoot");
}
 
Example 4
Source File: PhysicsTestHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * creates the necessary inputlistener and action to shoot balls from teh camera
 * @param app
 * @param rootNode
 * @param space
 */
public static void createBallShooter(final Application app, final Node rootNode, final PhysicsSpace space) {
    ActionListener actionListener = new ActionListener() {

        public void onAction(String name, boolean keyPressed, float tpf) {
            Sphere bullet = new Sphere(32, 32, 0.4f, true, false);
            bullet.setTextureMode(TextureMode.Projected);
            Material mat2 = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
            TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
            key2.setGenerateMips(true);
            Texture tex2 = app.getAssetManager().loadTexture(key2);
            mat2.setTexture("ColorMap", tex2);
            if (name.equals("shoot") && !keyPressed) {
                Geometry bulletg = new Geometry("bullet", bullet);
                bulletg.setMaterial(mat2);
                bulletg.setShadowMode(ShadowMode.CastAndReceive);
                bulletg.setLocalTranslation(app.getCamera().getLocation());
                RigidBodyControl bulletControl = new RigidBodyControl(1);
                bulletg.addControl(bulletControl);
                bulletControl.setLinearVelocity(app.getCamera().getDirection().mult(25));
                bulletg.addControl(bulletControl);
                rootNode.attachChild(bulletg);
                space.add(bulletControl);
            }
        }
    };
    app.getInputManager().addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    app.getInputManager().addListener(actionListener, "shoot");
}
 
Example 5
Source File: PhysicsTestHelper.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
     * creates a simple physics test world with a floor, an obstacle and some test boxes
     *
     * @param rootNode where lights and geometries should be added
     * @param assetManager for loading assets
     * @param space where collision objects should be added
     */
    public static void createPhysicsTestWorld(Node rootNode, AssetManager assetManager, PhysicsSpace space) {
        AmbientLight light = new AmbientLight();
        light.setColor(ColorRGBA.LightGray);
        rootNode.addLight(light);

        Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));

        Box floorBox = new Box(140, 0.25f, 140);
        Geometry floorGeometry = new Geometry("Floor", floorBox);
        floorGeometry.setMaterial(material);
        floorGeometry.setLocalTranslation(0, -5, 0);
//        Plane plane = new Plane();
//        plane.setOriginNormal(new Vector3f(0, 0.25f, 0), Vector3f.UNIT_Y);
//        floorGeometry.addControl(new RigidBodyControl(new PlaneCollisionShape(plane), 0));
        floorGeometry.addControl(new RigidBodyControl(0));
        rootNode.attachChild(floorGeometry);
        space.add(floorGeometry);

        //movable boxes
        for (int i = 0; i < 12; i++) {
            Box box = new Box(0.25f, 0.25f, 0.25f);
            Geometry boxGeometry = new Geometry("Box", box);
            boxGeometry.setMaterial(material);
            boxGeometry.setLocalTranslation(i, 5, -3);
            //RigidBodyControl automatically uses box collision shapes when attached to single geometry with box mesh
            boxGeometry.addControl(new RigidBodyControl(2));
            rootNode.attachChild(boxGeometry);
            space.add(boxGeometry);
        }

        //immovable sphere with mesh collision shape
        Sphere sphere = new Sphere(8, 8, 1);
        Geometry sphereGeometry = new Geometry("Sphere", sphere);
        sphereGeometry.setMaterial(material);
        sphereGeometry.setLocalTranslation(4, -4, 2);
        sphereGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(sphere), 0));
        rootNode.attachChild(sphereGeometry);
        space.add(sphereGeometry);

    }
 
Example 6
Source File: CollisionTester.java    From OpenRTS with MIT License 4 votes vote down vote up
public static boolean areColliding(Asset asset1, Asset asset2, boolean debug){
		Spatial s1 = getSpatialFromAsset(asset1); 
		Spatial s2 = getSpatialFromAsset(asset2);

		PhysicsSpace space = new PhysicsSpace();
		
		RigidBodyControl ghost1 = new RigidBodyControl(getCollisionShape(asset1));
		s1.addControl(ghost1);
		space.add(ghost1);

		RigidBodyControl ghost2 = new RigidBodyControl(getCollisionShape(asset2));
		s2.addControl(ghost2);
//		ghost2.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02);
//		space.add(ghost2);

		space.update(1);
		
//		int numCollision = ghost1.getOverlappingCount();
//		boolean collision = numCollision > 0;
		Transform t = new Transform();
		t.setRotation(s2.getLocalRotation());
		t.setTranslation(s2.getLocalTranslation());
		boolean collision = false;
		for(ChildCollisionShape hull : getCollisionShape(asset2).getChildren())
			if(!space.sweepTest(hull.shape, Transform.IDENTITY, t).isEmpty()){
				collision = true;
				break;
			}
				
		
		space.remove(ghost1);
//		space.remove(ghost2);

//		if(!collision){
//			Spatial debugS2 = DebugShapeFactory.getDebugShape(ghost2.getCollisionShape());
//			debugS2.setLocalRotation(ghost2.getPhysicsRotation());
////			Spatial debugS2 = s2;
//			Material m = new Material(am, "Common/MatDefs/Misc/Unshaded.j3md");
//			m.getAdditionalRenderState().setWireframe(true);
//			m.setColor("Color", ColorRGBA.Red);
//			debugS2.setMaterial(m);
//			debugS2.setLocalTranslation(ghost2.getPhysicsLocation());
//			asset2.s = debugS2;
//			//EventManager.post(new GenericEvent(debugS2));
//		}
			
		if(!collision){// && debug){
			Material m = new Material(am, "Common/MatDefs/Misc/Unshaded.j3md");
			m.getAdditionalRenderState().setWireframe(true);
			m.setColor("Color", ColorRGBA.Red);
			Spatial debugS2 = DebugShapeFactory.getDebugShape(getCollisionShape(asset2));
			debugS2.setLocalTransform(t);
//			debugS2.setLocalRotation(ghost2.getPhysicsRotation());
//			debugS2.setLocalTranslation(ghost2.getPhysicsLocation());
			debugS2.setMaterial(m);

			Material m2 = new Material(am, "Common/MatDefs/Misc/Unshaded.j3md");
			m2.getAdditionalRenderState().setWireframe(true);
			m2.setColor("Color", ColorRGBA.Blue);
			Geometry linegeom = new Geometry();
			Line l = new Line(debugS2.getLocalTranslation().add(0,  0, 1), ghost1.getPhysicsLocation().add(0,  0, 1));
			linegeom.setMesh(l);
			linegeom.setMaterial(m2);
	
			asset2.s = debugS2;
			if(l.getStart().distance(l.getEnd())<2)
				asset2.links.add(linegeom);
//			EventManager.post(new GenericEvent(debugS2));
//			EventManager.post(new GenericEvent(linegeom));
			
			
		}

		return collision; 
	}
 
Example 7
Source File: PhysicsTestHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
     * creates a simple physics test world with a floor, an obstacle and some test boxes
     * @param rootNode
     * @param assetManager
     * @param space
     */
    public static void createPhysicsTestWorld(Node rootNode, AssetManager assetManager, PhysicsSpace space) {
        AmbientLight light = new AmbientLight();
        light.setColor(ColorRGBA.LightGray);
        rootNode.addLight(light);

        Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));

        Box floorBox = new Box(140, 0.25f, 140);
        Geometry floorGeometry = new Geometry("Floor", floorBox);
        floorGeometry.setMaterial(material);
        floorGeometry.setLocalTranslation(0, -5, 0);
//        Plane plane = new Plane();
//        plane.setOriginNormal(new Vector3f(0, 0.25f, 0), Vector3f.UNIT_Y);
//        floorGeometry.addControl(new RigidBodyControl(new PlaneCollisionShape(plane), 0));
        floorGeometry.addControl(new RigidBodyControl(0));
        rootNode.attachChild(floorGeometry);
        space.add(floorGeometry);

        //movable boxes
        for (int i = 0; i < 12; i++) {
            Box box = new Box(0.25f, 0.25f, 0.25f);
            Geometry boxGeometry = new Geometry("Box", box);
            boxGeometry.setMaterial(material);
            boxGeometry.setLocalTranslation(i, 5, -3);
            //RigidBodyControl automatically uses box collision shapes when attached to single geometry with box mesh
            boxGeometry.addControl(new RigidBodyControl(2));
            rootNode.attachChild(boxGeometry);
            space.add(boxGeometry);
        }

        //immovable sphere with mesh collision shape
        Sphere sphere = new Sphere(8, 8, 1);
        Geometry sphereGeometry = new Geometry("Sphere", sphere);
        sphereGeometry.setMaterial(material);
        sphereGeometry.setLocalTranslation(4, -4, 2);
        sphereGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(sphere), 0));
        rootNode.attachChild(sphereGeometry);
        space.add(sphereGeometry);

    }
 
Example 8
Source File: PhysicsTestHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static void createPhysicsTestWorldSoccer(Node rootNode, AssetManager assetManager, PhysicsSpace space) {
        AmbientLight light = new AmbientLight();
        light.setColor(ColorRGBA.LightGray);
        rootNode.addLight(light);

        Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));

        Box floorBox = new Box(140, 0.25f, 140);
        Geometry floorGeometry = new Geometry("Floor", floorBox);
        floorGeometry.setMaterial(material);
        floorGeometry.setLocalTranslation(0, -0.25f, 0);
//        Plane plane = new Plane();
//        plane.setOriginNormal(new Vector3f(0, 0.25f, 0), Vector3f.UNIT_Y);
//        floorGeometry.addControl(new RigidBodyControl(new PlaneCollisionShape(plane), 0));
        floorGeometry.addControl(new RigidBodyControl(0));
        rootNode.attachChild(floorGeometry);
        space.add(floorGeometry);

        //movable spheres
        for (int i = 0; i < 5; i++) {
            Sphere sphere = new Sphere(16, 16, .5f);
            Geometry ballGeometry = new Geometry("Soccer ball", sphere);
            ballGeometry.setMaterial(material);
            ballGeometry.setLocalTranslation(i, 2, -3);
            //RigidBodyControl automatically uses Sphere collision shapes when attached to single geometry with sphere mesh
            ballGeometry.addControl(new RigidBodyControl(.001f));
            ballGeometry.getControl(RigidBodyControl.class).setRestitution(1);
            rootNode.attachChild(ballGeometry);
            space.add(ballGeometry);
        }

        //immovable Box with mesh collision shape
        Box box = new Box(1, 1, 1);
        Geometry boxGeometry = new Geometry("Box", box);
        boxGeometry.setMaterial(material);
        boxGeometry.setLocalTranslation(4, 1, 2);
        boxGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(box), 0));
        rootNode.attachChild(boxGeometry);
        space.add(boxGeometry);

    }