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

📄 fountaineffect.java

📁 这是有关J2ME 3D编程的源程序
💻 JAVA
字号:
import java.util.Random;

/*
 * Created on 2005-aug-31
 */

/**
 * Creates a nice fountain effect for the particles, that shoots particles
 * in a certain direction, determined by its angle. The angle can be changed in real-time.
 */
public class FountainEffect extends BitmapParticleEffect
{
    // The angle of particle emission
    private int angle = 90;
    
    // The sine and cosine of the current angle
    private float[] trig = {1.0f, 0.0f};
    
    // The emitting origin
    private float[] pos = {0.0f, 0.0f, 0.0f};
    
    // The randomizer
    Random rand = null;
    
    /**
     * @param angle The angle of particle emission
     */
    public FountainEffect(int angle)
    {
        // Init the bitmap
        super("/res/particle.png", 0.05f);
        
        // Set the angle
        setAngle(angle);
        
        // Get randomizer
        rand = new Random();
    }

    /**
     * @see ParticleEffect#init(Particle)
     */
    public void init(Particle p)
    {
        // Set the particle's life
        p.setLife(1.0f);
        
        // Set the particle's position
        p.setPos(pos);
        
        // Create the particle's velocties
        float[] vel = new float[3];
        
        // We want velocities from 0.2f to 1.0f
        float xyvel = rand.nextFloat() * 0.8f + 0.2f;
        
        // We want the particle to die slowly
        p.setDegradation(xyvel / 18);
        
        // Set velocities according to trigonometry with a small deviation
        vel[0] = xyvel * trig[1] + rand.nextFloat() * 0.125f - 0.0625f;
        vel[1] = xyvel * trig[0] + rand.nextFloat() * 0.125f - 0.0625f;
        
        // No movement in depth
        vel[2] = 0.0f;
        
        // Set the velocity
        p.setVel(vel);
        
        // Set the random color
        int r = (int)(120 * rand.nextFloat()) + 135;
        int g = (int)(120 * rand.nextFloat()) + 135;
        int b = (int)(120 * rand.nextFloat()) + 135;
        int col = (r << 16) | (g << 8) | b;
        p.setColor(col);
    }

    /**
     * @see ParticleEffect#update(Particle)
     */
    public void update(Particle p)
    {
        // Simply update position
        float[] ppos = p.getPos();
        float[] vel = p.getVel();
        ppos[0] += vel[0];
        ppos[1] += vel[1];
        ppos[2] += vel[2];
        
        // Update life
        p.setLife(p.getLife() - p.getDegradation());
        
        // Check life. If it is dead, we just reinit it
        if(p.getLife() < -0.001f)
        {
            init(p);
        }
    }

    /**
     * @param angle The angle to set.
     */
    public void setAngle(int angle) {
        this.angle = angle;
        trig[0] = (float)Math.sin(Math.toRadians(angle));
        trig[1] = (float)Math.cos(Math.toRadians(angle));
    }

    /**
     * @return Returns the angle.
     */
    public int getAngle() {
        return angle;
    }

    /**
     * @param pos The pos to set.
     */
    void setEmittingOrigin(float[] pos) {
        this.pos = pos;
    }

    /**
     * @return Returns the pos.
     */
    float[] getEmittingOrigin() {
        return pos;
    }

}

⌨️ 快捷键说明

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