com.jme3.bullet.PhysicsSpace Java Examples

The following examples show how to use com.jme3.bullet.PhysicsSpace. 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: RangeOfMotion.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read the maximum rotation around the indexed axis.
 *
 * @param axisIndex which axis: 0→X, 1→Y, 2→Z
 *
 * @return the rotation angle (in radians)
 */
public float getMaxRotation(int axisIndex) {
    float result;
    switch (axisIndex) {
        case PhysicsSpace.AXIS_X:
            result = maxX;
            break;
        case PhysicsSpace.AXIS_Y:
            result = maxY;
            break;
        case PhysicsSpace.AXIS_Z:
            result = maxZ;
            break;
        default:
            String msg = String.format("axisIndex=%d", axisIndex);
            throw new IllegalArgumentException(msg);
    }

    return result;
}
 
Example #2
Source File: PhysicsVehicle.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Used internally, creates the actual vehicle constraint when vehicle is
 * added to physics space.
 *
 * @param space which physics space
 */
public void createVehicle(PhysicsSpace space) {
    physicsSpace = space;
    if (space == null) {
        return;
    }
    if (space.getSpaceId() == 0) {
        throw new IllegalStateException("Physics space is not initialized!");
    }
    if (rayCasterId != 0) {
        Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Clearing RayCaster {0}", Long.toHexString(rayCasterId));
        Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Clearing Vehicle {0}", Long.toHexString(vehicleId));
        finalizeNative(rayCasterId, vehicleId);
    }
    rayCasterId = createVehicleRaycaster(objectId, space.getSpaceId());
    Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Created RayCaster {0}", Long.toHexString(rayCasterId));
    vehicleId = createRaycastVehicle(objectId, rayCasterId);
    Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Created Vehicle {0}", Long.toHexString(vehicleId));
    setCoordinateSystem(vehicleId, 0, 1, 2);
    for (VehicleWheel wheel : wheels) {
        wheel.setVehicleId(vehicleId, addWheel(vehicleId, wheel.getLocation(), wheel.getDirection(), wheel.getAxle(), wheel.getRestLength(), wheel.getRadius(), tuning, wheel.isFrontWheel()));
    }
}
 
Example #3
Source File: RangeOfMotion.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Instantiate a preset for rotation on a single axis.
 *
 * @param axisIndex which axis: 0→X, 1→Y, 2→Z
 */
public RangeOfMotion(int axisIndex) {
    switch (axisIndex) {
        case PhysicsSpace.AXIS_X:
            maxX = 1f;
            minX = -1f;
            break;
        case PhysicsSpace.AXIS_Y:
            maxY = 1f;
            minY = -1f;
            break;
        case PhysicsSpace.AXIS_Z:
            maxZ = 1f;
            minZ = -1f;
            break;
        default:
            String msg = String.format("axisIndex=%d", axisIndex);
            throw new IllegalArgumentException(msg);
    }
}
 
Example #4
Source File: PhysicsRigidBody.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Builds/rebuilds the phyiscs body when parameters have changed
 */
protected void rebuildRigidBody() {
    boolean removed = false;
    if (collisionShape instanceof MeshCollisionShape && mass != 0) {
        throw new IllegalStateException("Dynamic rigidbody can not have mesh collision shape!");
    }
    if (objectId != 0) {
        if (isInWorld(objectId)) {
            PhysicsSpace.getPhysicsSpace().remove(this);
            removed = true;
        }
        Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Clearing RigidBody {0}", Long.toHexString(objectId));
        finalizeNative(objectId);
    }
    preRebuild();
    objectId = createRigidBody(mass, motionState.getObjectId(), collisionShape.getObjectId());
    Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Created RigidBody {0}", Long.toHexString(objectId));
    postRebuild();
    if (removed) {
        PhysicsSpace.getPhysicsSpace().add(this);
    }
}
 
Example #5
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 #6
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 #7
Source File: DacLinks.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Remove all managed physics objects from the PhysicsSpace.
 */
@Override
protected void removePhysics(PhysicsSpace space) {
    assert added;

    PhysicsRigidBody rigidBody;
    if (torsoLink != null) {
        rigidBody = torsoLink.getRigidBody();
        space.remove(rigidBody);
    }

    for (BoneLink boneLink : boneLinks.values()) {
        rigidBody = boneLink.getRigidBody();
        space.remove(rigidBody);

        PhysicsJoint joint = boneLink.getJoint();
        space.remove(joint);
    }
}
 
Example #8
Source File: ConeCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void createShape() {
    if (axis == PhysicsSpace.AXIS_X) {
        cShape = new ConeShapeX(radius, height);
    } else if (axis == PhysicsSpace.AXIS_Y) {
        cShape = new ConeShape(radius, height);
    } else if (axis == PhysicsSpace.AXIS_Z) {
        cShape = new ConeShapeZ(radius, height);
    } else {
        throw new UnsupportedOperationException("Unexpected axis: " + axis);
    }
    cShape.setLocalScaling(Converter.convert(getScale()));
    cShape.setMargin(margin);
}
 
Example #9
Source File: CharacterControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setPhysicsSpace(PhysicsSpace space) {
    if (space == null) {
        if (this.space != null) {
            this.space.removeCollisionObject(this);
            added = false;
        }
    } else {
        if (this.space == space) {
            return;
        }
        space.addCollisionObject(this);
        added = true;
    }
    this.space = space;
}
 
Example #10
Source File: GhostControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setPhysicsSpace(PhysicsSpace space) {
    if (space == null) {
        if (this.space != null) {
            this.space.removeCollisionObject(this);
            added = false;
        }
    } else {
        if (this.space == space) {
            return;
        }
        space.addCollisionObject(this);
        added = true;
    }
    this.space = space;
}
 
Example #11
Source File: ConeCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a new cone collision shape with the given height, radius and default Y axis.
 *
 * @param radius The radius of the cone in world units.
 * @param height The height of the cone in world units.
 */
public ConeCollisionShape(float radius, float height) {
    this.radius = radius;
    this.height = height;
    this.axis = PhysicsSpace.AXIS_Y;
    createShape();
}
 
Example #12
Source File: BombControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void setPhysicsSpace(PhysicsSpace space) {
    super.setPhysicsSpace(space);
    if (space != null) {
        space.addCollisionListener(this);
    }
}
 
Example #13
Source File: DacLinks.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Callback from Bullet, invoked just before the physics is stepped. A good
 * time to clear/apply forces.
 *
 * @param space the space that is about to be stepped (not null)
 * @param timeStep the time per physics step (in seconds, &ge;0)
 */
@Override
public void prePhysicsTick(PhysicsSpace space, float timeStep) {
    assert space == getPhysicsSpace();

    torsoLink.preTick(timeStep);
    for (BoneLink boneLink : boneLinkList) {
        boneLink.preTick(timeStep);
    }
}
 
Example #14
Source File: DacLinks.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Callback from Bullet, invoked just after the physics has been stepped.
 * Used to re-activate any deactivated rigid bodies.
 *
 * @param space the space that was just stepped (not null)
 * @param timeStep the time per physics step (in seconds, &ge;0)
 */
@Override
public void physicsTick(PhysicsSpace space, float timeStep) {
    assert space == getPhysicsSpace();

    torsoLink.postTick();
    for (BoneLink boneLink : boneLinkList) {
        boneLink.postTick();
    }

    isReady = true;
}
 
Example #15
Source File: ConeCollisionShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void createShape() {
    if (axis == PhysicsSpace.AXIS_X) {
        cShape = new ConeShapeX(radius, height);
    } else if (axis == PhysicsSpace.AXIS_Y) {
        cShape = new ConeShape(radius, height);
    } else if (axis == PhysicsSpace.AXIS_Z) {
        cShape = new ConeShapeZ(radius, height);
    }
    cShape.setLocalScaling(Converter.convert(getScale()));
    cShape.setMargin(margin);
}
 
Example #16
Source File: PhysicsHoverControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void setPhysicsSpace(PhysicsSpace space) {
    createVehicle(space);
    if (space == null) {
        if (this.space != null) {
            this.space.removeCollisionObject(this);
            this.space.removeTickListener(this);
        }
        this.space = space;
    } else {
        space.addCollisionObject(this);
        space.addTickListener(this);
    }
    this.space = space;
}
 
Example #17
Source File: RangeOfMotion.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Apply this preset to the specified joint.
 *
 * @param joint where to apply this preset (not null, modified)
 */
public void setupJoint(SixDofJoint joint) {
    Vector3f lower = new Vector3f(minX, minY, minZ);
    Vector3f upper = new Vector3f(maxX, maxY, maxZ);

    RotationalLimitMotor rotX
            = joint.getRotationalLimitMotor(PhysicsSpace.AXIS_X);
    rotX.setLoLimit(lower.x);
    rotX.setHiLimit(upper.x);

    RotationalLimitMotor rotY
            = joint.getRotationalLimitMotor(PhysicsSpace.AXIS_Y);
    rotY.setLoLimit(lower.y);
    rotY.setHiLimit(upper.y);

    RotationalLimitMotor rotZ
            = joint.getRotationalLimitMotor(PhysicsSpace.AXIS_Z);
    rotZ.setLoLimit(lower.z);
    rotZ.setHiLimit(upper.z);

    joint.setAngularLowerLimit(lower);
    joint.setAngularUpperLimit(upper);

    for (int i = 0; i < 3; ++i) {
        RotationalLimitMotor rot = joint.getRotationalLimitMotor(i);
        rot.setMaxMotorForce(1e8f);
        rot.setMaxLimitForce(1e9f);
    }

    joint.setLinearLowerLimit(translateIdentity);
    joint.setLinearUpperLimit(translateIdentity);

    TranslationalLimitMotor tra = joint.getTranslationalLimitMotor();
    tra.setLowerLimit(translateIdentity);
    tra.setUpperLimit(translateIdentity);
}
 
