[solved I think?]Bouncing objects off "walls"

I see what you mean now h3ckboy. If only I could tweak things by their angles verses vectors…But everyone so far has said this is vector math.

hmm… time to try this from a different “angle” har har har…

I think the problem is this.

The “vectors” I made aren’t real vectors. And I still cannot find a way to programmaticly(made-up word) turn a LINE (two end-points{X1,Y1}{X2,Y2}) into a “direction vector”. Anyone just wanna help me with the understanding of this, or do I actually already have it? Link’s, concepts, anything and everything would be appreciated, right now it’s looking like 14 hours of wasted time.

make a class called, “vector 2D”

here is what it should have


public class Vector2D
{
     public float x;
     public float y;

     public Vector2D(float setX, float setY)
     {
          x = setX;
          y = setY;
     }
}

yes this is what demon pants gave me ;).

here’s my vector class


public class Vector2D {
    public float X = 0.0f, Y = 0.0f;

    public Vector2D() {}
    public Vector2D( float x, float y ) { X = x; Y = y; }
    public Vector2D( Vector2D a ) { X = a.X; Y = a.Y; }
    
    public float length() {
        return (float) Math.sqrt( (X * X) + (Y * Y) );
    }

    public Vector2D normalize(){
        float len = length();
        if (len > 0f || len < 0f) {
            X /= len;
            Y /= len;
        }
        return new Vector2D(X, Y);
    }
    
    public float dotProduct( Vector2D A, Vector2D B) {                       
        return (A.X * B.X) + (A.Y * B.Y);
    }

    public Vector2D add( Vector2D a ) {
        X += a.X;
        Y += a.Y;
        return new Vector2D(X, Y);
    }
    
    public Vector2D sub( Vector2D a ) {
        X -= a.X;
        Y -= a.Y;
        return new Vector2D(X, Y);
    }

    public Vector2D mul( Vector2D a ) {
        X *= a.X;
        Y *= a.Y;
        return new Vector2D(X, Y);
    }

    public Vector2D mul( float scalar ) {
        X *= scalar;
        Y *= scalar;
        return new Vector2D(X, Y);
    }
    
}

But how do you turn a line into a vector if you only know it’s two end points?

edit:
I found this:

[quote]In order to write down the vector equation of this line, we need to know two things.

* We have to know the position vector of some point which lies on the line, like a on my diagram.
* We have to know a vector which gives the direction of the line, like b in my diagram. This is called a direction vector. 

Then the position vector r of any general point P on the line is given by the equation
r = a + tb
[/quote]
but to be honest… I don’t really understand how to program the “direction vector” thing?

Edit #2: Now I’m REALLLY confused. For a direction vector you need degrees?
http://www.analyzemath.com/vector_calculators/magnitude_direction.html
so do I need a direction vector, or just a vector, or the vector equation of a line(consisting of both)?

edit # 3: Wait so a vectors direction is derived from the line it makes from the origin 0,0?

wow… FINALLLY SOLVED.
I only tested it on one wall so I’m not 100%, but I think this is it :slight_smile:


//Start point of Wall line
Vector2D collisionV1 = new Vector2D(wall.X - (wall.Width/2), wall.Z - (wall.Depth/2));
//End point of Wall line
Vector2D collisionV2 = new Vector2D(wall.X + (wall.Width/2), wall.Z - (wall.Depth/2));
				
//Create Line Vector
Vector2D surfaceVector = collisionV2.sub(collisionV1);
				
//Start point of intersecting line
Vector2D velocityVector1 = ballBehavior.toVector2D();
//End Point of the intersecting line
Vector2D velocityVector2 = ballBehavior.movePointForward(10f).toVector2D();
//Create Line Vector
Vector2D velocityVector = velocityVector2.sub(velocityVector1);
				
//Normalize the two surface vector
surfaceVector = surfaceVector.normalize();
				
//Get the Normal by flipping the X and Y
Vector2D normal = new Vector2D( surfaceVector.Y, surfaceVector.X);
				
float vectorX = surfaceVector.dotProduct(surfaceVector, velocityVector);
float vectorY = surfaceVector.dotProduct(normal, velocityVector);
//Make the VectorY negative as to "flip" the collision
vectorY = -vectorY;
							
Vector2D mathVector = normal.mul(vectorY);//To simplify the final operation
Vector2D bouncedVector = surfaceVector.mul(vectorX).add(mathVector);
							
//Place on Object on the Point of the reflection
reflect = new Point2D(bouncedVector.X, bouncedVector.Y);