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.

public void move(){
        X = X + Vel.X;
        Y = Y + Vel.Y;
        updateSuper();
        Vel.add(acc);
        life = life - 0.5;
        for(Object l:lines){
            OLine ol = (OLine)l;
            if(ol.ptSegDist(X,Y)<=radius){
//               System.out.println("XVel B: " + Vel.X);
  //             System.out.println("YVel B: " + Vel.Y);
    //           System.out.println("XN: " + ol.Normal.X);
      //         System.out.println("YN: " + ol.Normal.Y);
               
               double normAngle = Vec2d.angleBetween(CONTROL,ol.Normal);
               double velAngle = Vec2d.angleBetween(CONTROL,Vel);
               double addAngle = Vec2d.angleBetween(ol.Normal,Vel);
               double newAngle;
               if(ol.Normal.Y<0){
                    if(ol.Normal.X<0){
                        normAngle = Math.PI + (Math.PI - normAngle);
                    } else {
                        normAngle = Math.PI + normAngle;
                    }
                }
                if(Vel.Y<0){
                    if(Vel.X<0){
                        velAngle = Math.PI + (Math.PI - velAngle);
                    } else {
                        velAngle = Math.PI + velAngle;
                    }                    
                }
                if(velAngle<=normAngle){
                    newAngle = normAngle + addAngle;
                } else {
                    newAngle = normAngle - addAngle;
                }
               //Vec2d PVel = Vel.copY();
               Vel = new Vec2d((Vel.magnitude())*Math.cos(newAngle),(Vel.magnitude())*Math.sin(newAngle));
               
               Vel.Y = -Vel.Y;
//               System.out.println("Nangle: " + Math.toDegrees(normAngle));
  //             System.out.println("Vangle: " + Math.toDegrees(velAngle));
    //           System.out.println("Aangle: " + Math.toDegrees(addAngle));
      //         System.out.println("NEWangle: " + Math.toDegrees(newAngle));
        //       
          //     System.out.println("XVel: " + Vel.X);
            //   System.out.println("YVel: " + Vel.Y);
            }
        }
    }


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):


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

Attachments particles.jpg 26.03 KB

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--

for(int i = 0; i < lines.length; i++)
{

    if(lines[i] instanceof Oline)
    {

        //code here--

    }


}

--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.

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):

/**
 * A class to describe a two or three dimensional vector.
 * <br>
 * Created for use in examples from the Nature of Code course at ITP.
 * <p>
 * <a href="http://www.shiffman.net">http://www.shiffman.net/</a>
 * <br>
 * <a href="http://www.shiffman.net/teaching/the-nature-of-code">http://www.shiffman.net/teaching/the-nature-of-code</a>
 */

public class Vec2d {
    /**
     * The X component of the vector.
     */
    public double X;
    /**
     * The y component of the vector.
     */
    public double Y;
    /**
     * The z component of the vector.
     */
    public double Z;
    
    /**
     * Constructor for a 2d vector.
     *
     * @param  X_ the X coordinate.
     * @param  Y_ the Y coordinate.
     * @param  Z_ the Y coordinate.
     */
    
    public Vec2d(double X_, double Y_, double Z_) {
        X = X_; Y = Y_; Z = Z_;
    }
    
    /**
     * Constructor for a 2D vector: Z coordinate is set to 0.
     *
     * @param  X_ the X coordinate.
     * @param  Y_ the Y coordinate.
     */
    
    public Vec2d(double X_, double Y_) {
        X = X_; Y = Y_; Z = 0f;
    }
    
    /**
     * Constructor for an emptY vector: X, Y, and Z are set to 0.
     */
    
    public Vec2d() {
        X = 0f; Y = 0f; Z = 0f;
    }
    
    /**
     * Set the X coordinate.
     *     
     *  @param  X_ the X coordinate.
     */
    
    public void setX(double X_) {
        X = X_;
    }
    
    /**
     * Set the Y coordinate.
     *     
     *  @param  Y_ the Y coordinate.
     */
    public void setY(double Y_) {
        Y = Y_;
    }
    
    /**
     * Set the Z coordinate.
     *     
     *  @param  Z_ the Z coordinate.
     */
    public void setZ(double Z_) {
        Z = Z_;
    }
    
    /**
     * Set X,Y, and Z coordinates.
     *     
     *  @param  X_ the X coordinate.
     *  @param  Y_ the Y coordinate.
     *  @param  Z_ the Z coordinate.
     */
    public void setXYZ(double X_, double Y_, double Z_) {
        X = X_;
        Y = Y_;
        Z = Z_;
    }
    
    /**
     * Set X,Y, and Z coordinates from a Vec2d object.
     *     
     *  @param  v the Vec2d object to be copied
     */
    public void setXYZ(Vec2d v) {
        X = v.X;
        Y = v.Y;
        Z = v.Z;
    }
    
    /**
     * Calculate the magnitude (length) of the vector
     * @return      the magnitude of the vector    
     */
    public double magnitude() {
        return (double) Math.sqrt(X*X + Y*Y + Z*Z);
    }
    
    /**
     * CopY the vector
     * @return      a copY of the vector   
     */
    public Vec2d copY() {
        return new Vec2d(X,Y,Z);
    }
    
    /**
     * CopY the vector
     * @param      v the vector to be copied   
     * @return      a copY of the vector   
     */
    public static Vec2d copY(Vec2d v) {
        return new Vec2d(v.X, v.Y,v.Z);
    }
    
    /**
     * Add a vector to this vector
     * @param      v the vector to be added  
     */   
    public void add(Vec2d v) {
        X += v.X;
        Y += v.Y;
        Z += v.Z;
    }
    
    /**
     * Subtract a vector from this vector
     * @param      v the vector to be subtracted  
     */   
    public void sub(Vec2d v) {
        X -= v.X;
        Y -= v.Y;
        Z -= v.Z;
    }
    
    /**
     * MultiplY this vector bY a scalar
     * @param      n the value to multiplY bY 
     */     
    public void mult(double n) {
        X *= n;
        Y *= n;
        Z *= n;
    }
    
    /**
     * Divide this vector bY a scalar
     * @param      n the value to divide bY 
     */     
    public void div(double n) {
        X /= n;
        Y /= n;
        Z /= n;
    }
    
    
    /**
     * Calculate the dot product with another vector
     * @return  the dot product
     */     
    public double dot(Vec2d v) {
        double dot = X*v.X + Y*v.Y;
        return dot;
    }
    
    /**
     * Calculate the cross product with another vector
     * @return  the cross product
     */     
    public Vec2d cross(Vec2d v) {
        double crossX = Y * v.Z - v.Y * Z;
        double crossY = Z * v.X - v.Z * X;
        double crossZ = X * v.Y - v.X * Y;
        return(new Vec2d(crossX,crossY,crossZ));
    }
    
    /**
     * NormaliZe the vector to length 1 (make it a unit vector)
     */     
    public void normalize() {
        double m = magnitude();
        if (m > 0) {
            div(m);
        }
    }
    
    /**
     * Limit the magnitude of this vector
     * @param maX the maXimum length to limit this vector
     */     
    public void limit(double maX) {
        if (magnitude() > maX) {
            normalize();
            mult(maX);
        }
    }
    
    /**
     * Calculate the angle of rotation for this vector (onlY 2D vectors)
     * @return the angle of rotation
     */    
    public double heading2D() {
        double angle = (double) Math.atan2(-Y, X);
        return -1*angle;
    }
    
    /**
     * Add two vectors
     * @param      v1 a vector
     * @param v2 another vector   
     * @return a new vector that is the sum of v1 and v2  
     */   
    public static Vec2d add(Vec2d v1, Vec2d v2) {
        Vec2d v = new Vec2d(v1.X + v2.X,v1.Y + v2.Y, v1.Z + v2.Z);
        return v;
    }
    
    /**
     * Subtract one vector from another
     * @param      v1 a vector
     * @param v2 another vector   
     * @return a new vector that is v1 - v2 
     */    
    public static Vec2d sub(Vec2d v1, Vec2d v2) {
        Vec2d v = new Vec2d(v1.X - v2.X,v1.Y - v2.Y,v1.Z - v2.Z);
        return v;
    }
    
    /**
     * Divide a vector bY a scalar
     * @param      v1 a vector
     * @param n scalar 
     * @return a new vector that is v1 / n
     */ 
    public static Vec2d div(Vec2d v1, double n) {
        Vec2d v = new Vec2d(v1.X/n,v1.Y/n,v1.Z/n);
        return v;
    }
    
    /**
     * MultiplY a vector bY a scalar
     * @param      v1 a vector
     * @param n scalar 
     * @return a new vector that is v1 * n
     */ 
    public static Vec2d mult(Vec2d v1, double n) {
        Vec2d v = new Vec2d(v1.X*n,v1.Y*n,v1.Z*n);
        return v;
    }
    
    
    /**
     * Calculate the Euclidean distance between two points (considering a point as a vector object)
     * @param      v1 a vector
     * @param v2 another vector
     * @return the Euclidean distance between v1 and v2
     */ 
    public static double distance (Vec2d v1, Vec2d v2) {
        double dX = v1.X - v2.X;
        double dY = v1.Y - v2.Y;
        double dZ = v1.Z - v2.Z;
        return (double) Math.sqrt(dX*dX + dY*dY + dZ*dZ);
    }
    
    /**
     * Calculate the angle between two vectors, using the dot product
     * @param      v1 a vector
     * @param v2 another vector
     * @return the angle between the vectors
     */ 
    public static double angleBetween(Vec2d v1, Vec2d v2) {
        double dot = v1.dot(v2);
        double theta = Math.acos(dot / (v1.magnitude() * v2.magnitude()));
        return theta;
        
    }
    public String toString(){
        return "X: " + X + "\nY: " + Y;
    }
    
}

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){

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){

when i tried that it didn't work, maybe ill retry, but i really want help on the reflection of the particles

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--

class Point{     
      private:
             double xP;
             double yP;
             
      public:
             Point(double x, double y){
                   xP = x;
                   yP = y;
             };
             Point() {xP = 0; yP = 0;}
             void Set(double x,double y) {xP = x; yP = y;}
             double getX(){return xP;};
             double getY(){return yP;};
             void showCoord(){
                  cout << "(" << getX() << ", " << getY() << ")" << endl;
             };
};

/*
 *class P2DVector
 *Named P2DVector to differentiate between the vector class and 3D vectors.
 **/
class P2DVector{
      private:
             Point points[2]; 
      
      public:
             P2DVector(Point& first, Point& second){
                      points[0] = first;
                      points[1] = second;        
             };
             P2DVector() {points[0] = Point(0,0); points[1] = Point(0,0); }
             void Set(Point& first, Point& second){
                      points[0] = first;
                      points[1] = second;        
             };
             double getXDir(){
                    return (points[1].getX() - points[0].getX());
             };
             double getYDir(){
                    return (points[1].getY() - points[0].getY());
             };
             double magnitude(){
                    return sqrt( (pow( points[1].getX() - points[0].getX() ,2) 
                                 +pow(points[1].getY() - points[0].getY() ,2)));
             };
             Point startPoint(){
                   Point p(points[0].getX(), points[0].getY());
                   return p;
             };
             Point endPoint(){
                   Point p(points[1].getX(), points[1].getY());
                   return p;
             };
             P2DVector unitP2DVector(){      
                    Point unitPoint[2];
                    unitPoint[0].Set(points[0].getX() / magnitude(), points[0].getY() / magnitude());
                    unitPoint[1].Set(points[1].getX() / magnitude(), points[1].getY() / magnitude());
                    P2DVector temp (unitPoint[0], unitPoint[1]);
                    return temp;
             };
             void displayLocation(){
                  cout << "This P2DVector Starts-> " << "" << points[0].getX() << ", " << points[0].getY();
                  cout << "\nEnds-] " << "\t\t\t" << points[1].getX() << ", " << points[1].getY();
                  cout << "\nDirection: "<< "\t\t" << "<" << getXDir() << ", " << getYDir() <<">";
                  cout << "\nContains magnitude: \t" << magnitude() << "\n" << endl;
             };
             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;
             };
};

//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.

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

This article has been dead for over six months. Start a new discussion instead.