Created
January 4, 2018 16:47
-
-
Save mao-test-h/d96f31b71e8a7f36d111129609beb2e2 to your computer and use it in GitHub Desktop.
Processing 重力テスト → https://twitter.com/TEST_H_/status/948957197425258497
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
{ | |
float _mass; | |
PVector _location; | |
PVector _velocity; | |
PVector _acceleration; | |
float _G; | |
Object(float mass, float x, float y) | |
{ | |
_mass = mass; | |
_location = new PVector(x, y); | |
_velocity = new PVector(0, 0); | |
_acceleration = new PVector(0, 0); | |
_G = 10; | |
} | |
void update() | |
{ | |
_velocity.add(_acceleration); | |
_location.add(_velocity); | |
_acceleration.mult(0); | |
} | |
void display() | |
{ | |
stroke(0); | |
fill(180); | |
rect(_location.x, _location.y, _mass * 16, _mass * 16); | |
} | |
void applyForce(PVector force) | |
{ | |
_acceleration.add(PVector.div(force, _mass)); | |
} | |
PVector attract(Object obj) | |
{ | |
PVector force = PVector.sub(obj._location, _location); | |
float distance = force.mag(); | |
distance = constrain(distance, 5.0, 25.0); | |
force.normalize(); | |
float strength = (_G * _mass * obj._mass) / (distance * distance); | |
force.mult(strength); | |
return force; | |
} | |
} | |
Object[] objects = new Object[128]; | |
void setup() | |
{ | |
size(1280, 720); | |
for(int i = 0; i < objects.length; ++i) | |
{ | |
objects[i] = new Object(1, random(width), random(height)); | |
} | |
} | |
void draw() | |
{ | |
background(255); | |
for(int i = 0; i < objects.length; ++i) | |
{ | |
int next = i + 1; | |
next = next % objects.length; | |
PVector force = objects[i].attract(objects[next]); | |
objects[i].applyForce(force); | |
objects[i].update(); | |
objects[i].display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment