Search in sources :

Example 41 with Control

use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.

the class TestMotionPath method simpleInitApp.

@Override
public void simpleInitApp() {
    createScene();
    cam.setLocation(new Vector3f(8.4399185f, 11.189463f, 14.267577f));
    path = new MotionPath();
    path.addWayPoint(new Vector3f(10, 3, 0));
    path.addWayPoint(new Vector3f(10, 3, 10));
    path.addWayPoint(new Vector3f(-40, 3, 10));
    path.addWayPoint(new Vector3f(-40, 3, 0));
    path.addWayPoint(new Vector3f(-40, 8, 0));
    path.addWayPoint(new Vector3f(10, 8, 0));
    path.addWayPoint(new Vector3f(10, 8, 10));
    path.addWayPoint(new Vector3f(15, 8, 10));
    path.enableDebugShape(assetManager, rootNode);
    motionControl = new MotionEvent(teapot, path);
    motionControl.setDirectionType(MotionEvent.Direction.PathAndRotation);
    motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
    motionControl.setInitialDuration(10f);
    motionControl.setSpeed(2f);
    guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
    final BitmapText wayPointsText = new BitmapText(guiFont, false);
    wayPointsText.setSize(guiFont.getCharSet().getRenderedSize());
    guiNode.attachChild(wayPointsText);
    path.addListener(new MotionPathListener() {

        public void onWayPointReach(MotionEvent control, int wayPointIndex) {
            if (path.getNbWayPoints() == wayPointIndex + 1) {
                wayPointsText.setText(control.getSpatial().getName() + "Finished!!! ");
            } else {
                wayPointsText.setText(control.getSpatial().getName() + " Reached way point " + wayPointIndex);
            }
            wayPointsText.setLocalTranslation((cam.getWidth() - wayPointsText.getLineWidth()) / 2, cam.getHeight(), 0);
        }
    });
    flyCam.setEnabled(false);
    ChaseCamera chaser = new ChaseCamera(cam, teapot);
    //        motionControl.setSpeed(-3f);
    //        motionControl.setLoopMode(LoopMode.Loop);
    //        path.setCycle(true);
    // chaser.setEnabled(false);
    chaser.registerWithInput(inputManager);
    initInputs();
}
Also used : MotionPathListener(com.jme3.cinematic.MotionPathListener) BitmapText(com.jme3.font.BitmapText) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) ChaseCamera(com.jme3.input.ChaseCamera) MotionPath(com.jme3.cinematic.MotionPath) MotionEvent(com.jme3.cinematic.events.MotionEvent)

Example 42 with Control

use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.

the class ChaseCamera method updateCamera.

/**
     * Updates the camera, should only be called internally
     */
