Guest User

Untitled

a guest
Dec 12th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. public class Asteroid{
  2.  
  3. static final float G = 0.01;
  4. static final float LOW_MASS = 1;
  5.  
  6. PImage img;
  7.  
  8. PShape model;
  9.  
  10. ArrayList <Asteroid> asteroids;
  11.  
  12. PVector pos;
  13.  
  14. PVector vel= new PVector();
  15.  
  16. PVector acc = new PVector();
  17.  
  18. PVector spin = new PVector();
  19.  
  20. float mass = 0.5;
  21.  
  22. float siz = 1000;
  23.  
  24. boolean dead = false;
  25.  
  26. //the general purpose constructor
  27. public Asteroid(ArrayList<Asteroid> a, float m, PShape s, PVector p, PVector v){
  28. asteroids = a;
  29. asteroids.add(this);
  30. model = s;
  31. pos = p;
  32. vel = v;
  33. mass = m;
  34. spin = setSpin();
  35. siz = setSize();
  36. }
  37.  
  38. //constructor to make objects orbit about a central body
  39. public Asteroid(Asteroid parent, float m, PVector newVel){
  40. asteroids = parent.asteroids;
  41. asteroids.add(this);
  42. model = parent.model;
  43. pos = parent.pos.copy().add(newVel);//clear it away from the parent and sibings
  44. vel = parent.vel.copy().add(newVel);//give new velocity
  45. mass = m;
  46. parent.pos.sub(newVel);
  47. parent.vel.sub(newVel);
  48. parent.mass -= mass;
  49. parent.siz = parent.setSize();
  50. spin = setSpin();
  51. siz = setSize();
  52. }
  53.  
  54. public Asteroid(Asteroid main, PVector p){
  55. asteroids = main.asteroids;
  56. asteroids.add(this);
  57. model = main.model;
  58. pos = p;
  59. spin = setSpin();
  60. vel = calcOrbitalVelocity(main);
  61. siz = setSize();
  62. }
  63.  
  64. float setSize(){
  65. return 10.* pow(mass, 0.333);
  66. }
  67.  
  68. PVector setSpin(){
  69. return new PVector(random(-PI/100., PI/100.),random(-PI/100., PI/100.),random(-PI/100., PI/100.));
  70. }
  71.  
  72. void move(){
  73. acc.add(calcForce());
  74. vel.add(acc);
  75. pos.add(vel);
  76. acc.mult(0);
  77. //rot.add(spin);
  78. }
  79.  
  80. PVector calcForce() {
  81. PVector tempAcc = new PVector();
  82. for (int i = 0; i < asteroids.size(); i++) {
  83. Asteroid other = asteroids.get(i);
  84. // println(tempAcc.mag());
  85. if (this == other ) {
  86. continue;
  87. }
  88. PVector r = PVector.sub(pos, other.pos);
  89.  
  90. if (r.mag()>siz) {
  91. r.normalize();
  92. PVector grav = PVector.mult(r, - G*other.mass/r.magSq());
  93. tempAcc.add(grav);
  94. } else if (r.mag() < siz*0.666){// && mass / other.mass >= 5) {
  95. //lets have an inelastic collision
  96. vel.mult(mass).add(other.vel.mult(other.mass));
  97. vel.div(mass + other.mass);
  98. // spin.div(mass);
  99. // other.spin.div(other.mass);
  100. //spin.add(other.spin);
  101. mass += other.mass;
  102. // spin.mult(mass);
  103. siz = setSize();
  104. //model = getShape();
  105. asteroids.remove(other);//change this to asteroids.other.dead = true;
  106. } else {
  107. //lets bounce them off each other
  108.  
  109. }
  110. }
  111. return tempAcc;
  112. }
  113.  
  114. void drawA() {
  115.  
  116. pushMatrix();
  117. translate(pos.x, pos.y, pos.z );
  118. scale(siz/3);
  119. model.rotateX(spin.x);
  120. model.rotateY(spin.y);
  121. model.rotateZ(spin.z);
  122. shape(model, 0, 0 );
  123. popMatrix();
  124.  
  125. }
  126.  
  127. void checkPos() {
  128. //periodic boundary conditions
  129. if (pos.dist(posa) > 10*width) {
  130. pos.mult(-1);
  131. }
  132. }
  133.  
  134.  
  135. //not sure this is used currently, but will be handy once we have bullets.
  136. void checkDestroy() {
  137. if (dead) {
  138.  
  139. asteroids.remove(this);
  140.  
  141. }
  142. }
  143.  
  144. PVector calcOrbitalVelocity(Asteroid main) {
  145. //get a velocity based on how far away the asteroid is
  146. PVector temp = PVector.sub(pos,main.pos);
  147. float dist = temp.mag();
  148. float speed = sqrt(G*main.mass/dist);
  149. temp.normalize();
  150. temp.mult(speed*300);
  151. float randDir = random(50);
  152.  
  153. if (randDir < 50){
  154. randDir = PI*0.5;
  155. }
  156. else {
  157. randDir = -PI*0.5;
  158. }
  159. temp.rotate(randDir);
  160. //rotate it by either 90 or 270
  161. return temp;
  162. }
  163.  
  164. void update() {
  165. move();
  166.  
  167. checkPos();
  168. drawA();
  169. checkDestroy();//not really used
  170. }
  171.  
  172. }
Add Comment
Please, Sign In to add comment