⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleparticleinfluencefactory.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jmex.effects.particles;

import java.io.IOException;

import com.jme.math.FastMath;
import com.jme.math.Line;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;

/**
 * <code>SimpleParticleForceFactory</code>
 * @author Joshua Slack
 * @version $Id: SimpleParticleInfluenceFactory.java,v 1.7 2007/09/21 15:45:32 nca Exp $
 */
public final class SimpleParticleInfluenceFactory {

    public static class BasicWind extends ParticleInfluence {
        private float strength;
        private Vector3f windDirection;
        private boolean random, rotateWithScene;
        private Vector3f vector = new Vector3f(); 
        
        public BasicWind() {
        }
        
        public BasicWind(float windStr, Vector3f windDir, boolean addRandom,
            boolean rotateWithScene) {
            strength = windStr;
            windDirection = windDir;
            random = addRandom;
            this.rotateWithScene = rotateWithScene;
        }
        
        public float getStrength() {
            return strength;
        }
        
        public void setStrength(float windStr) {
            strength = windStr;
        }
        
        public Vector3f getWindDirection() {
            return windDirection;
        }
        
        public void setWindDirection(Vector3f windDir) {
            windDirection = windDir;
        }

        public boolean isRandom() {
            return random;
        }
        
        public void setRandom(boolean addRandom) {
            random = addRandom;
        }
        
        public boolean isRotateWithScene() {
            return rotateWithScene;
        }
        
        public void setRotateWithScene(boolean rotateWithScene) {
            this.rotateWithScene = rotateWithScene;
        }
        
        public void prepare(ParticleSystem system) {
            vector.set(windDirection);
            if (rotateWithScene) {
                system.getEmitterTransform().multNormal(vector);
            }
        }
        
        public void apply(float dt, Particle p, int index) {
            float tStr = (random ? FastMath.nextRandomFloat() * strength : strength);
            p.getVelocity().scaleAdd(tStr * dt, vector, p.getVelocity());
        }
        
        public void write(JMEExporter e) throws IOException {
            super.write(e);
            OutputCapsule capsule = e.getCapsule(this);
            capsule.write(strength, "strength", 1f);
            capsule.write(windDirection, "windDirection", Vector3f.UNIT_X);
            capsule.write(random, "random", false);
            capsule.write(rotateWithScene, "rotateWithScene", true);
        }

        public void read(JMEImporter e) throws IOException {
            super.read(e);
            InputCapsule capsule = e.getCapsule(this);
            strength = capsule.readFloat("strength", 1f);
            windDirection = (Vector3f)capsule.readSavable("windDirection",
                Vector3f.UNIT_X.clone());
            random = capsule.readBoolean("random", false);
            rotateWithScene = capsule.readBoolean("rotateWithScene", true);
        }

        public Class getClassTag() {
            return this.getClass();
        }
    }
    
    public static class BasicGravity extends ParticleInfluence {
        private Vector3f gravity;
        private boolean rotateWithScene;
        private Vector3f vector = new Vector3f();
        
        public BasicGravity() {
        }
        
        public BasicGravity(Vector3f gravForce, boolean rotateWithScene) {
            gravity = new Vector3f(gravForce);
            this.rotateWithScene = rotateWithScene;
        }
        
        public Vector3f getGravityForce() {
            return gravity;
        }
        
        public void setGravityForce(Vector3f gravForce) {
            gravity = gravForce;
        }
        
        public boolean isRotateWithScene() {
            return rotateWithScene;
        }
        
        public void setRotateWithScene(boolean rotateWithScene) {
            this.rotateWithScene = rotateWithScene;
        }
        
        public void prepare(ParticleSystem system) {
            vector.set(gravity);
            if (rotateWithScene) {
                system.getEmitterTransform().multNormal(vector);
            }
        }
        
        public void apply(float dt, Particle p, int index) {
            p.getVelocity().scaleAdd(dt, vector, p.getVelocity());
        }
    
        public void write(JMEExporter e) throws IOException {
            super.write(e);
            OutputCapsule capsule = e.getCapsule(this);
            capsule.write(gravity, "gravity", Vector3f.ZERO);
            capsule.write(rotateWithScene, "rotateWithScene", true);
        }

        public void read(JMEImporter e) throws IOException {
            super.read(e);
            InputCapsule capsule = e.getCapsule(this);
            gravity = (Vector3f)capsule.readSavable("gravity",
                Vector3f.ZERO.clone());
            rotateWithScene = capsule.readBoolean("rotateWithScene", true);
        }
        
        public Class getClassTag() {
            return this.getClass();
        }
    }
    
    public static class BasicDrag extends ParticleInfluence {
        private Vector3f velocity = new Vector3f();
        private float dragCoefficient;
        
        public BasicDrag() {
        }
        
        public BasicDrag(float dragCoef) {
            dragCoefficient = dragCoef;
        }
        
        public float getDragCoefficient() {
            return dragCoefficient;
        }
        
        public void setDragCoefficient(float dragCoef) {
            dragCoefficient = dragCoef;
        }
        
        public void apply(float dt, Particle p, int index) {
            // viscous drag
            velocity.set(p.getVelocity());
            p.getVelocity().addLocal(velocity.multLocal(-dragCoefficient * dt * p.getInvMass()));
        }
    
        public void write(JMEExporter e) throws IOException {
            super.write(e);
            OutputCapsule capsule = e.getCapsule(this);
            capsule.write(dragCoefficient, "dragCoefficient", 1f);
        }

        public void read(JMEImporter e) throws IOException {
            super.read(e);
            InputCapsule capsule = e.getCapsule(this);
            dragCoefficient = capsule.readFloat("dragCoefficient", 1f);
        }
        
        public Class getClassTag() {
            return this.getClass();
        }
    }
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -