mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-04-14 06:33:34 +02:00
Compare commits
6 Commits
v0.0.22-SN
...
partikel
| Author | SHA1 | Date | |
|---|---|---|---|
| c9a7ac9551 | |||
| c6615cc817 | |||
| 4d981b62a4 | |||
| 05aae6e6cc | |||
| 5e42509f75 | |||
| e2bdf1e998 |
30
examples/zm_partikel/PartikelBeispiel.java
Normal file
30
examples/zm_partikel/PartikelBeispiel.java
Normal file
@@ -0,0 +1,30 @@
|
||||
import schule.ngb.zm.DrawableLayer;
|
||||
import schule.ngb.zm.Zeichenmaschine;
|
||||
import schule.ngb.zm.partikel.PartikelGenerator;
|
||||
|
||||
public class PartikelBeispiel extends Zeichenmaschine {
|
||||
|
||||
public static void main( String[] args ) {
|
||||
new PartikelBeispiel();
|
||||
}
|
||||
|
||||
public PartikelBeispiel() {
|
||||
super(400, 400, "ZM: Partikel");
|
||||
}
|
||||
|
||||
PartikelGenerator pgen;
|
||||
|
||||
public void setup() {
|
||||
pgen = new PartikelGenerator(200, 200, 3, 20);
|
||||
DrawableLayer drawables = new DrawableLayer();
|
||||
addLayer(drawables);
|
||||
drawables.add(pgen);
|
||||
pgen.starten();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update( double delta ) {
|
||||
pgen.update(delta);
|
||||
}
|
||||
|
||||
}
|
||||
75
src/schule/ngb/zm/partikel/Partikel.java
Normal file
75
src/schule/ngb/zm/partikel/Partikel.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package schule.ngb.zm.partikel;
|
||||
|
||||
import schule.ngb.zm.Updatable;
|
||||
import schule.ngb.zm.Color;
|
||||
import schule.ngb.zm.Vector;
|
||||
import schule.ngb.zm.Drawable;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
|
||||
|
||||
public class Partikel implements Updatable, Drawable {
|
||||
|
||||
int maxLeben = 0;
|
||||
|
||||
int leben = 0;
|
||||
|
||||
Vector position, geschwindigkeit, beschleunigung;
|
||||
|
||||
Color farbe, farbeStart, farbeEnde;
|
||||
|
||||
Partikel naechster = null;
|
||||
|
||||
double masse = 1.0;
|
||||
|
||||
public Partikel( Vector pPosition ) {
|
||||
position = pPosition.copy();
|
||||
geschwindigkeit = new Vector();
|
||||
beschleunigung = new Vector();
|
||||
}
|
||||
|
||||
public Partikel( Vector pPosition, Vector pGeschwindigkeit ) {
|
||||
position = pPosition.copy();
|
||||
geschwindigkeit = pGeschwindigkeit.copy();
|
||||
beschleunigung = new Vector();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return leben > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVisible() {
|
||||
return isActive();
|
||||
}
|
||||
|
||||
public void beschleunigen( Vector pBeschleunigung ) {
|
||||
beschleunigung.add(Vector.div(pBeschleunigung, masse));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update( double delta ) {
|
||||
if( isActive() ) {
|
||||
geschwindigkeit.add(beschleunigung);
|
||||
position.add(Vector.scale(geschwindigkeit, delta));
|
||||
beschleunigung.scale(0.0);
|
||||
|
||||
leben -= 1;
|
||||
|
||||
if( farbeStart != null ) {
|
||||
double t = 1.0 - (double) leben / (double) maxLeben;
|
||||
farbe = Color.interpolate(farbeStart, farbeEnde, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw( Graphics2D graphics ) {
|
||||
if( isVisible() ) {
|
||||
graphics.setColor(farbe.getJavaColor());
|
||||
graphics.fillOval((int) position.x, (int) position.y, 4, 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
171
src/schule/ngb/zm/partikel/PartikelGenerator.java
Normal file
171
src/schule/ngb/zm/partikel/PartikelGenerator.java
Normal file
@@ -0,0 +1,171 @@
|
||||
package schule.ngb.zm.partikel;
|
||||
|
||||
import schule.ngb.zm.Updatable;
|
||||
import schule.ngb.zm.Color;
|
||||
import schule.ngb.zm.Vector;
|
||||
import schule.ngb.zm.Drawable;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class PartikelGenerator implements Updatable, Drawable {
|
||||
|
||||
private int partikelProFrame;
|
||||
|
||||
private int partikelLeben = 180;
|
||||
|
||||
private Partikel[] partikel;
|
||||
|
||||
private boolean aktiv = false, _aktiv = false;
|
||||
|
||||
private Partikel naechsterPartikel;
|
||||
|
||||
public Vector position;
|
||||
|
||||
public Vector richtung = new Vector();
|
||||
|
||||
public int winkel = 0;
|
||||
|
||||
public Color farbeStart = new Color(128,128,129);
|
||||
|
||||
public Color farbeEnde = new Color(128,128,129, 0);
|
||||
|
||||
public double zufallsfaktor = 0.0;
|
||||
|
||||
private Vortex vortex = null;
|
||||
|
||||
public PartikelGenerator( double pX, double pY, int pPartikelLeben, int pPartikelProFrame ) {
|
||||
position = new Vector(pX, pY);
|
||||
partikelProFrame = pPartikelProFrame;
|
||||
partikelLeben = pPartikelLeben;
|
||||
partikel = new Partikel[partikelProFrame*partikelLeben];
|
||||
|
||||
//vortex = new Vortex(position.kopie().addieren(-10, -10), -.2, 8);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return aktiv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVisible() {
|
||||
return aktiv;
|
||||
}
|
||||
|
||||
public void starten() {
|
||||
// Partikel initialisieren
|
||||
for( int i = 0; i < partikel.length; i++ ) {
|
||||
partikel[i] = new Partikel(position.copy());
|
||||
if( i > 0 ) {
|
||||
partikel[i-1].naechster = partikel[i];
|
||||
}
|
||||
}
|
||||
naechsterPartikel = partikel[0];
|
||||
|
||||
aktiv = true;
|
||||
}
|
||||
|
||||
public void stoppen() {
|
||||
aktiv = false;
|
||||
}
|
||||
|
||||
public void partikelGenerieren() {
|
||||
int i = partikelProFrame;
|
||||
while( i > 0 && naechsterPartikel != null ) {
|
||||
Partikel p = naechsterPartikel;
|
||||
naechsterPartikel = p.naechster;
|
||||
|
||||
double rotation = Math.toRadians(
|
||||
(winkel / 2) - (int) (Math.random() * winkel)
|
||||
);
|
||||
p.position.set(position);
|
||||
|
||||
p.geschwindigkeit.set(richtung.copy().rotate(rotation));
|
||||
p.geschwindigkeit.scale(zufall());
|
||||
|
||||
p.farbeStart = farbeStart;
|
||||
p.farbeEnde = farbeEnde;
|
||||
|
||||
int pLeben = (int) zufall(partikelLeben);
|
||||
p.leben = pLeben;
|
||||
p.maxLeben = pLeben;
|
||||
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update( double delta ) {
|
||||
if( isActive() ) {
|
||||
partikelGenerieren();
|
||||
|
||||
_aktiv = false;
|
||||
for( int i = 0; i < partikel.length; i++ ) {
|
||||
if( partikel[i] != null ) {
|
||||
if( partikel[i].isActive() ) {
|
||||
if( vortex != null ) {
|
||||
vortex.attract(partikel[i]);
|
||||
}
|
||||
partikel[i].update(delta);
|
||||
_aktiv = true;
|
||||
} else {
|
||||
partikel[i].naechster = naechsterPartikel;
|
||||
naechsterPartikel = partikel[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private double zufall() {
|
||||
return 1.0-(Math.random()*zufallsfaktor);
|
||||
}
|
||||
|
||||
private double zufall( double pZahl ) {
|
||||
return pZahl*zufall();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw( Graphics2D graphics ) {
|
||||
if( isActive() ) {
|
||||
java.awt.Color current = graphics.getColor();
|
||||
for( int i = 0; i < partikel.length; i++ ) {
|
||||
if( partikel[i] != null ) {
|
||||
partikel[i].draw(graphics);
|
||||
}
|
||||
}
|
||||
|
||||
if( vortex != null ) {
|
||||
graphics.setColor(java.awt.Color.BLACK);
|
||||
double vscale = (4*vortex.scale);
|
||||
graphics.fillOval((int)(vortex.position.x-vscale*.5), (int)(vortex.position.y-vscale*.5), (int)vscale, (int)vscale);
|
||||
}
|
||||
graphics.setColor(current);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
class Vortex {
|
||||
Vector position;
|
||||
double speed = 1.0, scale = 1.0;
|
||||
|
||||
public Vortex( Vector pPosition ,double pSpeed, double pScale ) {
|
||||
this.position = pPosition.copy();
|
||||
this.scale = pScale;
|
||||
this.speed = pSpeed;
|
||||
}
|
||||
|
||||
public void attract( Partikel pPartikel ) {
|
||||
Vector diff = Vector.sub(pPartikel.position, this.position);
|
||||
double dx = -diff.y * this.speed;
|
||||
double dy = diff.x * this.speed;
|
||||
double f = 1.0 / (1.0 + (dx*dx+dy*dy)/scale);
|
||||
|
||||
pPartikel.position.x += (diff.x - pPartikel.geschwindigkeit.x) * f;
|
||||
pPartikel.position.y += (diff.y - pPartikel.geschwindigkeit.y) * f;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
4
src/schule/ngb/zm/partikel/PartikelSystem.java
Normal file
4
src/schule/ngb/zm/partikel/PartikelSystem.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package schule.ngb.zm.partikel;
|
||||
|
||||
public class PartikelSystem {
|
||||
}
|
||||
49
test/schule/ngb/zm/TestPartikel.java
Normal file
49
test/schule/ngb/zm/TestPartikel.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package schule.ngb.zm;
|
||||
|
||||
import schule.ngb.zm.partikel.PartikelGenerator;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class TestPartikel extends Zeichenfenster {
|
||||
|
||||
public static void main( String[] args ) {
|
||||
new TestPartikel();
|
||||
}
|
||||
|
||||
PartikelGenerator[] pg = new PartikelGenerator[3];
|
||||
|
||||
public void vorbereiten() {
|
||||
setSize(400,400);
|
||||
|
||||
int w = 4, pl = 360;
|
||||
Farbe f1 = new Farbe(Color.GREEN, 128), f2 = new Farbe(Color.YELLOW, 0);
|
||||
Vektor r = new Vektor(-32, 10);
|
||||
int ppf = 2;
|
||||
|
||||
pg[0] = new PartikelGenerator(100,200, pl, ppf);
|
||||
pg[0].zufallsfaktor = 0.0;
|
||||
|
||||
pg[1] = new PartikelGenerator(200,200, pl, ppf);
|
||||
pg[1].zufallsfaktor = 0.5;
|
||||
|
||||
pg[2] = new PartikelGenerator(300,200, pl, ppf);
|
||||
pg[2].zufallsfaktor = 1.0;
|
||||
|
||||
/*for(PartikelGenerator p: pg) {
|
||||
p.winkel = w;
|
||||
p.farbeStart = f1;
|
||||
p.farbeEnde = f2;
|
||||
p.richtung = r;
|
||||
|
||||
drawing.hinzu(p);
|
||||
p.starten();
|
||||
}*/
|
||||
}
|
||||
|
||||
public void zeichnen() {
|
||||
for(PartikelGenerator p: pg) {
|
||||
p.aktualisieren(delta);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user