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

📄 particle.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                BufferUtils.setInBuffer(position, parent.getParticleGeometry()
                        .getVertexBuffer(), startIndex);
                break;
            }
        }
    }

    /**
     * <p>
     * update position (using current position and velocity), color
     * (interpolating between start and end color), size (interpolating between
     * start and end size), spin (using parent's spin speed) and current age of
     * particle. If this particle's age is greater than its lifespan, it is set
     * to status DEAD.
     * </p>
     * <p>
     * Note that this only changes the parameters of the Particle, not the
     * geometry the particle is associated with.
     * </p>
     * 
     * @param secondsPassed
     *            number of seconds passed since last update.
     * @return true if this particle is not ALIVE (in other words, if it is
     *         ready to be reused.)
     */
    public boolean updateAndCheck(float secondsPassed) {
        if (status != Status.Alive) {
            return true;
        }
        currentAge += secondsPassed * 1000; // add ms time to age
        if (currentAge > lifeSpan) {
        	killParticle();
            return true;
        }

        position.scaleAdd(secondsPassed * 1000f, velocity, position);

        // get interpolated values from appearance ramp:
        parent.getRamp().getValuesAtAge(currentAge, lifeSpan, currColor,
                values, parent);

        // interpolate colors
        int verts = ParticleSystem.getVertsForParticleType(type);
        for (int x = 0; x < verts; x++)
            BufferUtils.setInBuffer(currColor, parent.getParticleGeometry()
                    .getColorBuffer(), startIndex + x);

        // check for tex animation
        int newTexIndex = parent.getTexAnimation().getTexIndexAtAge(currentAge, lifeSpan, parent);
        // Update tex coords if applicable
        if (currentTexIndex != newTexIndex) {
            // Only supported in Quad type for now.
            if (ParticleType.Quad.equals(parent.getParticleType())) {
                // determine side
                float side = FastMath.sqrt(parent.getTexQuantity());
                int index = newTexIndex;
                if (index >= parent.getTexQuantity()) {
                    index %= parent.getTexQuantity();
                }
                // figure row / col
                float row = side - (int)(index / side) - 1;
                float col = index % side;
                // set texcoords
                float sU = col/side, eU = (col+1)/side;
                float sV = row/side, eV = (row+1)/side;
                FloatBuffer texs = parent.getParticleGeometry().getTextureCoords(0).coords;
                texs.position(startIndex*2);
                texs.put(sU).put(sV);
                texs.put(sU).put(eV);
                texs.put(eU).put(sV);
                texs.put(eU).put(eV);
                texs.clear();
            }
        }
        
        return false;
    }

    public void killParticle() {
        setStatus(Status.Dead);
        
        currColor.a = 0;

        BufferUtils.populateFromBuffer(tempVec3, parent
                .getParticleGeometry().getVertexBuffer(), startIndex);
        int verts = ParticleSystem.getVertsForParticleType(type);
        for (int x = 0; x < verts; x++) {
            BufferUtils.setInBuffer(tempVec3, parent.getParticleGeometry()
                    .getVertexBuffer(), startIndex + x);
            BufferUtils.setInBuffer(currColor, parent.getParticleGeometry()
                    .getColorBuffer(), startIndex + x);
        }

	}

	/**
     * Resets current age to 0
     */
    public void resetAge() {
        currentAge = 0;
    }

    /**
     * @return the current age of the particle in ms
     */
    public int getCurrentAge() {
        return currentAge;
    }

    /**
     * @return the current position of the particle in space
     */
    public Vector3f getPosition() {
        return position;
    }

    /**
     * Set the position of the particle in space.
     * 
     * @param position
     *            the new position in world coordinates
     */
    public void setPosition(Vector3f position) {
        this.position.set(position);
    }

    /**
     * @return the current status of this particle.
     * @see Status
     */
    public Status getStatus() {
        return status;
    }

    /**
     * Set the status of this particle.
     * 
     * @param status
     *            new status of this particle
     * @see Status
     */
    public void setStatus(Status status) {
        this.status = status;
    }

    /**
     * @return the current velocity of this particle
     */
    public Vector3f getVelocity() {
        return velocity;
    }

    /**
     * Set the current velocity of this particle
     * 
     * @param velocity
     *            the new velocity
     */
    public void setVelocity(Vector3f velocity) {
        this.velocity.set(velocity);
    }

    /**
     * @return the current color applied to this particle
     */
    public ColorRGBA getCurrentColor() {
        return currColor;
    }

    /**
     * @return the start index of this particle in relation to where it exists
     *         in its parent's geometry data.
     */
    public int getStartIndex() {
        return startIndex;
    }

    /**
     * Set the starting index where this particle is represented in its parent's
     * geometry data
     * 
     * @param index
     */
    public void setStartIndex(int index) {
        this.startIndex = index;
    }

    /**
     * @return the mass of this particle. Only used by ParticleInfluences such
     *         as drag.
     */
    public float getMass() {
        return values[VAL_CURRENT_MASS];
    }

    /**
     * @return the inverse mass of this particle. Often useful for skipping
     *         constant division by mass calculations. If the mass is 0, the
     *         inverse mass is considered to be positive infinity. Conversely,
     *         if the mass is positive infinity, the inverse is 0. The inverse
     *         of negative infinity is considered to be -0.
     */
    public float getInvMass() {
        float mass = values[VAL_CURRENT_MASS];
        if (mass == 0)
            return Float.POSITIVE_INFINITY;
        else if (mass == Float.POSITIVE_INFINITY)
            return 0;
        else if (mass == Float.NEGATIVE_INFINITY)
            return -0;
        else
            return 1f / mass;
    }

    /**
     * Sets a triangle model to use for particle calculations when using
     * particle type ParticleType.GeomMesh. The particle will maintain the
     * triangle's ratio and plane of orientation. It will spin (if applicable)
     * around the triangle's normal axis. The triangle should already have its
     * center and normal fields calculated before calling this method.
     * 
     * @param t
     *            the triangle to model this particle after.
     */
    public void setTriangleModel(Triangle t) {
        this.triModel = t;
    }

    /**
     * @return the triangle model used by this particle
     * @see #setTriangleModel(Triangle)
     */
    public Triangle getTriangleModel() {
        return this.triModel;
    }

    // /////
    // Savable interface methods
    // /////

    public void write(JMEExporter e) throws IOException {
        OutputCapsule capsule = e.getCapsule(this);
        capsule.write(startIndex, "startIndex", 0);
        capsule.write(position, "position", Vector3f.ZERO);
        capsule.write(status, "status", Status.Available);
        capsule.write(lifeSpan, "lifeSpan", 0);
        capsule.write(currentAge, "currentAge", 0);
        capsule.write(parent, "parent", null);
        capsule.write(velocity, "velocity", Vector3f.UNIT_XYZ);
        capsule.write(type, "type", ParticleSystem.ParticleType.Quad);
    }

    public void read(JMEImporter e) throws IOException {
        InputCapsule capsule = e.getCapsule(this);
        startIndex = capsule.readInt("startIndex", 0);
        position = (Vector3f) capsule.readSavable("position", Vector3f.ZERO
                .clone());
        status = capsule.readEnum("status", Status.class, Status.Available);
        lifeSpan = capsule.readFloat("lifeSpan", 0);
        currentAge = capsule.readInt("currentAge", 0);
        parent = (ParticleSystem) capsule.readSavable("parent", null);
        velocity = (Vector3f) capsule.readSavable("velocity", Vector3f.UNIT_XYZ
                .clone());
        type = capsule.readEnum("type", ParticleSystem.ParticleType.class,
                ParticleSystem.ParticleType.Quad);
    }

    public Class<? extends Particle> getClassTag() {
        return this.getClass();
    }
}

⌨️ 快捷键说明

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