Java Code Examples for com.jme3.math.Vector3f#normalize()

The following examples show how to use com.jme3.math.Vector3f#normalize() . 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: WorldOfInception.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void scaleAsChild(float percent, Vector3f dist) {
    float childScale = mapValue(percent, 1.0f / poiRadius, 1);
    Vector3f distToHorizon = dist.normalize();
    Vector3f scaledDistToHorizon = distToHorizon.mult(childScale * poiRadius);
    Vector3f rootOff = dist.add(scaledDistToHorizon);
    debugTools.setBlueArrow(Vector3f.ZERO, rootOff);
    getRootNode().setLocalScale(childScale);
    getRootNode().setLocalTranslation(rootOff);
    //prepare player position already
    Vector3f playerPosition = dist.normalize().mult(-poiRadius);
    setPlayerPosition(playerPosition);
}
 
Example 2
Source File: Arrow.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates an arrow mesh with the given extent.
 * The arrow will start at the origin (0,0,0) and finish
 * at the given extent.
 * 
 * @param extent Extent of the arrow from origin
 */
public Arrow(Vector3f extent) {
    float len = extent.length();
    Vector3f dir = extent.normalize();

    tempQuat.lookAt(dir, Vector3f.UNIT_Y);
    tempQuat.normalizeLocal();

    float[] newPositions = new float[positions.length];
    for (int i = 0; i < positions.length; i += 3) {
        Vector3f vec = tempVec.set(positions[i],
                positions[i + 1],
                positions[i + 2]);
        vec.multLocal(len);
        tempQuat.mult(vec, vec);

        newPositions[i] = vec.getX();
        newPositions[i + 1] = vec.getY();
        newPositions[i + 2] = vec.getZ();
    }

    setBuffer(Type.Position, 3, newPositions);
    setBuffer(Type.Index, 2,
            new short[]{
                0, 1,
                1, 2,
                1, 3,
                1, 4,
                1, 5,});
    setMode(Mode.Lines);

    updateBound();
    updateCounts();
}
 
Example 3
Source File: Arrow.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Creates an arrow mesh with the given extent.
 * The arrow will start at the origin (0,0,0) and finish
 * at the given extent.
 * 
 * @param extent Extent of the arrow from origin
 */
public Arrow(Vector3f extent) {
    float len = extent.length();
    Vector3f dir = extent.normalize();

    tempQuat.lookAt(dir, Vector3f.UNIT_Y);
    tempQuat.normalizeLocal();

    float[] newPositions = new float[positions.length];
    for (int i = 0; i < positions.length; i += 3) {
        Vector3f vec = tempVec.set(positions[i],
                positions[i + 1],
                positions[i + 2]);
        vec.multLocal(len);
        tempQuat.mult(vec, vec);

        newPositions[i] = vec.getX();
        newPositions[i + 1] = vec.getY();
        newPositions[i + 2] = vec.getZ();
    }

    setBuffer(Type.Position, 3, newPositions);
    setBuffer(Type.Index, 2,
            new short[]{
                0, 1,
                1, 2,
                1, 3,
                1, 4,
                1, 5,});
    setMode(Mode.Lines);

    updateBound();
    updateCounts();
}
 
Example 4
Source File: ObstacleAvoidanceBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @see AbstractSteeringBehavior#calculateSteering()
 */
@Override
protected Vector3f calculateRawSteering() {
    Vector3f nearestObstacleSteerForce = new Vector3f();

    if (this.agent.getVelocity() != null) {
        float agentVel = this.agent.getVelocity().length();
        float minDistanceToCollision = agentVel * timePerFrame * this.minTimeToCollision;

        // test all obstacles for intersection with my forward axis,
        // select the one whose intersection is nearest
        for (GameEntity obstacle : this.obstacles) {
            float distanceFromCenterToCenter = this.agent.distanceRelativeToGameEntity(obstacle);
            if (distanceFromCenterToCenter > this.minDistance) {
                break;
            }

            float distanceFromCenterToObstacleSuperf = distanceFromCenterToCenter - obstacle.getRadius();
            float distance = distanceFromCenterToObstacleSuperf - this.agent.getRadius();

            if (distanceFromCenterToObstacleSuperf < 0) {
                distanceFromCenterToObstacleSuperf = 0;
            }

            if (distance < 0) {
                distance = 0;
            }

            // if it is at least in the radius of the collision cylinder and we are facing the obstacle
            if (this.agent.forwardness(obstacle) > 0
                    && //Are we facing the obstacle ?
                    distance * distance
                    < ((minDistanceToCollision * minDistanceToCollision)
                    + (this.agent.getRadius() * this.agent.getRadius())) //Pythagoras Theorem
                    ) {
                Vector3f velocityNormalized = this.agent.getVelocity().normalize();
                Vector3f distanceVec = this.agent.offset(obstacle).normalize().mult(distanceFromCenterToObstacleSuperf);
                Vector3f projectedVector = velocityNormalized.mult(velocityNormalized.dot(distanceVec));

                Vector3f collisionDistanceOffset = projectedVector.subtract(distanceVec);

                if (collisionDistanceOffset.length() < this.agent.getRadius()) {
                    Vector3f collisionDistanceDirection;

                    if (!collisionDistanceOffset.equals(Vector3f.ZERO)) {
                        collisionDistanceDirection = collisionDistanceOffset.normalize();
                    } else {
                        collisionDistanceDirection = randomVectInPlane(this.agent.getVelocity(), this.agent.getLocalTranslation()).normalize();
                    }

                    Vector3f steerForce = collisionDistanceDirection.mult((this.agent.getRadius() - collisionDistanceOffset.length())
                            / this.agent.getRadius());

                    if (steerForce.length() > nearestObstacleSteerForce.length()) {
                        nearestObstacleSteerForce = steerForce;
                    }
                }
            }
        }
    }
    return nearestObstacleSteerForce;
}
 
Example 5
Source File: PathFollowBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @see AbstractStrengthSteeringBehavior#calculateRawSteering()
 */
@Override
protected Vector3f calculateRawSteering() {
    Vector3f steer = new Vector3f();

    if (active) {
        //Start to follow from the beginning
        if (this.nextSpineJoint < 0) {
            this.nextSpineJoint = 0;
        }

        if (this.nextSpineJoint < this.orderedPointsList.size()) {
            //Calculate the next exit
            Vector3f exitNormal;
            if (this.nextSpineJoint == 0) {
                exitNormal = this.orderedPointsList.get(this.nextSpineJoint + 1).subtract(this.orderedPointsList.get(this.nextSpineJoint));
            } else {
                exitNormal = this.orderedPointsList.get(this.nextSpineJoint).subtract(this.orderedPointsList.get(this.nextSpineJoint - 1));
            }

            this.nextExit = new Plane();
            this.nextExit.setOriginNormal(
                    this.orderedPointsList.get(this.nextSpineJoint),
                    exitNormal);

            Vector3f posProjection = this.nextExit.getClosestPoint(this.agent.getLocalTranslation());
            float distanceToCenter = posProjection.subtract(this.orderedPointsList.get(this.nextSpineJoint)).length();
            //chaeck if the agent is outside the path
            if (distanceToCenter > this.pathRadius) {
                //Move to the next spine and inside the path
                Vector3f moveToSpine = this.agent.offset(this.orderedPointsList.get(this.nextSpineJoint)).normalize();
                Vector3f moveToCenter = this.orderedPointsList.get(this.nextSpineJoint).subtract(posProjection).normalize();
                steer = moveToSpine.add(moveToCenter);
            } else {
                //Move through the path
                Vector3f moveThroughPathSteer = exitNormal.normalize();

                Vector3f predictedPos = this.agent.getPredictedPosition();
                Vector3f predictedOffsetFromNextCenter = predictedPos.subtract(this.orderedPointsList.get(this.nextSpineJoint));
                Vector3f projectionIntoSpine = this.orderedPointsList.get(this.nextSpineJoint).add(
                        exitNormal.mult(predictedOffsetFromNextCenter.dot(exitNormal) / exitNormal.lengthSquared()));

                Vector3f predictedOffset = predictedPos.subtract(projectionIntoSpine);
                Vector3f predictedOffsetFromSurface = predictedOffset.subtract(predictedOffset.normalize().mult(this.pathRadius));

                //Path containment
                if (predictedOffset.length() > this.pathRadius) {
                    moveThroughPathSteer = moveThroughPathSteer.add(predictedOffsetFromSurface.mult(cohesionStrength));
                }

                steer = moveThroughPathSteer;

                if (this.nextExit.whichSide(this.agent.getLocalTranslation()) == Side.Positive) {
                    this.nextSpineJoint++;
                }
            }
        } else {
            //The path has ended
            this.active = false;
        }
    }

    return steer;
}
 
Example 6
Source File: MoveBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void setMoveDirection(Vector3f moveDirection) {
    this.moveDirection = moveDirection.normalize();
}
 
Example 7
Source File: SimpleMoveBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 *
 * @param moveDirection movement vector
 */
public void setMoveDirection(Vector3f moveDirection) {
    this.moveDirection = moveDirection.normalize();
}