Advertisement
Guest User

ParcticleConstants

a guest
Mar 2nd, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. package src;
  2.  
  3. import com.jagex.org.Particle;
  4. import com.jagex.org.Position;
  5. import com.jagex.org.Velocity;
  6. import java.awt.Point;
  7. import java.awt.Polygon;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. /**
  12. * ParcticleConstants
  13. *
  14. * @author Seth Rogen
  15. * @Client Revison #317
  16. */
  17. public class ParcticleConstants implements Effect
  18. {
  19. private Polygon polygon = new Polygon();
  20.  
  21. private Segment[] segments;
  22.  
  23. /**
  24. * ParcticleConstants
  25. *
  26. * @param polygon Polygon
  27. */
  28. public ParcticleConstants( Polygon polygon )
  29. {
  30. this.polygon = polygon;
  31.  
  32. segments = new Segment[polygon.npoints];
  33.  
  34. int[] xs = polygon.xpoints;
  35. int[] ys = polygon.ypoints;
  36.  
  37. for ( int i = 0; i < polygon.npoints - 1; i++ )
  38. {
  39. segments[i] = new Segment( xs[i], ys[i],
  40. xs[i+1], ys[i+1] );
  41. }
  42.  
  43. segments[polygon.npoints-1] = new Segment( xs[polygon.npoints-1], ys[polygon.npoints-1],
  44. xs[0], ys[0] );
  45. }
  46.  
  47. /**
  48. * contains
  49. *
  50. * @param x int
  51. * @param y int
  52. * @return boolean
  53. */
  54. @Override
  55. public boolean contains( int x, int y )
  56. {
  57. return polygon.contains( x, y );
  58. }
  59.  
  60. /**
  61. * createParticles
  62. *
  63. * @param rate int
  64. * @return List<Particle>
  65. */
  66. @Override
  67. public List<Particle> createParticles( int rate )
  68. {
  69. List<Particle> list = new ArrayList<Particle>();
  70.  
  71. for ( int i = 0; i < rate; i++ )
  72. {
  73. Particle p = new Particle();
  74.  
  75. Point point = getValidPoint();
  76.  
  77. p.setCurrentPosition( new Position( point.getX(), point.getY() ) );
  78. p.setPreviousPosition( new Position( point.getX(), point.getY() ) );
  79.  
  80. list.add( p );
  81. }
  82.  
  83. return list;
  84. }
  85.  
  86. /**
  87. * getValidPoint
  88. *
  89. * @return Point
  90. */
  91. private Point getValidPoint()
  92. {
  93. Point point = new Point( (int) (polygon.getBounds().x + polygon.getBounds().width * Math.random()),
  94. (int) (polygon.getBounds().y + polygon.getBounds().height * Math.random()) );
  95.  
  96. while ( !polygon.contains( point ) )
  97. {
  98. point = new Point( (int) (polygon.getBounds().x + polygon.getBounds().width * Math.random()),
  99. (int) (polygon.getBounds().y + polygon.getBounds().height * Math.random()) );
  100. }
  101.  
  102. return point;
  103. }
  104.  
  105. /**
  106. * bounce
  107. *
  108. * @param p Particle
  109. * @param resilience float
  110. */
  111. @Override
  112. public void bounce( Particle p, float resilience )
  113. {
  114. for ( int i = 0; i < segments.length; i++ )
  115. {
  116. checkCollision( segments[i], p, resilience );
  117. }
  118. }
  119.  
  120. /**
  121. * checkCollision
  122. *
  123. * @param segment Segment
  124. * @param p Particle
  125. * @param resilience float
  126. */
  127. private void checkCollision( Segment segment, Particle p, float resilience )
  128. {
  129. // Get difference between orb and ground
  130. double deltaX = p.getCurrentPosition().x - segment.x;
  131. double deltaY = p.getCurrentPosition().y - segment.y;
  132.  
  133. // Precalculate trig values
  134. double cosine = Math.cos( segment.rot );
  135. double sine = Math.sin( segment.rot );
  136.  
  137. /* Rotate ground and velocity to allow
  138. orthogonal collision calculations */
  139. double groundXTemp = cosine * deltaX + sine * deltaY;
  140. double groundYTemp = cosine * deltaY - sine * deltaX;
  141. double velocityXTemp = cosine * p.getCurrentVelocity().dx + sine * p.getCurrentVelocity().dy;
  142. double velocityYTemp = cosine * p.getCurrentVelocity().dy - sine * p.getCurrentVelocity().dx;
  143.  
  144. /* Ground collision - check for surface
  145. collision and also that orb is within
  146. left/rights bounds of ground segment */
  147. if ( groundYTemp > -p.getSize()
  148. && p.getCurrentPosition().x > segment.x1
  149. && p.getCurrentPosition().x < segment.x2 )
  150. {
  151. // keep orb from going into ground
  152. groundYTemp = -p.getSize();
  153. // bounce and slow down orb
  154. velocityYTemp *= -1.0;
  155. velocityYTemp *= resilience;
  156. }
  157.  
  158. // Reset ground, velocity and orb
  159. deltaX = cosine * groundXTemp - sine * groundYTemp;
  160. deltaY = cosine * groundYTemp + sine * groundXTemp;
  161.  
  162. p.setCurrentVelocity( new Velocity( cosine * velocityXTemp - sine * velocityYTemp,
  163. cosine * velocityYTemp + sine * velocityXTemp ) );
  164. }
  165.  
  166. /**
  167. * Segment
  168. *
  169. * @return class
  170. */
  171. public class Segment
  172. {
  173. float x1, y1, x2, y2;
  174. float x, y, len, rot;
  175.  
  176. /**
  177. * Segment
  178. *
  179. */
  180. Segment()
  181. {
  182. }
  183.  
  184. /**
  185. * Segment
  186. *
  187. * @param x1 float
  188. * @param y1 float
  189. * @param x2 float
  190. * @param y2 float
  191. */
  192. Segment( float x1, float y1, float x2, float y2 )
  193. {
  194. this.x1 = x1;
  195. this.y1 = y1;
  196. this.x2 = x2;
  197. this.y2 = y2;
  198. x = (x1 + x2) / 2;
  199. y = (y1 + y2) / 2;
  200.  
  201. len = (float) Point.distance( x1, y1, x2, y2 );
  202. rot = (float) Math.atan2( (y2 - y1), (x2 - x1) );
  203. }
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement