/** * Removes all physics controls and joints in the given spatial from the physics space (e.g. * before saving to disk) - recursive if node * * @param spatial the rootnode containing the physics objects */ public void removeAll(Spatial spatial) { if (spatial.getControl(RigidBodyControl.class) != null) { RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class); // remove joints with physicsNode as BodyA List<PhysicsJoint> joints = physicsNode.getJoints(); for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext(); ) { PhysicsJoint physicsJoint = it1.next(); if (physicsNode.equals(physicsJoint.getBodyA())) { removeJoint(physicsJoint); // remove(physicsJoint.getBodyB()); } } remove(physicsNode); } else if (spatial.getControl(PhysicsControl.class) != null) { remove(spatial); } // recursion if (spatial instanceof Node) { List<Spatial> children = ((Node) spatial).getChildren(); for (Iterator<Spatial> it = children.iterator(); it.hasNext(); ) { Spatial spat = it.next(); removeAll(spat); } } }
@Override public void simpleUpdate(float tpf) { Juggernaut.Update(tpf); // Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f); // walkDirection.set( 0, 0, 0); // if(left) { walkDirection.addLocal(Vector3f.UNIT_X.negate().multLocal(0.4f));} // if(right) { walkDirection.addLocal(Vector3f.UNIT_X.clone().multLocal(0.4f));} // player.setWalkDirection(walkDirection); // if( walkDirection != Vector3f.ZERO){ // player.setViewDirection(walkDirection.negate()); // } // // playerDebug.setLocalTranslation(player.getPhysicsLocation()); elevator1.setLocalTranslation(304 + 20 * FastMath.cos(timer.getTimeInSeconds()), 20, 0); elvtr1.setPhysicsLocation(elevator1.getLocalTranslation()); // elevator2.setLocalTranslation( 192, 208 + 35*FastMath.cos(timer.getTimeInSeconds()), // 0); // elvtr2.setPhysicsLocation(elevator2.getLocalTranslation()); elvtr2.setLinearVelocity(new Vector3f(0, 25 * FastMath.cos(timer.getTimeInSeconds()), 0)); elvtr2.setPhysicsRotation(Matrix3f.IDENTITY); System.out.print(landscape.getCollideWithGroups() + "\n"); for (int i = 0; i < views.length; i++) { if (views[i].testForPlayer(Juggernaut)) { currentView = views[i]; } } cam.setLocation(currentView.CamPosition()); cam.lookAt(currentView.CamLookAt(), Vector3f.UNIT_Y); }
/** * Moves the camera to (0, y, 0) * * @param y */ public void reset() { centralNodeControl.setEnabled(false); centralNode.setLocalTranslation(0, 0, 0); centralNodeControl.setEnabled(true); // Re-enabling the control resets the gravity, // so we have to set it back to zero. centralNodeControl.setGravity(Vector3f.ZERO); }
@Override public void update(float tpf) { Quaternion rot = new Quaternion().fromAngleAxis(FastMath.QUARTER_PI * tpf, Vector3f.UNIT_Y); if (control != null) { Quaternion angle = control.getPhysicsRotation(); Quaternion rotation = rot.mult(angle).normalizeLocal(); control.setPhysicsRotation(rotation); car_con[index].setPhysicsRotation(rotation); } }
/** * Create a prop without adding it to the scene. * * @param propType which kind of prop to add (not null) * @param relativeSize the prop's size relative to standard (>0) * @return a new node to represent the prop, or null if the model is invalid */ private Node create(String propType, float relativeSize) { assert propType != null; assert relativeSize > 0f : relativeSize; Node propNode = loadModel(propType); Node modelNode = (Node) propNode.getChild(0); List<Spatial> parts = modelNode.getChildren(); /* * Texture each part of the prop and specify its hardness. */ for (Spatial part : parts) { String partName = part.getName(); if (!(part instanceof Geometry)) { logger.log(Level.SEVERE, "Prop part {0} is not a geometry.", MyString.quote(partName)); return null; } String materialName = material(partName); SpatialProperties.setHardness(part, materialName); Material material = Assets.loadBlockMaterial(materialName); part.setMaterial(material); } /* * Set the scale factor. */ float scaleFactor = scene.getScaleFactor(); float modelScale = relativeSize / scaleFactor; propNode.setLocalScale(modelScale); assert getRelativeSize(propNode) == relativeSize : relativeSize; /* * Set other properties. */ propNode.setShadowMode(RenderQueue.ShadowMode.CastAndReceive); /* * Give the prop a physics control. */ CollisionShape shape = createShape(propType, relativeSize, parts); float mass = calculateMass(propType, relativeSize); RigidBodyControl rigidBodyControl = new RigidBodyControl(shape, mass); propNode.addControl(rigidBodyControl); /* * Configure the physics control. */ rigidBodyControl.setFriction(friction); scene.getWorld().getPhysics().addObject(rigidBodyControl); /* * Give the prop a PickedControl. */ SpatialProperties.setPickType(propNode, pickType); PickedControl pickedControl = new PickedControl(); propNode.addControl(pickedControl); return propNode; }
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(getCam().getLocation()); RigidBodyControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1); // RigidBodyControl bulletNode = new // RigidBodyControl(bulletCollisionShape, 1); bulletNode.setLinearVelocity(getCam().getDirection().mult(25)); bulletg.addControl(bulletNode); rootNode.attachChild(bulletg); getPhysicsSpace().add(bulletNode); } }
public void setPosition(String objectID, Vector3f position) { // get "visual" or "physical" spatial // search in all sub-nodes of root node (scene node, trigger node, ...) Spatial object = Util.findNode(sim.getRootNode(), objectID); RigidBodyControl control = null; try { control = (RigidBodyControl) object.getControl(0); } catch (IndexOutOfBoundsException e2) { System.err.println("Could not manipulate physics of '" + objectID + "'!"); } if (control != null) control.setPhysicsLocation(position); else object.setLocalTranslation(position); }
public void showCar() { for (int i = 0; i < 4; i++) { cars[i] = factory.getCar(car_names[i], assetManager); car_con[i] = cars[i].getControl(VehicleControl.class); } Camera camera = cam.clone(); camera.setViewPort(.25f, .9f, .1f, .75f); carView = this.app.getRenderManager().createPostView("carview", camera); space.add(car_con[index]); dl = new DirectionalLight(); localRootNode.addLight(dl); ai = new AmbientLight(); localRootNode.addLight(ai); car_con[index].setPhysicsLocation(new Vector3f(0, 1, 0)); localRootNode.attachChild(cars[index]); floor = assetManager.loadModel("Models/garage/garage.mesh.j3o"); control = new RigidBodyControl(0); floor.addControl(control); control.setPhysicsLocation(Vector3f.ZERO); space.add(control); localRootNode.attachChild(floor); camera.setLocation(car_con[index].getPhysicsLocation().add(new Vector3f(3, 1f, 0))); camera.lookAt(car_con[index].getPhysicsLocation().add(new Vector3f(0, -1, 0)), Vector3f.UNIT_Y); dl.setDirection(camera.getDirection()); carView.attachScene(localRootNode); }
public void setRotation(String objectID, float[] rotation) { // get "visual" or "physical" spatial // search in all sub-nodes of root node (scene node, trigger node, ...) Spatial object = Util.findNode(sim.getRootNode(), objectID); RigidBodyControl control = null; try { control = (RigidBodyControl) object.getControl(0); } catch (IndexOutOfBoundsException e2) { System.err.println("Could not manipulate physics of '" + objectID + "'!"); } if (control != null) { Quaternion rot = new Quaternion().fromAngles(degToRad(rotation)); control.setPhysicsRotation(rot); } else { Quaternion rot = new Quaternion().fromAngles(degToRad(rotation)); object.setLocalRotation(rot); } }
private void resetWallPhysics(PhysicsSpace space) { List<Spatial> children = ((Node) getTerrainNode().getChild("Walls")).getChildren(); for (Spatial wallNode : children) { Spatial wall = ((Node) wallNode).getChild("Wall"); wall.scale(6f); space.removeAll(wallNode); CollisionShape meshShape = CollisionShapeFactory.createMeshShape(wall); wall.scale(1f / 6f); RigidBodyControl wallPhysics = new RigidBodyControl(meshShape, 0); wallPhysics.setCollideWithGroups(CollisionGroups.NONE); wallPhysics.setFriction(0.5f); wall.addControl(wallPhysics); wall.getControl(RigidBodyControl.class).setCollisionGroup(CollisionGroups.WALLS); space.addAll(wall); } }
public void loadLevel() { // create ground Vector3f pos = new Vector3f(0f, -1.5f, 0f); float size = 10f; Geometry ground = new Geometry("ground", new Box(Vector3f.ZERO, size, 1.0f, size)); // create node Node groundNode = new Node(); groundNode.attachChild(ground); // setup physics RigidBodyControl grbc = new RigidBodyControl(0f); ground.addControl(grbc); getPhysicsSpace().add(grbc); grbc.setPhysicsLocation(pos); // create boxes Vector3f[] positions = new Vector3f[] { new Vector3f(2f, 0f, 2f), new Vector3f(2f, 0f, -2f), new Vector3f(-2f, 0f, -2f), new Vector3f(-2f, 0f, 2f) }; for (int i = 0; i < 4; i++) { Box box = new Box(1f, 1f, 1f); Geometry geometry = new Geometry("geometry", box); Node node = new Node("box"); node.attachChild(geometry); RigidBodyControl rbc = new RigidBodyControl(0f); geometry.addControl(rbc); getPhysicsSpace().add(rbc); rbc.setPhysicsLocation(positions[i]); } }
/** * 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 and activate shadows */ ball_geo.setLocalTranslation(cam.getLocation()); ball_geo.setShadowMode(ShadowMode.CastAndReceive); /** Make the ball physcial 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 physcial ball to shoot it. */ ball_phy.setLinearVelocity(cam.getDirection().mult(25)); }
public static Spatial AddModel(Vector3f position, String name) { s_Model = Main.s_AssetManager.loadModel("Models/tree.obj"); s_Model.setLocalTranslation(position); s_Model.scale(0.8f); s_Model.setName(name); s_Model.setShadowMode(ShadowMode.CastAndReceive); CollisionShape collision = new CapsuleCollisionShape(0.5f, 1f); physicsControl = new RigidBodyControl(collision, 0); physicsControl.setKinematic(true); s_Model.addControl(physicsControl); Main.bulletAppState.getPhysicsSpace().add(s_Model); // Main.s_TreeNode.attachChild(s_Model); return s_Model; }
@Override protected void controlUpdate(float tpf) { RigidBodyControl diceControl = spatial.getControl(RigidBodyControl.class); if (number != 0) { diceControl.setEnabled(true); int numberValue = 0; synchronized (spatial) { numberValue = number; } switch (numberValue) { case 1: diceControl.setPhysicsRotation( new Quaternion().fromAngleAxis(FastMath.HALF_PI, new Vector3f(1, 0, 0))); break; case 2: diceControl.setPhysicsRotation( new Quaternion().fromAngleAxis(-FastMath.HALF_PI, new Vector3f(0, 1, 0))); break; case 3: diceControl.setPhysicsRotation( new Quaternion().fromAngleAxis(FastMath.ZERO_TOLERANCE, new Vector3f(1, 0, 0))); break; case 4: diceControl.setPhysicsRotation( new Quaternion().fromAngleAxis(FastMath.PI, new Vector3f(1, 0, 0))); break; case 5: diceControl.setPhysicsRotation( new Quaternion().fromAngleAxis(FastMath.HALF_PI, new Vector3f(0, 1, 0))); break; case 6: diceControl.setPhysicsRotation( new Quaternion().fromAngleAxis(-FastMath.HALF_PI, new Vector3f(1, 0, 0))); break; } diceControl.setLinearVelocity(new Vector3f(0, 0, 0)); diceControl.setAngularVelocity(new Vector3f(0, 0, 0)); diceControl.setPhysicsLocation(putLocation); spatial.getControl(AbstractRollControl.class).setEnabled(false); Geometry d6 = ((Geometry) spatial); d6.getMaterial().setColor("Diffuse", ColorRGBA.White); number = 0; } else { diceControl.setEnabled(false); this.setEnabled(false); } }
/** Sets the speed at which the camera should move forward */ public void setSpeed(float speed) { centralNodeControl.setLinearVelocity(new Vector3f(0, speed, 0)); }
/** Gets the speed at which the camera is moving forward */ public float getSpeed() { return centralNodeControl.getLinearVelocity().y; }
@Override public void simpleInitApp() { // setUpKeys(); flyCam.setEnabled(false); bulletAppState = new BulletAppState(); stateManager.attach(bulletAppState); bulletAppState.getPhysicsSpace().enableDebug(assetManager); Juggernaut = new Character(this, bulletAppState); setUpCameraBoxes(); // Load in the level // Material m = assetManager.loadMaterial("Models/levelLayout - Update.mtl"); Spatial map = assetManager.loadModel("Models/levelLayout - Update_cameraPos.obj"); rootNode.attachChild(map); Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); // mark_mat.setColor("Color", ColorRGBA.White); // mark.setMaterial(mark_mat); // mark.setLocalTranslation(1f,1f,1f); // rootNode.attachChild(mark); CollisionShape sceneShape = CollisionShapeFactory.createMeshShape(map); landscape = new RigidBodyControl(sceneShape, 0); map.addControl(landscape); landscape.setCollisionGroup(1); landscape.removeCollideWithGroup(2); // //Load Ninja as filler for character model // ninja = assetManager.loadModel("Models/Ninja/Ninja.mesh.xml"); // //ninja.rotate(0, -1.5f, 0); // ninja.scale(0.02f, 0.02f, 0.02f); // ninja.setLocalTranslation(new Vector3f(341, 300, 0)); // //ninja.setMaterial(mark_mat); // rootNode.attachChild(ninja); // CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1f, 2f); // player = new CharacterControl(capsuleShape, .05f); // player.setJumpSpeed(50); // player.setFallSpeed(50); // player.setGravity(120); // player.setPhysicsLocation(new Vector3f(1f,8f,1f)); // player.setViewDirection(new Vector3f(-1.0f, 0, 0)); // player.setCollideWithGroups(2); // // // // playerDebug = player.createDebugShape(assetManager); // ninja.addControl(player); // rootNode.attachChild(playerDebug); // // bulletAppState.getPhysicsSpace().add(player); bulletAppState.getPhysicsSpace().add(landscape); // Elevator 1 elevator1 = new Geometry("Elevator1", new Box(4, 1, 5)); elevator1.setLocalTranslation(304, 20, 0); elevator1.setMaterial(mark_mat); elvtr1 = new RigidBodyControl(10); elevator1.addControl(elvtr1); elvtr1.setFriction(1000.0f); elvtr1.setKinematic(true); bulletAppState.getPhysicsSpace().add(elvtr1); rootNode.attachChild(elevator1); // Elevator 2 elevator2 = new Geometry("Elevator2", new Box(4, 1, 5)); elevator2.setLocalTranslation(192, 208, 0); elevator2.setMaterial(mark_mat); elvtr2 = new RigidBodyControl(1000000000); elevator2.addControl(elvtr2); elvtr2.setAngularDamping(100000.0f); elvtr2.setFriction(1000.0f); // elvtr2.setKinematic(true); bulletAppState.getPhysicsSpace().add(elvtr2); rootNode.attachChild(elevator2); // Add lights to see the models DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f)); rootNode.addLight(sun); AmbientLight al = new AmbientLight(); al.setColor(ColorRGBA.White); rootNode.addLight(al); // cam.setLocation(new Vector3f(player.getPhysicsLocation().x, // player.getPhysicsLocation().y + 5, player.getPhysicsLocation().z +40 )); // cam.lookAt(player.getPhysicsLocation(), Vector3f.UNIT_Y); cam.setLocation(new Vector3f(0, 5, 40)); cam.lookAt(new Vector3f(0, 5, 0), Vector3f.UNIT_Y); }
/** * Initialises and sets up the camera. This should be called as soon as possible * * @param root The node to attach the camera to * @param physics The physics of the physics engine * @param cam The camera that we are going to position here */ public void loadCamera(Node root, PhysicsSpace physics, Camera cam) { // Create a node that is at the origin centralNode = new Node(nodeName); // Create a physics control, that we can move the node with centralNodeControl = new RigidBodyControl(1); // Add the control centralNode.addControl(centralNodeControl); // Set up camera CameraNode camNode = new CameraNode("cam", cam); // The node moves the camera, instead of the camera moving the node camNode.setControlDir(CameraControl.ControlDirection.SpatialToCamera); // Move it back and up a bit camNode.setLocalTranslation(new Vector3f(0, -10, 20)); // camNode.setLocalTranslation(new Vector3f(0, -10, 50)); // Look at the origin camNode.lookAt(centralNode.getLocalTranslation(), Vector3f.UNIT_Z); // Attach the camera to the node at the origin, so that the camera // follows the node centralNode.attachChild(camNode); // Plane that stays at the top of the screen to stop the ball rolling // out of sight Plane upperPlane = new Plane(Vector3f.UNIT_Y.negate(), -11f); RigidBodyControl upperCollision = new RigidBodyControl(new PlaneCollisionShape(upperPlane), 1f); Node upperPlaneNode = new Node("uplane"); upperPlaneNode.addControl(upperCollision); // And another plane for bottom of the screen Plane lowerPlane = new Plane(Vector3f.UNIT_Y, -19f); RigidBodyControl lowerCollision = new RigidBodyControl(new PlaneCollisionShape(lowerPlane), 1f); Node lowerPlaneNode = new Node("lplane"); lowerPlaneNode.addControl(lowerCollision); // Put the planes into their own group so that boxes do not collide with them upperCollision.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02); lowerCollision.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02); // Makes the planes un-affected by forces upperCollision.setKinematic(true); lowerCollision.setKinematic(true); // Attach the planes to the central point that the camera looks at // That way we can move them both by moving the central point. centralNode.attachChild(upperPlaneNode); centralNode.attachChild(lowerPlaneNode); // Add some lighting PointLight light = new PointLight(); light.setColor(ColorRGBA.White); // light.setRadius(30); LightControl lightControl = new LightControl(light); // Needs to be added to the camera, as a point in space camNode.addControl(lightControl); // And added to the root, to have an effect on everything root.addLight(light); // Finally, add the centre point to the root node root.attachChild(centralNode); // And add the planes and the centre point to the physics space physics.add(upperPlaneNode); physics.add(lowerPlaneNode); physics.add(centralNode); centralNodeControl.setGravity(Vector3f.ZERO); // No gravity centralNodeControl.setLinearVelocity(Vector3f.UNIT_Y); // Moves forward }