Particle System problem (collision detection and handeling)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Particle System problem (collision detection and handeling)

 
0
  #1
Jun 29th, 2008
i am getting strange results and cannot figure out why:

Vel CONTROL (the vector 1,0), and Acc are Vec2D objects, which has X and Y values, and methods such as add, angle between, and other such vector util
particle extends ellipse2d.double updateSuper() simply changes the values for it
OLine extends line2d.double
i cannot remember why the for loop uses object

Vel.Y=-Vel.Y is to correct for java's coordinate system being +=down

i think the problem may be a missed conversion between radians and degrees, but can't find it.

  1. public void move(){
  2. X = X + Vel.X;
  3. Y = Y + Vel.Y;
  4. updateSuper();
  5. Vel.add(acc);
  6. life = life - 0.5;
  7. for(Object l:lines){
  8. OLine ol = (OLine)l;
  9. if(ol.ptSegDist(X,Y)<=radius){
  10. // System.out.println("XVel B: " + Vel.X);
  11. // System.out.println("YVel B: " + Vel.Y);
  12. // System.out.println("XN: " + ol.Normal.X);
  13. // System.out.println("YN: " + ol.Normal.Y);
  14.  
  15. double normAngle = Vec2d.angleBetween(CONTROL,ol.Normal);
  16. double velAngle = Vec2d.angleBetween(CONTROL,Vel);
  17. double addAngle = Vec2d.angleBetween(ol.Normal,Vel);
  18. double newAngle;
  19. if(ol.Normal.Y<0){
  20. if(ol.Normal.X<0){
  21. normAngle = Math.PI + (Math.PI - normAngle);
  22. } else {
  23. normAngle = Math.PI + normAngle;
  24. }
  25. }
  26. if(Vel.Y<0){
  27. if(Vel.X<0){
  28. velAngle = Math.PI + (Math.PI - velAngle);
  29. } else {
  30. velAngle = Math.PI + velAngle;
  31. }
  32. }
  33. if(velAngle<=normAngle){
  34. newAngle = normAngle + addAngle;
  35. } else {
  36. newAngle = normAngle - addAngle;
  37. }
  38. //Vec2d PVel = Vel.copY();
  39. Vel = new Vec2d((Vel.magnitude())*Math.cos(newAngle),(Vel.magnitude())*Math.sin(newAngle));
  40.  
  41. Vel.Y = -Vel.Y;
  42. // System.out.println("Nangle: " + Math.toDegrees(normAngle));
  43. // System.out.println("Vangle: " + Math.toDegrees(velAngle));
  44. // System.out.println("Aangle: " + Math.toDegrees(addAngle));
  45. // System.out.println("NEWangle: " + Math.toDegrees(newAngle));
  46. //
  47. // System.out.println("XVel: " + Vel.X);
  48. // System.out.println("YVel: " + Vel.Y);
  49. }
  50. }
  51. }

any questions on the reasoning that may help will be gladly answered.

this is the result i am getting (the red particles originate at the blue point): Click image for larger version

Name:	particles.jpg
Views:	6
Size:	26.0 KB
ID:	6469

i gave up on this a while ago, but after getting my other questions from other projects answered, this came back to my mind, help would be appreciated
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Particle System problem (collision detection and handeling)

 
0
  #2
Jun 30th, 2008
anyone, anything, nobody can help with this?
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Particle System problem (collision detection and handeling)

 
0
  #3
Jun 30th, 2008
Originally Posted by sciwizeh View Post
anyone, anything, nobody can help with this?
First off, if you're using the Math class' static final variables this much you should consider a static import.

Secondly I'd have to see how you are calculating with your Vec2D class. I'm assuming that you are using a class you created yourself so I wanted to check it.

I also have a vector-collision class in C++ that can easily be modified to fit in Java if you need it - it's one of my code snippets.

And also I was just about to ask why the for loop does use Object - if it is generalized then you should do something like this--

  1.  
  2. for(int i = 0; i < lines.length; i++)
  3. {
  4.  
  5. if(lines[i] instanceof Oline)
  6. {
  7.  
  8. //code here--
  9.  
  10. }
  11.  
  12.  
  13. }

