Skip to content

Commit

Permalink
Setup a simple ticking system to get the current timeslice.
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu committed Jan 20, 2011
1 parent 7087388 commit de4a581
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/com/timwu/Particles/ParticleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public ParticleView(Context context, AttributeSet attrs) {
private class ParticleViewLoop extends Thread {
private List<Particle> particles = Collections.synchronizedList(new ArrayList<Particle>());
private long prevTick;
private float curTimeslice;

@Override
public void run() {
Expand All @@ -41,6 +42,7 @@ public void run() {
prevTick = System.nanoTime();
Log.i(TAG, "Starting with tick " + prevTick);
while(!isInterrupted()) {
tick(); // Update the clocking info
doPhysics();
doAnimation();
doDraw();
Expand All @@ -63,12 +65,9 @@ private void doPhysics() {
}

private void doAnimation() {
long curTick = System.nanoTime();
float dt = (curTick - prevTick) / NANO_SECONDS_PER_SECOND;
for(Particle p : particles) {
p.tick(dt);
p.tick(curTimeslice);
}
prevTick = curTick;
}

private void doDraw() {
Expand All @@ -86,18 +85,10 @@ private void doDraw() {
getHolder().unlockCanvasAndPost(c);
}

private void addRandomParticles() {
Random r = new Random();
for (int i = 0; i < 1000; i++) {
Particle p = new Particle();
p.x = r.nextInt(getWidth());
p.y = r.nextInt(getHeight());
p.r = 2.0f;
p.color = Color.CYAN;
p.vx = r.nextFloat() * 10.0f;
p.vy = r.nextFloat() * 10.0f;
particles.add(p);
}
private void tick() {
long curTick = System.nanoTime();
curTimeslice = (curTick - prevTick) / NANO_SECONDS_PER_SECOND;
prevTick = curTick;
}

private void sprayParticles(int n, float x, float y, float vMax, float angle, float angleWindow) {
Expand Down

0 comments on commit de4a581

Please sign in to comment.