Skip to content

Commit 4ea9a85

Browse files
committed
Ajout du projet asteroid vaisseau
1 parent 18cc22b commit 4ea9a85

11 files changed

+466
-0
lines changed

asteroid_vaisseau/GraphicObject.pde

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
abstract class GraphicObject {
2+
PVector location;
3+
PVector velocity;
4+
PVector acceleration;
5+
6+
color fillColor = color (255);
7+
color strokeColor = color (255);
8+
float strokeWeight = 1;
9+
10+
abstract void update(float deltaTime);
11+
12+
abstract void display();
13+
14+
15+
}

asteroid_vaisseau/Vaisseau.pde

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
int currentTime;
2+
int previousTime;
3+
int deltaTime;
4+
5+
boolean saveVideo = false;
6+
7+
Vaisseau v;
8+
9+
void setup () {
10+
size (800, 600);
11+
currentTime = millis();
12+
previousTime = millis();
13+
14+
v = new Vaisseau();
15+
v.location.x = width / 2;
16+
v.location.y = height / 2;
17+
18+
}
19+
20+
void draw () {
21+
currentTime = millis();
22+
deltaTime = currentTime - previousTime;
23+
previousTime = currentTime;
24+
25+
26+
update(deltaTime);
27+
display();
28+
29+
savingFrames(5, deltaTime);
30+
}
31+
32+
PVector thrusters = new PVector(0, -0.02);
33+
34+
/***
35+
The calculations should go here
36+
*/
37+
void update(int delta) {
38+
if (keyPressed) {
39+
switch (key) {
40+
case ' ':
41+
v.thrust();
42+
break;
43+
case CODED:
44+
if (keyCode == LEFT) v.pivote(-.03);
45+
if (keyCode == RIGHT) v.pivote(.03);
46+
break;
47+
}
48+
}
49+
50+
v.update(delta);
51+
}
52+
53+
/***
54+
The rendering should go here
55+
*/
56+
void display () {
57+
background(0);
58+
59+
v.display();
60+
}
61+
62+
//Saving frames for video
63+
//Put saveVideo to true;
64+
int savingAcc = 0;
65+
int nbFrames = 0;
66+
67+
void savingFrames(int forMS, int deltaTime) {
68+
69+
if (!saveVideo) return;
70+
71+
savingAcc += deltaTime;
72+
73+
if (savingAcc < forMS) {
74+
saveFrame("frames/####.tiff");
75+
nbFrames++;
76+
} else {
77+
println("Saving frames done! " + nbFrames + " saved");
78+
saveVideo = false;
79+
}
80+
}
81+
82+
83+
84+
void keyReleased() {
85+
switch (key) {
86+
case ' ':
87+
v.noThrust();
88+
break;
89+
}
90+
}

demo_20201103/GraphicObject.pde

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface IGraphicObject {
2+
3+
abstract void update(int deltaTime);
4+
abstract void display();
5+
}

demo_20201103/Mover.pde

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
abstract class Mover implements IGraphicObject {
2+
PVector location;
3+
PVector velocity;
4+
PVector acceleration;
5+
6+
color strokeColor;
7+
color fillColor;
8+
}

demo_20201103/Particle.pde

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Particle extends Mover {
2+
3+
int diameter = 5;
4+
float lifespan;
5+
6+
PVector initialPosition;
7+
8+
float intensity;
9+
10+
Particle (PVector loc) {
11+
location = loc.copy();
12+
acceleration = new PVector (0f, 0.05f);
13+
intensity = 1;
14+
velocity = new PVector (randomGaussian() * intensity, random (-2, 0) * 3);
15+
initialPosition = location.copy();
16+
17+
lifespan = random (200, 255);
18+
19+
strokeColor = 255;
20+
fillColor = color (200, 0, 0);
21+
}
22+
23+
void update (int delta) {
24+
velocity.add (acceleration);
25+
location.add (velocity);
26+
27+
lifespan -= 2.0;
28+
}
29+
30+
void display() {
31+
stroke (strokeColor, lifespan);
32+
fill (fillColor, lifespan);
33+
34+
pushMatrix();
35+
translate (location.x, location.y);
36+
ellipse (0, 0, diameter, diameter);
37+
popMatrix();
38+
}
39+
40+
boolean isDead () {
41+
return lifespan <= 0.0;
42+
}
43+
44+
void reset () {
45+
location.x = initialPosition.x;
46+
location.y = initialPosition.y;
47+
velocity.x = randomGaussian() * intensity;
48+
velocity.y = random (-2, 0);
49+
lifespan = random (200, 255);
50+
}
51+
}

demo_20201103/ParticleSystem.pde

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class ParticleSystem implements IGraphicObject {
2+
ArrayList<Particle> particles;
3+
int nbParticles = 100;
4+
5+
ParticleSystem () {
6+
7+
particles = new ArrayList<Particle>();
8+
for (int i = 0; i < nbParticles; i++) {
9+
particles.add (new Particle(new PVector (width / 2, 100)));
10+
}
11+
}
12+
13+
ParticleSystem (PVector loc) {
14+
15+
particles = new ArrayList<Particle>();
16+
for (int i = 0; i < nbParticles; i++) {
17+
particles.add (new Particle(loc));
18+
}
19+
20+
}
21+
22+
23+
24+
void update(int delta) {
25+
for (Particle p : particles){
26+
if (p.isDead()){
27+
p.reset();
28+
}
29+
30+
p.update (delta);
31+
}
32+
}
33+
34+
void display() {
35+
for (Particle p : particles){
36+
p.display();
37+
}
38+
}
39+
40+
}

demo_20201103/demo_20201103.pde

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
int currentTime;
2+
int previousTime;
3+
int deltaTime;
4+
5+
ParticleSystem ps;
6+
7+
void setup () {
8+
size (640, 480);
9+
currentTime = millis();
10+
previousTime = currentTime;
11+
12+
ps = new ParticleSystem(new PVector(width / 2, height / 2));
13+
}
14+
15+
void draw () {
16+
currentTime = millis();
17+
deltaTime = currentTime - previousTime;
18+
previousTime = currentTime;
19+
20+
update(deltaTime);
21+
display();
22+
}
23+
24+
color fillColor = 0;
25+
int alpha = 2;
26+
27+
/***
28+
The calculations should go here
29+
*/
30+
void update(int delta) {
31+
ps.update(delta);
32+
}
33+
34+
/***
35+
The rendering should go here
36+
*/
37+
void display () {
38+
background (0);
39+
//fill (fillColor, alpha);
40+
//rect (0, 0, width, height);
41+
42+
//fill (0);
43+
//rect (width / 2 - 128, 100, 256, 300);
44+
ps.display();
45+
46+
}
47+
48+
void mousePressed() {
49+
if (mouseButton == LEFT) {
50+
ps = new ParticleSystem(new PVector (mouseX, mouseY));
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
abstract class GraphicObject {
2+
3+
PVector location;
4+
PVector velocity;
5+
PVector acceleration;
6+
7+
PVector size;
8+
9+
abstract void update(int deltaTime);
10+
abstract void display();
11+
}

0 commit comments

Comments
 (0)