(1).Line
import toxi.math.conversion.*;
import toxi.geom.*;
import toxi.math.*;
import toxi.geom.mesh2d.*;
import toxi.util.datatypes.*;
import toxi.util.events.*;
import toxi.geom.mesh.subdiv.*;
import toxi.geom.mesh.*;
import toxi.math.waves.*;
import toxi.util.*;
import toxi.math.noise.*;
import peasy.test.*;
import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
import processing.opengl.*;
ArrayList ballCollection=new ArrayList();
Ball basketBall;
PeasyCam cam;
float a=300;
void setup() {
cam=new PeasyCam(this, 600);
size(600, 400, OPENGL);
smooth();
for (int i=0;i<10;i++) {
Vec3D loc=new Vec3D(0, 0, -a/2);
Vec3D vel=new Vec3D(random(-5, 5), random(-5, 5), random(-5, 5));
basketBall=new Ball(loc, vel);
ballCollection.add(basketBall);
}
}
void draw() {
background(0);
stroke(255);
strokeWeight(1);
noFill();
box(2*a);
frameRate(30);
for (int i=0;i< ballCollection.size();i++) {
Ball ball =(Ball) ballCollection.get(i);
ball.run();
}
for (int i=0;i< ballCollection.size();i++) {
Ball ball =(Ball) ballCollection.get(i);
for (int j=0;j< ball.pointsList.size()-1;j++) {
Trace agent1=(Trace) ball.pointsList.get(j);
Trace agent2=(Trace) ball.pointsList.get(j+1);
agent1.run();
stroke(255, 50);
strokeWeight(1);
line(agent1.loc.x, agent1.loc.y, agent1.loc.z, agent2.loc.x, agent2.loc.y, agent2.loc.z);
}
}
}
(2).Ball
class Ball {
Vec3D loc;
Vec3D vel;
ArrayList pointsList=new ArrayList();
Vec3D gravity=new Vec3D (0, 0, -0.1);
Ball(Vec3D _loc, Vec3D _vel) {
loc = _loc;
vel = _vel;
}
void run() {
display();
moveBall();
boundary();
gravity();
createPoints();
}
void createPoints(){
if (vel.magnitude() > 1 && loc.z>-290){
Vec3D agentLoc=new Vec3D();
agentLoc=this.loc.copy();
Trace agents = new Trace(agentLoc,this);
this.pointsList.add(agents);
}
}
void friction() {
vel.scaleSelf(0.8);
}
void gravity() {
vel.addSelf(gravity);
}
void boundary() {
if (loc.x>=300) {
loc.x=300;
vel.x=-vel.x;
friction();
}
if (loc.x<=-300) {
loc.x=-300;
vel.x=-vel.x;
friction();
}
if (loc.y>=300) {
loc.y=300;
vel.y=-vel.y;
friction();
}
if (loc.y<=-300) {
loc.y=-300;
vel.y=-vel.y;
friction();
}
if (loc.z>=300) {
loc.z=300;
vel.z=-vel.z;
friction();
}
if (loc.z<=-300) {
loc.z=-300;
vel.z=-vel.z;
friction();
}
}
void display() {
stroke(255, 0, 0);
strokeWeight(10);
point(loc.x, loc.y, loc.z);
}
void moveBall() {
loc.addSelf(vel);
}
}
(3).Trace
class Trace {
Vec3D loc;
Ball parent;
int id_Ball;
int id_Trace;
boolean found = false;
Trace(Vec3D _loc,Ball _parent) {
loc= _loc;
parent = _parent;
}
void run() {
//display();
connection();
}
void connection(){
float minDis=100000;
if(found == false){
for(int i=0,i< ballCollection.size();i++){
Ball otherBall=(Ball) ballCollection.get(i);
if(otherBall != this.parent){
for(int j=0,j< otherBall.pointsList.size(),j++){
Trace otherTrace=(Trace) otherBall.pointsList.get(j);
float dis =this.loc.distanceTo(otherTrace.loc);
if(dis < minDis){
minDis=dis;
id_Ball=i;
id_Trace=j;
}
}
}
}
found = true;
}
Ball minBall=(Ball) ballCollection.get(id_Ball);
Trace minTrace=(Trace) minBall.pointsList.get(id_Trace);
line(this.loc.x,this.loc.y,this.loc.z,minTrace.loc.x,minTrace.loc.y,minTrace.loc.z);
}
void display() {
stroke(255);
strokeWeight(2);
point (loc.x, loc.y, loc.z);
}
}
|