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

The following examples show how to use com.jme3.math.Vector3f#mult() . 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: LeaderFollowingBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @see AbstractStrengthSteeringBehavior#calculateRawSteering()
 */
@Override
protected Vector3f calculateRawSteering() {
    Vector3f steer;
    float distanceBetwen = this.agent.distanceRelativeToGameEntity(this.getTarget());

    //See how far ahead we need to leed
    Vector3f fullProjectedLocation = this.getTarget().getPredictedPosition();
    Vector3f predictedPositionDiff = fullProjectedLocation.subtract(this.getTarget().getLocalTranslation());
    Vector3f projectedLocation = this.getTarget().getLocalTranslation().add(predictedPositionDiff.mult(
            this.calculateFocusFactor(distanceBetwen)));

    this.arriveBehavior.setSeekingPosition(projectedLocation);

    steer = this.arriveBehavior.calculateRawSteering();

    if (!(distanceBetwen > this.distanceToEvade) && !(this.getTarget().forwardness(this.agent) < FastMath.cos(this.minimumAngle))) { //Incorrect angle and Is in the proper distance to evade -> Evade the leader

        Vector3f arriveSteer = steer.mult(distanceBetwen / this.distanceToEvade);
        Vector3f evadeSteer = this.evadeBehavior.calculateRawSteering();
        evadeSteer.mult(this.distanceToEvade / (1 + distanceBetwen));
        steer = (new Vector3f()).add(arriveSteer).add(evadeSteer);
    }

    return steer;
}
 
Example 2
Source File: DynamicAnimControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Recalculate the total mass of the ragdoll. Also updates the location and
 * estimated velocity of the center of mass.
 */
private void recalculateCenter() {
    double massSum = 0.0;
    Vector3f locationSum = new Vector3f();
    Vector3f velocitySum = new Vector3f();
    Vector3f tmpVector = new Vector3f();
    List<PhysicsLink> links = listLinks(PhysicsLink.class);
    for (PhysicsLink link : links) {
        PhysicsRigidBody rigidBody = link.getRigidBody();
        float mass = rigidBody.getMass();
        massSum += mass;

        rigidBody.getPhysicsLocation(tmpVector);
        tmpVector.multLocal(mass);
        locationSum.addLocal(tmpVector);

        link.velocity(tmpVector);
        tmpVector.multLocal(mass);
        velocitySum.addLocal(tmpVector);
    }

    float invMass = (float) (1.0 / massSum);
    locationSum.mult(invMass, centerLocation);
    velocitySum.mult(invMass, centerVelocity);
    ragdollMass = (float) massSum;
}
 
Example 3
Source File: AbstractStrengthSteeringBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Calculates the steering force with the specified strength. <br><br>
 *
 * If the strength was not setted up it the return calculateSteering(), the
 * unmodified force.
 *
 * @return The steering force with the specified strength.
 */
@Override
protected Vector3f calculateSteering() {

    Vector3f strengthSteeringForce = calculateRawSteering();

    switch (this.type) {
        case SCALAR:
            strengthSteeringForce = strengthSteeringForce.mult(this.scalar);
            break;

        case AXIS:
            strengthSteeringForce.setX(strengthSteeringForce.getX() * this.x);
            strengthSteeringForce.setY(strengthSteeringForce.getY() * this.y);
            strengthSteeringForce.setZ(strengthSteeringForce.getZ() * this.z);
            break;

        case PLANE:
            strengthSteeringForce = this.plane.getClosestPoint(strengthSteeringForce).mult(this.scalar);
            break;

    }
    
    //if there is no steering force, than the steering vector is zero
    if (strengthSteeringForce.equals(Vector3f.NAN)) {
        strengthSteeringForce = Vector3f.ZERO.clone();
    }
    
    return strengthSteeringForce;
}
 
Example 4
Source File: BalancedCompoundSteeringBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @see
 * CompoundSteeringBehavior#calculatePartialForce(com.jme3.ai.agents.behaviors.npc.steering.AbstractSteeringBehavior)
 */
@Override
protected Vector3f calculatePartialForce(AbstractSteeringBehavior behavior) {
    this.calculateTotalForce();
    Vector3f partialForce = this.partialForces.get(this.numberOfPartialForcesAlreadyCalculated);

    if (this.strengthIsBalanced && this.totalForce.length() > 0) {
        partialForce.mult(partialForce.length()
                / this.totalForce.length());
    }

    this.partialForceCalculated();
    return partialForce;
}
 
Example 5
Source File: TestIssue1283.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add a projectile to the scene and physics space. Its initial position and
 * velocity are determined by the camera the and mouse pointer.
 */
private void launchProjectile() {
    Vector2f screenXY = inputManager.getCursorPosition();
    float nearZ = 0f;
    Vector3f nearLocation = cam.getWorldCoordinates(screenXY, nearZ);
    float farZ = 1f;
    Vector3f farLocation = cam.getWorldCoordinates(screenXY, farZ);
    Vector3f direction = farLocation.subtract(nearLocation);
    direction.normalizeLocal();

    Geometry geometry = new Geometry("projectile", projectileMesh);
    rootNode.attachChild(geometry);
    geometry.setLocalTranslation(nearLocation);
    geometry.setMaterial(projectileMaterial);

    float mass = 1f;
    RigidBodyControl physicsControl = new RigidBodyControl(mass);
    geometry.addControl(physicsControl);

    physicsControl.setCcdMotionThreshold(0.01f);
    physicsControl.setCcdSweptSphereRadius(projectileRadius);
    physicsControl.setCollisionGroup(
            PhysicsCollisionObject.COLLISION_GROUP_02);
    physicsControl.setCollideWithGroups(
            PhysicsCollisionObject.COLLISION_GROUP_02);
    physicsControl.setRestitution(0.8f);

    float projectileSpeed = 250f; // physics-space units per second
    Vector3f initialVelocity = direction.mult(projectileSpeed);
    physicsControl.setLinearVelocity(initialVelocity);

    physicsSpace.add(physicsControl);
}
 
Example 6
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 7
Source File: TransparentComparator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Calculates the distance from a spatial to the camera. Distance is a
 * squared distance.
 *
 * @param spat
 *            Spatial to distancize.
 * @return Distance from Spatial to camera.
 */
private float distanceToCam2(Geometry spat){
    if (spat == null)
        return Float.NEGATIVE_INFINITY;

    if (spat.queueDistance != Float.NEGATIVE_INFINITY)
        return spat.queueDistance;

    Vector3f camPosition = cam.getLocation();
    Vector3f viewVector = cam.getDirection();
    Vector3f spatPosition = null;

    if (spat.getWorldBound() != null){
        spatPosition = spat.getWorldBound().getCenter();
    }else{
        spatPosition = spat.getWorldTranslation();
    }

    spatPosition.subtract(camPosition, tempVec);
    spat.queueDistance = tempVec.dot(tempVec);

    float retval = Math.abs(tempVec.dot(viewVector)
            / viewVector.dot(viewVector));
    viewVector.mult(retval, tempVec);

    spat.queueDistance = tempVec.length();

    return spat.queueDistance;
}
 
Example 8
Source File: TransparentComparator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Calculates the distance from a spatial to the camera. Distance is a
 * squared distance.
 *
 * @param spat
 *            Spatial to distancize.
 * @return Distance from Spatial to camera.
 */
private float distanceToCam2(Geometry spat){
    if (spat == null)
        return Float.NEGATIVE_INFINITY;

    if (spat.queueDistance != Float.NEGATIVE_INFINITY)
        return spat.queueDistance;

    Vector3f camPosition = cam.getLocation();
    Vector3f viewVector = cam.getDirection();
    Vector3f spatPosition = null;

    if (spat.getWorldBound() != null){
        spatPosition = spat.getWorldBound().getCenter();
    }else{
        spatPosition = spat.getWorldTranslation();
    }

    spatPosition.subtract(camPosition, tempVec);
    spat.queueDistance = tempVec.dot(tempVec);

    float retval = Math.abs(tempVec.dot(viewVector)
            / viewVector.dot(viewVector));
    viewVector.mult(retval, tempVec);

    spat.queueDistance = tempVec.length();

    return spat.queueDistance;
}
 
Example 9
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;
}