--and it's not clear what you're using to determine collisions. If you're using strictly vector-to-vector collisions then consider the code I have posted for C++ snippet. If not please give more details.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Particle System problem (collision detection and handeling)

 
0
  #4
Jun 30th, 2008
thanks for the reply,
i didn't think of the static import good idea.
the vec2d class (not originally written by me actually, found it originally for 3d):
  1. /**
  2.  * A class to describe a two or three dimensional vector.
  3.  * <br>
  4.  * Created for use in examples from the Nature of Code course at ITP.
  5.  * <p>
  6.  * <a href="http://www.shiffman.net">http://www.shiffman.net/</a>
  7.  * <br>
  8.  * <a href="http://www.shiffman.net/teaching/the-nature-of-code">http://www.shiffman.net/teaching/the-nature-of-code</a>
  9.  */
  10.  
  11. public class Vec2d {
  12. /**
  13.   * The X component of the vector.
  14.   */
  15. public double X;
  16. /**
  17.   * The y component of the vector.
  18.   */
  19. public double Y;
  20. /**
  21.   * The z component of the vector.
  22.   */
  23. public double Z;
  24.  
  25. /**
  26.   * Constructor for a 2d vector.
  27.   *
  28.   * @param X_ the X coordinate.
  29.   * @param Y_ the Y coordinate.
  30.   * @param Z_ the Y coordinate.
  31.   */
  32.  
  33. public Vec2d(double X_, double Y_, double Z_) {
  34. X = X_; Y = Y_; Z = Z_;
  35. }
  36.  
  37. /**
  38.   * Constructor for a 2D vector: Z coordinate is set to 0.
  39.   *
  40.   * @param X_ the X coordinate.
  41.   * @param Y_ the Y coordinate.
  42.   */
  43.  
  44. public Vec2d(double X_, double Y_) {
  45. X = X_; Y = Y_; Z = 0f;
  46. }
  47.  
  48. /**
  49.   * Constructor for an emptY vector: X, Y, and Z are set to 0.
  50.   */
  51.  
  52. public Vec2d() {
  53. X = 0f; Y = 0f; Z = 0f;
  54. }
  55.  
  56. /**
  57.   * Set the X coordinate.
  58.   *
  59.   * @param X_ the X coordinate.
  60.   */
  61.  
  62. public void setX(double X_) {
  63. X = X_;
  64. }
  65.  
  66. /**
  67.   * Set the Y coordinate.
  68.   *
  69.   * @param Y_ the Y coordinate.
  70.   */
  71. public void setY(double Y_) {
  72. Y = Y_;
  73. }
  74.  
  75. /**
  76.   * Set the Z coordinate.
  77.   *
  78.   * @param Z_ the Z coordinate.
  79.   */
  80. public void setZ(double Z_) {
  81. Z = Z_;
  82. }
  83.  
  84. /**
  85.   * Set X,Y, and Z coordinates.
  86.   *
  87.   * @param X_ the X coordinate.
  88.   * @param Y_ the Y coordinate.
  89.   * @param Z_ the Z coordinate.
  90.   */
  91. public void setXYZ(double X_, double Y_, double Z_) {
  92. X = X_;
  93. Y = Y_;
  94. Z = Z_;
  95. }
  96.  
  97. /**
  98.   * Set X,Y, and Z coordinates from a Vec2d object.
  99.   *
  100.   * @param v the Vec2d object to be copied
  101.   */
  102. public void setXYZ(Vec2d v) {
  103. X = v.X;
  104. Y = v.Y;
  105. Z = v.Z;
  106. }
  107.  
  108. /**
  109.   * Calculate the magnitude (length) of the vector
  110.   * @return the magnitude of the vector
  111.   */
  112. public double magnitude() {
  113. return (double) Math.sqrt(X*X + Y*Y + Z*Z);
  114. }
  115.  
  116. /**
  117.   * CopY the vector
  118.   * @return a copY of the vector
  119.   */
  120. public Vec2d copY() {
  121. return new Vec2d(X,Y,Z);
  122. }
  123.  
  124. /**
  125.   * CopY the vector
  126.   * @param v the vector to be copied
  127.   * @return a copY of the vector
  128.   */
  129. public static Vec2d copY(Vec2d v) {
  130. return new Vec2d(v.X, v.Y,v.Z);
  131. }
  132.  
  133. /**
  134.   * Add a vector to this vector
  135.   * @param v the vector to be added
  136.   */
  137. public void add(Vec2d v) {
  138. X += v.X;
  139. Y += v.Y;
  140. Z += v.Z;
  141. }
  142.  
  143. /**
  144.   * Subtract a vector from this vector
  145.   * @param v the vector to be subtracted
  146.   */
  147. public void sub(Vec2d v) {
  148. X -= v.X;
  149. Y -= v.Y;
  150. Z -= v.Z;
  151. }
  152.  
  153. /**
  154.   * MultiplY this vector bY a scalar
  155.   * @param n the value to multiplY bY
  156.   */
  157. public void mult(double n) {
  158. X *= n;
  159. Y *= n;
  160. Z *= n;
  161. }
  162.  
  163. /**
  164.   * Divide this vector bY a scalar
  165.   * @param n the value to divide bY
  166.   */
  167. public void div(double n) {
  168. X /= n;
  169. Y /= n;
  170. Z /= n;
  171. }
  172.  
  173.  
  174. /**
  175.   * Calculate the dot product with another vector
  176.   * @return the dot product
  177.   */
  178. public double dot(Vec2d v) {
  179. double dot = X*v.X + Y*v.Y;
  180. return dot;
  181. }
  182.  
  183. /**
  184.   * Calculate the cross product with another vector
  185.   * @return the cross product
  186.   */
  187. public Vec2d cross(Vec2d v) {
  188. double crossX = Y * v.Z - v.Y * Z;
  189. double crossY = Z * v.X - v.Z * X;
  190. double crossZ = X * v.Y - v.X * Y;
  191. return(new Vec2d(crossX,crossY,crossZ));
  192. }
  193.  
  194. /**
  195.   * NormaliZe the vector to length 1 (make it a unit vector)
  196.   */
  197. public void normalize() {
  198. double m = magnitude();
  199. if (m > 0) {
  200. div(m);
  201. }
  202. }
  203.  
  204. /**
  205.   * Limit the magnitude of this vector
  206.   * @param maX the maXimum length to limit this vector
  207.   */
  208. public void limit(double maX) {
  209. if (magnitude() > maX) {
  210. normalize();
  211. mult(maX);
  212. }
  213. }
  214.  
  215. /**
  216.   * Calculate the angle of rotation for this vector (onlY 2D vectors)
  217.   * @return the angle of rotation
  218.   */
  219. public double heading2D() {
  220. double angle = (double) Math.atan2(-Y, X);
  221. return -1*angle;
  222. }
  223.  
  224. /**
  225.   * Add two vectors
  226.   * @param v1 a vector
  227.   * @param v2 another vector
  228.   * @return a new vector that is the sum of v1 and v2
  229.   */
  230. public static Vec2d add(Vec2d v1, Vec2d v2) {
  231. Vec2d v = new Vec2d(v1.X + v2.X,v1.Y + v2.Y, v1.Z + v2.Z);
  232. return v;
  233. }
  234.  
  235. /**
  236.   * Subtract one vector from another
  237.   * @param v1 a vector
  238.   * @param v2 another vector
  239.   * @return a new vector that is v1 - v2
  240.   */
  241. public static Vec2d sub(Vec2d v1, Vec2d v2) {
  242. Vec2d v = new Vec2d(v1.X - v2.X,v1.Y - v2.Y,v1.Z - v2.Z);
  243. return v;
  244. }
  245.  
  246. /**
  247.   * Divide a vector bY a scalar
  248.   * @param v1 a vector
  249.   * @param n scalar
  250.   * @return a new vector that is v1 / n
  251.   */
  252. public static Vec2d div(Vec2d v1, double n) {
  253. Vec2d v = new Vec2d(v1.X/n,v1.Y/n,v1.Z/n);
  254. return v;
  255. }
  256.  
  257. /**
  258.   * MultiplY a vector bY a scalar
  259.   * @param v1 a vector
  260.   * @param n scalar
  261.   * @return a new vector that is v1 * n
  262.   */
  263. public static Vec2d mult(Vec2d v1, double n) {
  264. Vec2d v = new Vec2d(v1.X*n,v1.Y*n,v1.Z*n);
  265. return v;
  266. }
  267.  
  268.  
  269. /**
  270.   * Calculate the Euclidean distance between two points (considering a point as a vector object)
  271.   * @param v1 a vector
  272.   * @param v2 another vector
  273.   * @return the Euclidean distance between v1 and v2
  274.   */
  275. public static double distance (Vec2d v1, Vec2d v2) {
  276. double dX = v1.X - v2.X;
  277. double dY = v1.Y - v2.Y;
  278. double dZ = v1.Z - v2.Z;
  279. return (double) Math.sqrt(dX*dX + dY*dY + dZ*dZ);
  280. }
  281.  
  282. /**
  283.   * Calculate the angle between two vectors, using the dot product
  284.   * @param v1 a vector
  285.   * @param v2 another vector
  286.   * @return the angle between the vectors
  287.   */
  288. public static double angleBetween(Vec2d v1, Vec2d v2) {
  289. double dot = v1.dot(v2);
  290. double theta = Math.acos(dot / (v1.magnitude() * v2.magnitude()));
  291. return theta;
  292.  
  293. }
  294. public String toString(){
  295. return "X: " + X + "\nY: " + Y;
  296. }
  297.  
  298. }

i have tested the angle between method, which is the only method i used

looking back at the code i remember why i used the Object for the loop, in the constructor for the class i pass the ArrayList and could not use the Generics for passing it, if you know a way it would be appreciated the only thing it holds it OLine's

the actual collision detection is in this statement:
if(ol.ptSegDist(X,Y)<=radius){
Last edited by sciwizeh; Jun 30th, 2008 at 2:50 pm.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Particle System problem (collision detection and handeling)

 
0
  #5
Jun 30th, 2008
Originally Posted by sciwizeh View Post
looking back at the code i remember why i used the Object for the loop, in the constructor for the class i pass the ArrayList and could not use the Generics for passing it, if you know a way it would be appreciated the only thing it holds it OLine's
The code that creates the list List<OLine> lines = new ArrayList<OLine>(); and the constructor public SomeThing(List<OLine> lines){
Last edited by Ezzaral; Jun 30th, 2008 at 3:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Particle System problem (collision detection and handeling)

 
0
  #6
Jun 30th, 2008
when i tried that it didn't work, maybe ill retry, but i really want help on the reflection of the particles
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Particle System problem (collision detection and handeling)

 
0
  #7
Jun 30th, 2008
Originally Posted by sciwizeh View Post
when i tried that it didn't work, maybe ill retry, but i really want help on the reflection of the particles
Usually in order to detect something like you have to constantly debug your program with less presented.

For example, does this happen with just one vector? Or does it only happen when you perform some kind of command with 2?

Here's the code for my point and vector classes in C++, but ill highlight the interesting part--

  1. class Point{
  2. private:
  3. double xP;
  4. double yP;
  5.  
  6. public:
  7. Point(double x, double y){
  8. xP = x;
  9. yP = y;
  10. };
  11. Point() {xP = 0; yP = 0;}
  12. void Set(double x,double y) {xP = x; yP = y;}
  13. double getX(){return xP;};
  14. double getY(){return yP;};
  15. void showCoord(){
  16. cout << "(" << getX() << ", " << getY() << ")" << endl;
  17. };
  18. };
  19.  
  20. /*
  21.  *class P2DVector
  22.  *Named P2DVector to differentiate between the vector class and 3D vectors.
  23.  **/
  24. class P2DVector{
  25. private:
  26. Point points[2];
  27.  
  28. public:
  29. P2DVector(Point& first, Point& second){
  30. points[0] = first;
  31. points[1] = second;
  32. };
  33. P2DVector() {points[0] = Point(0,0); points[1] = Point(0,0); }
  34. void Set(Point& first, Point& second){
  35. points[0] = first;
  36. points[1] = second;
  37. };
  38. double getXDir(){
  39. return (points[1].getX() - points[0].getX());
  40. };
  41. double getYDir(){
  42. return (points[1].getY() - points[0].getY());
  43. };
  44. double magnitude(){
  45. return sqrt( (pow( points[1].getX() - points[0].getX() ,2)
  46. +pow(points[1].getY() - points[0].getY() ,2)));
  47. };
  48. Point startPoint(){
  49. Point p(points[0].getX(), points[0].getY());
  50. return p;
  51. };
  52. Point endPoint(){
  53. Point p(points[1].getX(), points[1].getY());
  54. return p;
  55. };
  56. P2DVector unitP2DVector(){
  57. Point unitPoint[2];
  58. unitPoint[0].Set(points[0].getX() / magnitude(), points[0].getY() / magnitude());
  59. unitPoint[1].Set(points[1].getX() / magnitude(), points[1].getY() / magnitude());
  60. P2DVector temp (unitPoint[0], unitPoint[1]);
  61. return temp;
  62. };
  63. void displayLocation(){
  64. cout << "This P2DVector Starts-> " << "" << points[0].getX() << ", " << points[0].getY();
  65. cout << "\nEnds-] " << "\t\t\t" << points[1].getX() << ", " << points[1].getY();
  66. cout << "\nDirection: "<< "\t\t" << "<" << getXDir() << ", " << getYDir() <<">";
  67. cout << "\nContains magnitude: \t" << magnitude() << "\n" << endl;
  68. };
  69. bool operator==(P2DVector &other){
  70. double otherXDirection = other.getXDir();
  71. double otherYDirection = other.getYDir();
  72. double xDirection = getXDir();
  73. double yDirection = getYDir();
  74.  
  75. //The statements below are a solution to a system of equations for vector-to-vector collisions
  76. double time2 = ((other.startPoint().getY()-startPoint().getY())-
  77. (((other.getYDir())*((other.startPoint().getX())-(startPoint().getX())))
  78. /(other.getXDir())))/((getYDir())-((other.getYDir())*((getXDir())/(other.getXDir()))));
  79.  
  80. double time1 = ((startPoint().getX() - other.startPoint().getX())
  81. + ((getXDir()) * (time2)))/(other.getXDir());
  82.  
  83. return time1 >= 0 && time1 <=1 && time2 >= 0 && time2 <=1;
  84. };
  85. };


//interesting part here--
             bool operator==(P2DVector &other){
                  double otherXDirection = other.getXDir();
                  double otherYDirection = other.getYDir();
                  double xDirection = getXDir();
                  double yDirection = getYDir();
                 
                  //The statements below are a solution to a system of equations for vector-to-vector collisions
                  double time2 = ((other.startPoint().getY()-startPoint().getY())-
                  (((other.getYDir())*((other.startPoint().getX())-(startPoint().getX())))
                  /(other.getXDir())))/((getYDir())-((other.getYDir())*((getXDir())/(other.getXDir()))));    
                              
                  double time1 =  ((startPoint().getX() - other.startPoint().getX())
                                  + ((getXDir()) * (time2)))/(other.getXDir());
                                   
                 return time1 >= 0 && time1 <=1 && time2 >= 0 && time2 <=1;
             };
};

The comment states it all. The collisions are vector-to-vector based. If you need a 3D collision test I can probably make the class for you using a triple system but it wont be easy.

As for your particles reflecting, I seriously don't know what may be causing the problem. If possible please submit your code to me privately. I understand that you are probably doing this to improve your website and that's cool.

I can understand if you're being protective with your code and don't want to publicly view it but I honestly don't know if there's a logic flaw with your code the way it is or if its a small logic-flaw with the way the GUI is set up.

You're more than welcome to add my bit of code to your vector class, but other than that I am really not sure if I can help you with what you've shown so far.

I'll look at the equations though and see if any are at fault. Since you're using a class that isn't your own I'd guess that the flaw may reside in the calculations.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Particle System problem (collision detection and handeling)

 
0
  #8
Jun 30th, 2008
i don't mind my code being viewed, this just contains alot of code and would be cumbersome to post on the forum so now i uploaded a zip to my site, i compiled it in bluej but i expect it should work in anything you use, i don't have any real licenses or anything, but i dont want it used without permission

the code in the zip is set up the same way the image is

http://matrixpeckham.googlepages.com/source.zip
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 683 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC