Skip to content

Commit

Permalink
Ajout de s02_forces_exemple
Browse files Browse the repository at this point in the history
  • Loading branch information
nbourre committed Sep 11, 2023
1 parent 53a9d1b commit f0c27aa
Show file tree
Hide file tree
Showing 8 changed files with 563 additions and 0 deletions.
Binary file added bin/s02_forces_exemple.pdez
Binary file not shown.
66 changes: 66 additions & 0 deletions s02_forces_exemple/Fluid.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
Classe simulant un fluide. Elle est représentée par un rectangle.
*/
class Fluid {
Rectangle r;
float density;
float coefficientFriction;

private color c;

Fluid () {
float quarterHeight = height / 4;
r = new Rectangle(0, height - quarterHeight, width, quarterHeight);
density = 0.8;
coefficientFriction = 0.1;

this.c = color (127, 127);
}

Fluid (Rectangle _r, float _density, float _coefficientFriction) {
r = _r;
density = _density;
coefficientFriction = _coefficientFriction;

this.c = color (127, 127);
}

void setRectangle (Rectangle _r) {
r = _r;
}

Rectangle getRectangle () {
return r;
}

void display () {

fill (c);
r.display();
}

/**
Formule F = -0.5 * rho * ||v||^2 * area * friction * speed.normalise
*/
PVector draggingForce(PVector speed, float area) {
float speedMag = speed.mag();
float coeffRhoMag = density * coefficientFriction * speedMag * speedMag * 0.5;

PVector result = speed.get();
result.mult(-1);
result.normalize();
result.mult(area);
result.mult(coeffRhoMag);

return result;
}

void setColor (color c) {
this.c = c;
}

void setLocation (int x, int y) {
this.r.x = x;
this.r.y = y;
}
}
20 changes: 20 additions & 0 deletions s02_forces_exemple/Forces.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Forces {

Forces () {
}

// Faux calcul qui prend une valeur scalaire pour la normale
PVector friction (PVector vitesse, float coeffFriction, float normale) {
PVector resultat = new PVector();

resultat = vitesse.get();

resultat.mult(-1);
resultat.normalize(); // Réduire la direction en valeur unitaire


resultat.mult(normale * coeffFriction);

return resultat;
}
}
39 changes: 39 additions & 0 deletions s02_forces_exemple/Message.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Message {

String text = "";
PVector location;
PFont font;
int fontSize = 16;
color fontColor = color (0);

Message (String text) {
this.text = text;

baseInit();
}

void baseInit () {
location = new PVector();
font = createFont ("Arial", fontSize, true);
textFont (font);
}

void display () {
fill( fontColor);
text(text, location.x, location.y);
}

void setLocation (float x, float y) {
location.set (x, y);
}

PVector getLocation () {return location;}

void setText (String text) {
this.text = text;
}

String getText() {return text;}

float getWidth() {return textWidth(text); }
}
132 changes: 132 additions & 0 deletions s02_forces_exemple/Mover.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
class Mover {
PVector location;
PVector velocity;
PVector acceleration;


float topSpeed;
float mass = 1;
float coeffRestitution = 1;
float radius;
float diametre;

boolean hasContactWithFloor = false;

private float diametreFactor = 16;



Mover () {

this.location = new PVector (random (width), random (height));
this.velocity = new PVector (0, 0);
this.acceleration = new PVector (0 , 0);

updateMath();
}

Mover (PVector loc, PVector vel) {
this.location = loc;
this.velocity = vel;
this.acceleration = new PVector (0 , 0);

this.topSpeed = 100;

updateMath();
}

Mover (float m, float x, float y) {
mass = m;
location = new PVector (x, y);

velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);

updateMath();
}

Mover (float x, float y) {
mass = 1;
location = new PVector (x, y);
velocity = new PVector(0, 0);
acceleration = new PVector (0, 0);

updateMath();
}

void updateMath() {
diametre = mass * diametreFactor;
radius = diametre / 2;
}

void setMass(float mass) {
this.mass = mass;
updateMath();
}

void update () {
velocity.add (acceleration);
location.add (velocity);

acceleration.mult (0);
}

void display () {
stroke (0);
fill (127, 127, 127, 127);

if (location.y > -diametre) {
ellipse (location.x, location.y, diametre, diametre); // Dimension à l'échelle de la masse
}
else {
fill (200, 0, 0);
rect (location.x, 0, diametre, -location.y / 10);
}
}

void checkEdges() {
if (location.x + radius > width) {
location.x = width - radius;
velocity.x *= -1 * coeffRestitution;
} else if (location.x < 0) {
velocity.x *= -1 * coeffRestitution;
location.x = radius;
}

if (location.y + radius > height) {
velocity.y *= -1 * coeffRestitution;
location.y = height - radius;
hasContactWithFloor = true;
}
else
hasContactWithFloor = false;

// Damping pour empêcher la vibration
if (Math.abs(velocity.y) < 0.0001) {
velocity.y = 0;
}

if (Math.abs(velocity.x) < 0.0001) {
velocity.x = 0;
}
}


void applyForce (PVector force) {
PVector f = PVector.div (force, mass);

this.acceleration.add(f);
}

// Utiliser pour la gravity uniquement
// la masse n'a aucun effet dessus
void applyGravity (PVector acc) {
this.acceleration.add (acc);
}

Rectangle getRectangle() {
Rectangle r = new Rectangle(location.x - radius, location.y - radius, diametre, diametre);

return r;
}
}
52 changes: 52 additions & 0 deletions s02_forces_exemple/Rectangle.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Rectangle {
float x, y, w, h;

Rectangle(float _x, float _y, float _w, float _h) {
x = _x;
y = _y;
w = _w;
h = _h;
}

Boolean contains(Rectangle _r) {
Boolean result = false;

if (x <= _r.x && ((x + w) >= (_r.x + _r.w)) && y <= _r.y && ((y + h) >= (_r.y + _r.h))) {
result = true;
}

return result;
}

Boolean contains (int x, int y) {
Boolean result = false;

if (this.x <= x && this.x + this.w > x && this.y <= y && this.y + this.h > y) {
result = true;
}

return result;
}

float left () {return x;}
float top () {return y;}
float right () {return x + w;}
float bottom () {return y + h;}

Boolean intersect(Rectangle _r) {
Boolean result = false;

if (!(this.left() > _r.right() ||
this.right() < _r.left() ||
this.top() > _r.bottom() ||
this.bottom() < _r.top())) {
result = true;
}

return result;
}

void display() {
rect (x, y, w, h);
}
}
Loading

0 comments on commit f0c27aa

Please sign in to comment.