protected void updateCamera(float tpf) {
    if (enabled) {
        targetLocation.set(target.getWorldTranslation()).addLocal(lookAtOffset);
        if (smoothMotion) {
            //computation of target direction
            targetDir.set(targetLocation).subtractLocal(prevPos);
            float dist = targetDir.length();
            //Low pass filtering on the target postition to avoid shaking when physics are enabled.
            if (offsetDistance < dist) {
                //target moves, start chasing.
                chasing = true;
                //target moves, start trailing if it has to.
                if (trailingEnabled) {
                    trailing = true;
                }
                //target moves...
                targetMoves = true;
            } else {
                //We do not if the player is rotationg the cam
                if (targetMoves && !canRotate) {
                    if (targetRotation - rotation > trailingRotationInertia) {
                        targetRotation = rotation + trailingRotationInertia;
                    } else if (targetRotation - rotation < -trailingRotationInertia) {
                        targetRotation = rotation - trailingRotationInertia;
                    }
                }
                //Target stops
                targetMoves = false;
            }
            //the user is rotating the cam by dragging the mouse
            if (canRotate) {
                //reseting the trailing lerp factor
                trailingLerpFactor = 0;
                //stop trailing user has the control
                trailing = false;
            }
            if (trailingEnabled && trailing) {
                if (targetMoves) {
                    //computation if the inverted direction of the target
                    Vector3f a = targetDir.negate().normalizeLocal();
                    //the x unit vector
                    Vector3f b = Vector3f.UNIT_X;
                    //2d is good enough
                    a.y = 0;
                    //computation of the rotation angle between the x axis and the trail
                    if (targetDir.z > 0) {
                        targetRotation = FastMath.TWO_PI - FastMath.acos(a.dot(b));
                    } else {
                        targetRotation = FastMath.acos(a.dot(b));
                    }
                    if (targetRotation - rotation > FastMath.PI || targetRotation - rotation < -FastMath.PI) {
                        targetRotation -= FastMath.TWO_PI;
                    }
                    //if there is an important change in the direction while trailing reset of the lerp factor to avoid jumpy movements
                    if (targetRotation != previousTargetRotation && FastMath.abs(targetRotation - previousTargetRotation) > FastMath.PI / 8) {
                        trailingLerpFactor = 0;
                    }
                    previousTargetRotation = targetRotation;
                }
                //computing lerp factor
                trailingLerpFactor = Math.min(trailingLerpFactor + tpf * tpf * trailingSensitivity, 1);
                //computing rotation by linear interpolation
                rotation = FastMath.interpolateLinear(trailingLerpFactor, rotation, targetRotation);
                //if the rotation is near the target rotation we're good, that's over
                if (targetRotation + 0.01f >= rotation && targetRotation - 0.01f <= rotation) {
                    trailing = false;
                    trailingLerpFactor = 0;
                }
            }
            //linear interpolation of the distance while chasing
            if (chasing) {
                distance = temp.set(targetLocation).subtractLocal(cam.getLocation()).length();
                distanceLerpFactor = Math.min(distanceLerpFactor + (tpf * tpf * chasingSensitivity * 0.05f), 1);
                distance = FastMath.interpolateLinear(distanceLerpFactor, distance, targetDistance);
                if (targetDistance + 0.01f >= distance && targetDistance - 0.01f <= distance) {
                    distanceLerpFactor = 0;
                    chasing = false;
                }
            }
            //linear interpolation of the distance while zooming
            if (zooming) {
                distanceLerpFactor = Math.min(distanceLerpFactor + (tpf * tpf * zoomSensitivity), 1);
                distance = FastMath.interpolateLinear(distanceLerpFactor, distance, targetDistance);
                if (targetDistance + 0.1f >= distance && targetDistance - 0.1f <= distance) {
                    zooming = false;
                    distanceLerpFactor = 0;
                }
            }
            //linear interpolation of the rotation while rotating horizontally
            if (rotating) {
                rotationLerpFactor = Math.min(rotationLerpFactor + tpf * tpf * rotationSensitivity, 1);
                rotation = FastMath.interpolateLinear(rotationLerpFactor, rotation, targetRotation);
                if (targetRotation + 0.01f >= rotation && targetRotation - 0.01f <= rotation) {
                    rotating = false;
                    rotationLerpFactor = 0;
                }
            }
            //linear interpolation of the rotation while rotating vertically
            if (vRotating) {
                vRotationLerpFactor = Math.min(vRotationLerpFactor + tpf * tpf * rotationSensitivity, 1);
                vRotation = FastMath.interpolateLinear(vRotationLerpFactor, vRotation, targetVRotation);
                if (targetVRotation + 0.01f >= vRotation && targetVRotation - 0.01f <= vRotation) {
                    vRotating = false;
                    vRotationLerpFactor = 0;
                }
            }
            //computing the position
            computePosition();
            //setting the position at last
            cam.setLocation(pos.addLocal(lookAtOffset));
        } else {
            //easy no smooth motion
            vRotation = targetVRotation;
            rotation = targetRotation;
            distance = targetDistance;
            computePosition();
            cam.setLocation(pos.addLocal(lookAtOffset));
        }
        //keeping track on the previous position of the target
        prevPos.set(targetLocation);
        //the cam looks at the target
        cam.lookAt(targetLocation, initialUpVec);
    }
}
Also used : Vector3f(com.jme3.math.Vector3f)

Example 43 with Control

use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.

the class PhysicsHoverControl method prePhysicsTick.

public void prePhysicsTick(PhysicsSpace space, float f) {
    Vector3f angVel = getAngularVelocity();
    float rotationVelocity = angVel.getY();
    Vector3f dir = getForwardVector(tempVect2).multLocal(1, 0, 1).normalizeLocal();
    getLinearVelocity(tempVect3);
    Vector3f linearVelocity = tempVect3.multLocal(1, 0, 1);
    if (steeringValue != 0) {
        if (rotationVelocity < 1 && rotationVelocity > -1) {
            applyTorque(tempVect1.set(0, steeringValue, 0));
        }
    } else {
        // counter the steering value!
        if (rotationVelocity > 0.2f) {
            applyTorque(tempVect1.set(0, -mass * 20, 0));
        } else if (rotationVelocity < -0.2f) {
            applyTorque(tempVect1.set(0, mass * 20, 0));
        }
    }
    if (accelerationValue > 0) {
        // counter force that will adjust velocity
        // if we are not going where we want to go.
        // this will prevent "drifting" and thus improve control
        // of the vehicle
        float d = dir.dot(linearVelocity.normalize());
        Vector3f counter = dir.project(linearVelocity).normalizeLocal().negateLocal().multLocal(1 - d);
        applyForce(counter.multLocal(mass * 10), Vector3f.ZERO);
        if (linearVelocity.length() < 30) {
            applyForce(dir.multLocal(accelerationValue), Vector3f.ZERO);
        }
    } else {
        // counter the acceleration value
        if (linearVelocity.length() > FastMath.ZERO_TOLERANCE) {
            linearVelocity.normalizeLocal().negateLocal();
            applyForce(linearVelocity.mult(mass * 10), Vector3f.ZERO);
        }
    }
}
Also used : Vector3f(com.jme3.math.Vector3f)

Example 44 with Control

use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.

the class PhysicsTestHelper method createPhysicsTestNode.

/**
     * creates an empty node with a RigidBodyControl
     * @param manager
     * @param shape
     * @param mass
     * @return
     */
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;
}
Also used : Node(com.jme3.scene.Node) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl)

Example 45 with Control

use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.

the class TestBetterCharacter method simpleInitApp.

@Override
public void simpleInitApp() {
    //setup keyboard mapping
    setupKeys();
    // activate physics
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.setDebugEnabled(true);
    // init a physics test scene
    PhysicsTestHelper.createPhysicsTestWorldSoccer(rootNode, assetManager, bulletAppState.getPhysicsSpace());
    PhysicsTestHelper.createBallShooter(this, rootNode, bulletAppState.getPhysicsSpace());
    setupPlanet();
    // Create a node for the character model
    characterNode = new Node("character node");
    characterNode.setLocalTranslation(new Vector3f(4, 5, 2));
    // Add a character control to the node so we can add other things and
    // control the model rotation
    physicsCharacter = new BetterCharacterControl(0.3f, 2.5f, 8f);
    characterNode.addControl(physicsCharacter);
    getPhysicsSpace().add(physicsCharacter);
    // Load model, attach to character node
    Node model = (Node) assetManager.loadModel("Models/Jaime/Jaime.j3o");
    model.setLocalScale(1.50f);
    characterNode.attachChild(model);
    // Add character node to the rootNode
    rootNode.attachChild(characterNode);
    // Set forward camera node that follows the character, only used when
    // view is "locked"
    camNode = new CameraNode("CamNode", cam);
    camNode.setControlDir(ControlDirection.SpatialToCamera);
    camNode.setLocalTranslation(new Vector3f(0, 2, -6));
    Quaternion quat = new Quaternion();
    // These coordinates are local, the camNode is attached to the character node!
    quat.lookAt(Vector3f.UNIT_Z, Vector3f.UNIT_Y);
    camNode.setLocalRotation(quat);
    characterNode.attachChild(camNode);
    // Disable by default, can be enabled via keyboard shortcut
    camNode.setEnabled(false);
}
Also used : Quaternion(com.jme3.math.Quaternion) BulletAppState(com.jme3.bullet.BulletAppState) Node(com.jme3.scene.Node) CameraNode(com.jme3.scene.CameraNode) Vector3f(com.jme3.math.Vector3f) CameraNode(com.jme3.scene.CameraNode) BetterCharacterControl(com.jme3.bullet.control.BetterCharacterControl)

Aggregations

Vector3f (com.jme3.math.Vector3f)51 DirectionalLight (com.jme3.light.DirectionalLight)24 Material (com.jme3.material.Material)23 Quaternion (com.jme3.math.Quaternion)21 Node (com.jme3.scene.Node)19 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)18 Texture (com.jme3.texture.Texture)16 AnimControl (com.jme3.animation.AnimControl)15 ColorRGBA (com.jme3.math.ColorRGBA)15 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)15 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)14 Geometry (com.jme3.scene.Geometry)14 Spatial (com.jme3.scene.Spatial)13 DistanceLodCalculator (com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator)13 BulletAppState (com.jme3.bullet.BulletAppState)12 AbstractHeightMap (com.jme3.terrain.heightmap.AbstractHeightMap)11 ImageBasedHeightMap (com.jme3.terrain.heightmap.ImageBasedHeightMap)11 ArrayList (java.util.ArrayList)10 Box (com.jme3.scene.shape.Box)7 Animation (com.jme3.animation.Animation)5