RigidBodyControl control = new RigidBodyControl(collisionShape, mass); control.setAngularFactor(getAngularFactor()); control.setAngularSleepingThreshold(getAngularSleepingThreshold()); control.setCcdMotionThreshold(getCcdMotionThreshold()); control.setCcdSweptSphereRadius(getCcdSweptSphereRadius()); control.setCollideWithGroups(getCollideWithGroups()); control.setCollisionGroup(getCollisionGroup()); control.setContactResponse(isContactResponse()); control.setDamping(getLinearDamping(), getAngularDamping()); control.setFriction(getFriction()); control.setGravity(getGravity()); control.setKinematic(isKinematic()); control.setKinematicSpatial(isKinematicSpatial()); control.setLinearSleepingThreshold(getLinearSleepingThreshold()); control.setPhysicsLocation(getPhysicsLocation(null)); control.setPhysicsRotation(getPhysicsRotationMatrix(null)); control.setRestitution(getRestitution()); control.setAngularVelocity(getAngularVelocity()); control.setLinearVelocity(getLinearVelocity()); control.setApplyPhysicsLocal(isApplyPhysicsLocal()); control.spatial = this.spatial; control.setEnabled(isEnabled());
/** * Alter which spatial is controlled. Invoked when the control is added to * or removed from a spatial. Should be invoked only by a subclass or from * Spatial. Do not invoke directly from user code. * * @param spatial the spatial to control (or null) */ public void setSpatial(Spatial spatial) { this.spatial = spatial; setUserObject(spatial); if (spatial == null) { return; } if (collisionShape == null) { createCollisionShape(); rebuildRigidBody(); } setPhysicsLocation(getSpatialTranslation()); setPhysicsRotation(getSpatialRotation()); }
/** * Enable or disable this control. * <p> * When the control is disabled, the body is removed from physics space. * When the control is enabled again, the body is moved to the current * location of the spatial and then added to the physics space. * * @param enabled true→enable the control, false→disable it */ public void setEnabled(boolean enabled) { this.enabled = enabled; if (space != null) { if (enabled && !added) { if (spatial != null) { setPhysicsLocation(getSpatialTranslation()); setPhysicsRotation(getSpatialRotation()); } space.addCollisionObject(this); added = true; } else if (!enabled && added) { space.removeCollisionObject(this); added = false; } } }
/** * creates an empty node with a RigidBodyControl * * @param manager for loading assets * @param shape a shape for the collision object * @param mass a mass for rigid body * @return a new Node */ public static Node createPhysicsTestNode(AssetManager manager, CollisionShape shape, float mass) { Node node = new Node("PhysicsNode"); RigidBodyControl control = new RigidBodyControl(shape, mass); node.addControl(control); return node; }
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.rootNode.attachChild(reBoxg); this.getPhysicsSpace().add(reBoxg); }
public void onAction(String binding, boolean value, float tpf) { if (binding.equals("shoot") && !value) { Geometry bulletg = new Geometry("bullet", bullet); bulletg.setMaterial(mat); bulletg.setName("bullet"); bulletg.setLocalTranslation(cam.getLocation()); bulletg.setShadowMode(ShadowMode.CastAndReceive); bulletg.addControl(new RigidBodyControl(bulletCollisionShape, 1)); bulletg.getControl(RigidBodyControl.class).setCcdMotionThreshold(0.1f); bulletg.getControl(RigidBodyControl.class).setLinearVelocity(cam.getDirection().mult(40)); rootNode.attachChild(bulletg); getPhysicsSpace().add(bulletg); } else if (binding.equals("shoot2") && !value) { Geometry bulletg = new Geometry("bullet", bullet); bulletg.setMaterial(mat2); bulletg.setName("bullet"); bulletg.setLocalTranslation(cam.getLocation()); bulletg.setShadowMode(ShadowMode.CastAndReceive); bulletg.addControl(new RigidBodyControl(bulletCollisionShape, 1)); bulletg.getControl(RigidBodyControl.class).setLinearVelocity(cam.getDirection().mult(40)); rootNode.attachChild(bulletg); getPhysicsSpace().add(bulletg); } } }
floorGeometry.addControl(new RigidBodyControl(0)); rootNode.attachChild(floorGeometry); space.add(floorGeometry); ballGeometry.setLocalTranslation(i, 2, -3); ballGeometry.addControl(new RigidBodyControl(.001f)); ballGeometry.getControl(RigidBodyControl.class).setRestitution(1); rootNode.attachChild(ballGeometry); space.add(ballGeometry); boxGeometry.setMaterial(material); boxGeometry.setLocalTranslation(4, 1, 2); boxGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(box), 0)); rootNode.attachChild(boxGeometry); space.add(boxGeometry); boxGeometry.setMaterial(material); boxGeometry.setLocalTranslation(4, 3, 4); boxGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(box), 0)); rootNode.attachChild(boxGeometry); space.add(boxGeometry);
vehicle.setAngularVelocity(Vector3f.ZERO); vehicle.resetSuspension(); bridge.setPhysicsLocation(new Vector3f(0,1.4f,4)); bridge.setPhysicsRotation(Quaternion.DIRECTION_Z.toRotationMatrix());
private Geometry getRandomBall(Vector3f location) { Vector3f localLocation = new Vector3f(); localLocation.set(location); localLocation.addLocal(new Vector3f(random.nextFloat() - 0.5f, random.nextFloat() - 0.5f, random.nextFloat() - 0.5f).normalize().mult(3)); Geometry poiGeom = new Geometry("ball", ballMesh); poiGeom.setLocalTranslation(localLocation); poiGeom.setMaterial(ballMaterial); RigidBodyControl control = new RigidBodyControl(ballCollisionShape, 1); //!!! Important control.setApplyPhysicsLocal(true); poiGeom.addControl(control); float x = (random.nextFloat() - 0.5f) * 100; float y = (random.nextFloat() - 0.5f) * 100; float z = (random.nextFloat() - 0.5f) * 100; control.setLinearVelocity(new Vector3f(x, y, z)); return poiGeom; }
private void reset() { // Reset the pickups for(Spatial pickUp : pickUps.getChildren()) { GhostControl pickUpControl = pickUp.getControl(GhostControl.class); if(pickUpControl != null) { pickUpControl.setEnabled(true); } pickUp.setLocalScale(1.0f); } // Reset the player player.setPhysicsLocation(PLAYER_START.clone()); player.setAngularVelocity(Vector3f.ZERO.clone()); player.setLinearVelocity(Vector3f.ZERO.clone()); // Reset the score score = 0; // Reset the message messageText.setLocalScale(0.0f); }
/** 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)); }
physicsSphere.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3, 6, 0)); physicsSphere.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true); rootNode.attachChild(physicsSphere); getPhysicsSpace().add(physicsSphere); Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, physicsSphere.getControl(RigidBodyControl.class).getCollisionShape(), 1); physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(4, 8, 0)); physicsSphere2.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true); rootNode.attachChild(physicsSphere2); getPhysicsSpace().add(physicsSphere2); physicsBox.getControl(RigidBodyControl.class).setFriction(0.1f); physicsBox.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.6f, 4, .5f)); physicsBox.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true); rootNode.attachChild(physicsBox); getPhysicsSpace().add(physicsBox); physicsCylinder.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2, 2, 0)); physicsCylinder.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true); rootNode.attachChild(physicsCylinder); getPhysicsSpace().add(physicsCylinder); node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2.5f, -4, 0f)); node2.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true); rootNode.attachChild(node2); getPhysicsSpace().add(node2); node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f)); node3.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
@Override public void simpleInitApp() { bulletAppState = new BulletAppState(); stateManager.attach(bulletAppState); bulletAppState.setDebugEnabled(true); // Add a physics sphere to the world Node physicsSphere = PhysicsTestHelper.createPhysicsTestNode(assetManager, new SphereCollisionShape(1), 1); physicsSphere.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3, 6, 0)); rootNode.attachChild(physicsSphere); getPhysicsSpace().add(physicsSphere); // Add a physics sphere to the world Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new SphereCollisionShape(1), 1); physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(4, 8, 0)); physicsSphere2.getControl(RigidBodyControl.class).addCollideWithGroup(PhysicsCollisionObject.COLLISION_GROUP_02); rootNode.attachChild(physicsSphere2); getPhysicsSpace().add(physicsSphere2); // an obstacle mesh, does not move (mass=0) Node node2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new MeshCollisionShape(new Sphere(16, 16, 1.2f)), 0); node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2.5f, -4, 0f)); node2.getControl(RigidBodyControl.class).setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02); node2.getControl(RigidBodyControl.class).setCollideWithGroups(PhysicsCollisionObject.COLLISION_GROUP_02); rootNode.attachChild(node2); getPhysicsSpace().add(node2); // the floor, does not move (mass=0) Node node3 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new MeshCollisionShape(new Box(100f, 0.2f, 100f)), 0); node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f)); rootNode.attachChild(node3); getPhysicsSpace().add(node3); }
Node createTestNode(float mass, Vector3f location) { float size = 0.1f; Vector3f halfExtents = new Vector3f(size, size, size); CollisionShape shape = new BoxCollisionShape(halfExtents); RigidBodyControl control = new RigidBodyControl(shape, mass); Node node = new Node(); node.addControl(control); rootNode.attachChild(node); bulletAppState.getPhysicsSpace().add(node); control.setPhysicsLocation(location); return node; }
physicsSphere.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3, 6, 0)); rootNode.attachChild(physicsSphere); getPhysicsSpace().add(physicsSphere); Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, physicsSphere.getControl(RigidBodyControl.class).getCollisionShape(), 1); physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(4, 8, 0)); rootNode.attachChild(physicsSphere2); getPhysicsSpace().add(physicsSphere2); physicsBox.getControl(RigidBodyControl.class).setFriction(0.1f); physicsBox.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.6f, 4, .5f)); rootNode.attachChild(physicsBox); getPhysicsSpace().add(physicsBox); physicsCylinder.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2, 2, 0)); rootNode.attachChild(physicsCylinder); getPhysicsSpace().add(physicsCylinder); node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2.5f, -4, 0f)); rootNode.attachChild(node2); getPhysicsSpace().add(node2); node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f)); rootNode.attachChild(node3); getPhysicsSpace().add(node3);
device.getControl(RigidBodyControl.class).setEnabled(true); device.getControl(RigidBodyControl.class).setPhysicsLocation(place.getWorldTranslation().addLocal(0f, 0.015f, 0f)); device.getControl(RigidBodyControl.class).setPhysicsRotation(new Quaternion().fromAngles(-FastMath.HALF_PI, 0f, 0f));
ball.control.setGravity(Vector3f.ZERO); Vector3f location = ball.geometry.getLocalTranslation(); Vector3f velocity = ball.control.getLinearVelocity(); ball.control.setLinearVelocity(velocity.mult(0.01f)); myLogger.warn("Ball velocity at {}; auto-braking!", velocity.length()); velocity = velocity.mult(0.01f); //In case we use it later; ball.control.applyCentralForce(totalForce); ball.control.setLinearDamping(currentDamping); } else {
@Override public void simpleInitApp() { flyCam.setEnabled(false); BulletAppState bulletAppState = new BulletAppState(); bulletAppState.setDebugEnabled(true); bulletAppState.setSpeed(0f); stateManager.attach(bulletAppState); PhysicsSpace space = bulletAppState.getPhysicsSpace(); float radius = 1f; CollisionShape sphere = new SphereCollisionShape(radius); CollisionShape box = new BoxCollisionShape(Vector3f.UNIT_XYZ); RigidBodyControl rbc = new RigidBodyControl(box); rbc.setEnabled(false); rbc.setPhysicsSpace(space); rootNode.addControl(rbc); BetterCharacterControl bcc = new BetterCharacterControl(radius, 4f, 1f); bcc.setEnabled(false); bcc.setPhysicsSpace(space); rootNode.addControl(bcc); GhostControl gc = new GhostControl(sphere); gc.setEnabled(false); gc.setPhysicsSpace(space); rootNode.addControl(gc); } }
@Override public void simpleInitApp() { stateManager.attach(bulletAppState); initCrossHair(); Spatial s = assetManager.loadModel("Models/Elephant/Elephant.mesh.xml"); s.setLocalScale(0.1f); CollisionShape collisionShape = CollisionShapeFactory.createMeshShape(s); Node n = new Node("elephant"); n.addControl(new RigidBodyControl(collisionShape, 1)); n.getControl(RigidBodyControl.class).setKinematic(true); bulletAppState.getPhysicsSpace().add(n); rootNode.attachChild(n); bulletAppState.setDebugEnabled(true); }
public void setupJoint() { Node holderNode=PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f( .1f, .1f, .1f)),0); holderNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f,0,0f)); rootNode.attachChild(holderNode); getPhysicsSpace().add(holderNode); Node hammerNode=PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f( .3f, .3f, .3f)),1); hammerNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f,-1,0f)); rootNode.attachChild(hammerNode); getPhysicsSpace().add(hammerNode); joint=new HingeJoint(holderNode.getControl(RigidBodyControl.class), hammerNode.getControl(RigidBodyControl.class), Vector3f.ZERO, new Vector3f(0f,-1,0f), Vector3f.UNIT_Z, Vector3f.UNIT_Z); getPhysicsSpace().add(joint); }