//puppet //cowboy will stumble near your mouse and fire the shotgun when //the mouse is clicked. hold down the mouse to aim the shotgun. //drunk walk cycle is 89 frames long but java runs out of memory //you are viewing 39 frames into the (cycle). //http://users.dma.ucla.edu/~chaudown/drunkcycle.swf //program is fixed by running through the P3D graphics engine AniSprite animation1, animation2; PImage gun; PImage arm; PImage elbow; float x = 100; float y = 100; float x2 = 100; float y2 = 435; float y3 = 435; float segLength = 30; float xpos; float ypos; float drag = 50; ArrayList psystems; void setup() { size(600,600, P3D); animation1 = new AniSprite("cowboy_strut",89); animation2 = new AniSprite("cowboy_shoot",1); frameRate(24); psystems = new ArrayList(); // smooth(); gun = loadImage("shotgun.gif"); arm = loadImage("arm.gif"); elbow = loadImage("elbow.gif"); } void draw() { background(216,79,38); float difx = mouseX - xpos; if(abs(difx) > 1.0) { xpos = xpos + difx/drag; xpos = constrain(xpos,0,width); } if(mousePressed){ animation2.display(xpos-animation1.getWidth()/2+10, 300); stroke(255); float x3 = xpos; float dx = mouseX - x; float dy = mouseY - y; float angle1 = atan2(dy, dx); float tx = mouseX - cos(angle1) * segLength; float ty = mouseY - sin(angle1) * segLength; dx = tx - x2; dy = ty - y2; float angle2 = atan2(dy, dx); x = x2 + cos(angle2) * segLength; y = y2 + sin(angle2) * segLength; float tx2 = mouseX - cos(angle2) * segLength; float ty2 = mouseY - sin(angle2) * segLength; dx = tx2 - x3; dy = ty2 - y3; float angle3 = atan2(dy, dx); x2 = x3 + cos(angle3) * segLength; y2 = y3 + sin(angle3) * segLength; segment(x, y, angle1, 1); segment(x2, y2, angle2, 2); segment(x3, y3, angle3, 3); } else { animation1.display(xpos-animation1.getWidth()/2, 300); } for (int i = psystems.size()-1; i >= 0; i--) { ParticleSystem psys = (ParticleSystem) psystems.get(i); psys.run(); if (psys.dead()) { psystems.remove(i); } } } void mousePressed() { psystems.add(new ParticleSystem(int(random(5,45)),new Vector3D(mouseX,mouseY))); } //arm movement void segment(float x, float y, float a, int imagenum) { pushMatrix(); translate(x, y); rotate(a); if(imagenum==1) { image(gun, -40, -18); } else if(imagenum==2) { image(elbow, -10, -2); } else if(imagenum==3) { image(arm, 0, 0); } else { line(0, 0, segLength, 0); } popMatrix(); } class AniSprite { PImage[] ani; int frame = 0; int numFrames; AniSprite(String imageName, int aframeCount) { numFrames = aframeCount; ani = new PImage[numFrames]; loadImages (imageName); } void loadImages (String name) { for (int i=0; i= 0; i--) { Particle p = (Particle) particles.get(i); p.run(); if (p.dead()) { particles.remove(i); } } } void addParticle() { particles.add(new Particle(origin)); } void addParticle(Particle p) { particles.add(p); } // A method to test if the particle system still has particles boolean dead() { if (particles.isEmpty()) { return true; } else { return false; } } } // Simple Vector3D Class public class Vector3D { public float x; public float y; public float z; Vector3D(float x_, float y_, float z_) { x = x_; y = y_; z = z_; } Vector3D(float x_, float y_) { x = x_; y = y_; z = 0f; } Vector3D() { x = 0f; y = 0f; z = 0f; } void setX(float x_) { x = x_; } void setY(float y_) { y = y_; } void setZ(float z_) { z = z_; } void setXY(float x_, float y_) { x = x_; y = y_; } void setXYZ(float x_, float y_, float z_) { x = x_; y = y_; z = z_; } void setXYZ(Vector3D v) { x = v.x; y = v.y; z = v.z; } public float magnitude() { return (float) Math.sqrt(x*x + y*y + z*z); } public Vector3D copy() { return new Vector3D(x,y,z); } public Vector3D copy(Vector3D v) { return new Vector3D(v.x, v.y,v.z); } public void add(Vector3D v) { x += v.x; y += v.y; z += v.z; } public void sub(Vector3D v) { x -= v.x; y -= v.y; z -= v.z; } public void mult(float n) { x *= n; y *= n; z *= n; } public void div(float n) { x /= n; y /= n; z /= n; } public void normalize() { float m = magnitude(); if (m > 0) { div(m); } } public void limit(float max) { if (magnitude() > max) { normalize(); mult(max); } } public float heading2D() { float angle = (float) Math.atan2(-y, x); return -1*angle; } public Vector3D add(Vector3D v1, Vector3D v2) { Vector3D v = new Vector3D(v1.x + v2.x,v1.y + v2.y, v1.z + v2.z); return v; } public Vector3D sub(Vector3D v1, Vector3D v2) { Vector3D v = new Vector3D(v1.x - v2.x,v1.y - v2.y,v1.z - v2.z); return v; } public Vector3D div(Vector3D v1, float n) { Vector3D v = new Vector3D(v1.x/n,v1.y/n,v1.z/n); return v; } public Vector3D mult(Vector3D v1, float n) { Vector3D v = new Vector3D(v1.x*n,v1.y*n,v1.z*n); return v; } public float distance (Vector3D v1, Vector3D v2) { float dx = v1.x - v2.x; float dy = v1.y - v2.y; float dz = v1.z - v2.z; return (float) Math.sqrt(dx*dx + dy*dy + dz*dz); } }