|
| 1 | +class Vaisseau extends GraphicObject { |
| 2 | + float angularVelocity = 0.0; |
| 3 | + float angularAcceleration = 0.0; |
| 4 | + |
| 5 | + float angle = 0.0; |
| 6 | + float heading = 0.0; |
| 7 | + |
| 8 | + float w = 20; |
| 9 | + float h = 10; |
| 10 | + |
| 11 | + float mass = 1.0; |
| 12 | + |
| 13 | + float speedLimit = 5; |
| 14 | + boolean thrusting = false; |
| 15 | + |
| 16 | + Vaisseau() { |
| 17 | + initValues(); |
| 18 | + } |
| 19 | + |
| 20 | + void initValues() { |
| 21 | + location = new PVector(); |
| 22 | + velocity = new PVector(); |
| 23 | + acceleration = new PVector(); |
| 24 | + } |
| 25 | + |
| 26 | + void applyForce (PVector force) { |
| 27 | + PVector f; |
| 28 | + |
| 29 | + if (mass != 1) |
| 30 | + f = PVector.div (force, mass); |
| 31 | + else |
| 32 | + f = force; |
| 33 | + |
| 34 | + this.acceleration.add(f); |
| 35 | + } |
| 36 | + |
| 37 | + void checkEdges() { |
| 38 | + if (location.x < -size) location.x = width + size; |
| 39 | + if (location.y < -size) location.y = height + size; |
| 40 | + if (location.x > width + size) location.x = -size; |
| 41 | + if (location.y > height + size) location.y = -size; |
| 42 | + } |
| 43 | + |
| 44 | + void thrust(){ |
| 45 | + float angle = heading - PI/2; |
| 46 | + |
| 47 | + PVector force = new PVector (cos(angle), sin(angle)); |
| 48 | + force.mult(0.1); |
| 49 | + |
| 50 | + applyForce(force); |
| 51 | + |
| 52 | + thrusting = true; |
| 53 | + } |
| 54 | + |
| 55 | + void update(float deltaTime) { |
| 56 | + checkEdges(); |
| 57 | + |
| 58 | + velocity.add(acceleration); |
| 59 | + |
| 60 | + velocity.limit(speedLimit); |
| 61 | + |
| 62 | + location.add(velocity); |
| 63 | + |
| 64 | + acceleration.mult(0); |
| 65 | + |
| 66 | + angularVelocity += angularAcceleration; |
| 67 | + angle += angularVelocity; |
| 68 | + |
| 69 | + angularAcceleration = 0.0; |
| 70 | + } |
| 71 | + |
| 72 | + float size = 20; |
| 73 | + |
| 74 | + void display() { |
| 75 | + pushMatrix(); |
| 76 | + translate (location.x, location.y); |
| 77 | + rotate (heading); |
| 78 | + |
| 79 | + fill(200); |
| 80 | + noStroke(); |
| 81 | + |
| 82 | + beginShape(TRIANGLES); |
| 83 | + vertex(0, -size); |
| 84 | + vertex(size, size); |
| 85 | + vertex(-size, size); |
| 86 | + endShape(); |
| 87 | + |
| 88 | + if (thrusting) { |
| 89 | + fill(200, 0, 0); |
| 90 | + } |
| 91 | + rect(-size + (size/4), size, size / 2, size / 2); |
| 92 | + rect(size - ((size/4) + size/2), size, size / 2, size / 2); |
| 93 | + |
| 94 | + popMatrix(); |
| 95 | + } |
| 96 | + |
| 97 | + void pivote(float angle) { |
| 98 | + heading += angle; |
| 99 | + } |
| 100 | + |
| 101 | + void noThrust() { |
| 102 | + thrusting = false; |
| 103 | + } |
| 104 | +} |
0 commit comments