patternjavaMinor
Java snow animation
Viewed 0 times
snowanimationjava
Problem
I was inspired by this to write a graphic snow animation in Java/Processing. It works great, but it has some design issues.
The full project is here on github;
```
package com.jatkin.snowday;
import com.jatkin.snowday.snowflake.FlakeFactory;
import com.jatkin.snowday.snowflake.Snowflake;
import processing.core.PGraphics;
import processing.core.PVector;
import java.util.ArrayList;
import java.util.List;
/**
* This class controls all physics for SnowDay.
*
* Created by Jarrett on 02/04/16.
*/
public class World {
private float gravity;
private List flurry;
private Wind wind;
private int flakeCountTarget;
public World() { this(2); } // FIXME magic number
public World(int gravity) {
this.gravity = gravity;
flurry = new ArrayList<>();
wind = new Wind();
flakeCountTarget = 200;// FIXME magic number
}
/**
* Advance the snow animation 1 frame
*/
public void tick(int screenWidth, int screenHeight) {
wind.tick();
manageSnowflakeCount(screenWidth, screenHeight);
edgeWrapSnowflakes(screenWidth, screenHeight);
updateSnowflakePosition();
}
/**
The full project is here on github;
Mainpackage com.jatkin.snowday;
import processing.core.PApplet;
import processing.core.PGraphics;
/**
* Created by Jarrett on 02/04/16.
*/
public class Main extends PApplet {
public static void main(String[] args) {PApplet.main(Main.class.getCanonicalName());}
final int backgroundColor = color(0, 146, 178);
final World world = new World();
/**
* Processing 3 has changed the basic format for starting an app from raw
* java. Size now must be called from `settings`.
*/
@Override
public void settings() {
size(500, 300, FX2D);
}
@Override
public void draw() {
fill(255);
noStroke();
background(backgroundColor);
world.tick(sketchWidth(), sketchHeight());
world.render(getGraphics());
}
}World```
package com.jatkin.snowday;
import com.jatkin.snowday.snowflake.FlakeFactory;
import com.jatkin.snowday.snowflake.Snowflake;
import processing.core.PGraphics;
import processing.core.PVector;
import java.util.ArrayList;
import java.util.List;
/**
* This class controls all physics for SnowDay.
*
* Created by Jarrett on 02/04/16.
*/
public class World {
private float gravity;
private List flurry;
private Wind wind;
private int flakeCountTarget;
public World() { this(2); } // FIXME magic number
public World(int gravity) {
this.gravity = gravity;
flurry = new ArrayList<>();
wind = new Wind();
flakeCountTarget = 200;// FIXME magic number
}
/**
* Advance the snow animation 1 frame
*/
public void tick(int screenWidth, int screenHeight) {
wind.tick();
manageSnowflakeCount(screenWidth, screenHeight);
edgeWrapSnowflakes(screenWidth, screenHeight);
updateSnowflakePosition();
}
/**
Solution
That looks really good! I just have a few improvements, nothing big.
A math function called
You might consider moving all the private
Thoughts
Really these are just small tweaks, you have some very well-written and documented code!
The next thing to do would be to make more types of snowflakes (hexagons, stars, textures) for variety.
Wind.interplorateWindSpeed()A math function called
signum could be used instead of if statements.float speedDiff = targetSpeed - speed;
float speedChange = (float) (sqrt(abs(speedDiff))/15);
speed += speedChange * signum(speedDiff);WindYou might consider moving all the private
floats values into a constructor, just for consistency.Thoughts
Really these are just small tweaks, you have some very well-written and documented code!
The next thing to do would be to make more types of snowflakes (hexagons, stars, textures) for variety.
Code Snippets
float speedDiff = targetSpeed - speed;
float speedChange = (float) (sqrt(abs(speedDiff))/15);
speed += speedChange * signum(speedDiff);Context
StackExchange Code Review Q#119033, answer score: 2
Revisions (0)
No revisions yet.