Example #18
Source File: MapView.java    From OpenRTS with MIT License 5 votes vote down vote up
public MapView(Node rootNode, Node gui, PhysicsSpace physicsSpace, AssetManager am, ViewPort vp) {

		this.setRootNode(rootNode);
		this.physicsSpace = physicsSpace;
		gui.attachChild(guiNode);

		this.assetManager = am;
		this.vp = vp;

		lightDrawer = new LightDrawer(this, am, rootNode, vp);

		createSky();
		EventManager.register(this);
	}
 
Example #19
Source File: DynamicAnimControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add all managed physics objects to the PhysicsSpace.
 */
@Override
protected void addPhysics(PhysicsSpace space) {
    super.addPhysics(space);

    space.addCollisionListener(this);
    space.addTickListener(this);
}
 
Example #20
Source File: KinematicRagdollControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * set the physic space to this ragdoll
 * @param space 
 */
public void setPhysicsSpace(PhysicsSpace space) {
    if (space == null) {
        removeFromPhysicsSpace();
        this.space = space;
    } else {
        if (this.space == space) {
            return;
        }
        this.space = space;
        addToPhysicsSpace();
        this.space.addCollisionListener(this);
    }
}
 
Example #21
Source File: ConeCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Serialize this shape, for example when saving to a J3O file.
 *
 * @param ex exporter (not null)
 * @throws IOException from exporter
 */
@Override
public void write(JmeExporter ex) throws IOException {
    super.write(ex);
    OutputCapsule capsule = ex.getCapsule(this);
    capsule.write(radius, "radius", 0.5f);
    capsule.write(height, "height", 0.5f);
    capsule.write(axis, "axis", PhysicsSpace.AXIS_Y);
}
 
Example #22
Source File: BetterCharacterControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add all managed physics objects to the specified space.
 *
 * @param space which physics space to add to (not null)
 */
@Override
protected void addPhysics(PhysicsSpace space) {
    space.getGravity(localUp).normalizeLocal().negateLocal();
    updateLocalCoordinateSystem();

    space.addCollisionObject(rigidBody);
    space.addTickListener(this);
}
 
Example #23
Source File: CharacterControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setPhysicsSpace(PhysicsSpace space) {
    if (space == null) {
        if (this.space != null) {
            this.space.removeCollisionObject(this);
            added = false;
        }
    } else {
        if (this.space == space) {
            return;
        }
        space.addCollisionObject(this);
        added = true;
    }
    this.space = space;
}
 
Example #24
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 #25
Source File: RigidBodyControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setPhysicsSpace(PhysicsSpace space) {
    if (space == null) {
        if (this.space != null) {
            this.space.removeCollisionObject(this);
            added = false;
        }
    } else {
        if(this.space==space) return;
        space.addCollisionObject(this);
        added = true;
    }
    this.space = space;
}
 
Example #26
Source File: RigidBodyControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setPhysicsSpace(PhysicsSpace space) {
    if (space == null) {
        if (this.space != null) {
            this.space.removeCollisionObject(this);
            added = false;
        }
    } else {
        if(this.space==space) return;
        space.addCollisionObject(this);
        added = true;
    }
    this.space = space;
}
 
Example #27
Source File: PhysicsVehicle.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Used internally, creates the actual vehicle constraint when vehicle is added to phyicsspace
 */
public void createVehicle(PhysicsSpace space) {
    physicsSpace = space;
    if (space == null) {
        return;
    }
    rayCaster = new DefaultVehicleRaycaster(space.getDynamicsWorld());
    vehicle = new RaycastVehicle(tuning, rBody, rayCaster);
    vehicle.setCoordinateSystem(0, 1, 2);
    for (VehicleWheel wheel : wheels) {
        wheel.setWheelInfo(vehicle.addWheel(Converter.convert(wheel.getLocation()), Converter.convert(wheel.getDirection()), Converter.convert(wheel.getAxle()),
                wheel.getRestLength(), wheel.getRadius(), tuning, wheel.isFrontWheel()));
    }
}
 
Example #28
Source File: VehicleControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setPhysicsSpace(PhysicsSpace space) {
//        createVehicle(space);
        if (space == null) {
            if (this.space != null) {
                this.space.removeCollisionObject(this);
                added = false;
            }
        } else {
            if(this.space==space) return;
            space.addCollisionObject(this);
            added = true;
        }
        this.space = space;
    }
 
Example #29
Source File: TestIssue883.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void simpleInitApp() {

    BulletAppState bulletAppState = new BulletAppState() {
        @Override
        public void physicsTick(PhysicsSpace space, float timeStep) {
            physicsTime += timeStep;
        }
    };
    bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
    stateManager.attach(bulletAppState);
}
 
Example #30
Source File: ConeCollisionShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * De-serialize this shape, for example when loading from a J3O file.
 *
 * @param im importer (not null)
 * @throws IOException from importer
 */
@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule capsule = im.getCapsule(this);
    radius = capsule.readFloat("radius", 0.5f);
    height = capsule.readFloat("height", 0.5f);
    axis = capsule.readInt("axis", PhysicsSpace.AXIS_Y);
    createShape();
}