Mathematical vectors

After having gone through http://forums.tigsource.com/index.php?topic=14154.0 I realize I may have jumped the gun by trying to implement vectors seeing as I still don’t fully grasp the concept of a vector. I understand that velocity, position, and from the article (acceleration?) is a vector with a direction and a magnitude but don’t understand how that is possible (for velocity and acceleration) since we represent a vector as a point and not a scalar. For example speed = velocity without the direction. I don’t understand how we can represent this. I mean how does velocity have a magnitude? ??? If I take a look at


currentSpeed = Math.min(+topspeed, currentSpeed + acceleration * deltaSeconds);

currentSpeed is a scalar value. This is what I use to represent velocity (or rather speed since there is no magnitude) in my game. Can someone please fill in the missing blank?

Holy smokes what a confusing thread!! Just to confirm, this has nothing to do with the Java Vector class, correct? :stuck_out_tongue:

It seems to me like points in space are getting confused with magnitudes and directions. For example, a Point (3,4) is just a point in space, a location. But a Vector (3,4), pertains to the distance and direction from (0,0) to (3,4). The “Magnitude” of this Vector is 5, which can be calculated using the pythagorian formula, as the distance from (0,0) to (3,4). The “Direction” can be stated in either pi-radians or in degrees, usually involves something like an arctan function to derive it.

Velocity can be expressed as a Vector, it has a magnitude and a direction. Acceleration can be expressed as a Vector, it has a magnitude and a direction. Position (a point in space, the location at a given point in time for your spaceship or car or whatever) is NOT a Vector.

Maybe keeping these things straight will alleviate confusion. At least, I think I’m a little less confused now. I hope I didn’t make things worse!

In your car game the car always moves in the direction it is pointing. You’re storing the car’s direction (carAngle) and speed (currentSpeed), and these together imply a velocity vector.

In the following, the velocity vector is (axcurrentSpeed, aycurrentSpeed). (To throw in a bit more terminology, (ax,ay) is a unit vector. Its magnitude is one, and it points in the direction of the car.)


double ax = Math.sin(Math.toRadians(carAngle));
double ay = -Math.cos(Math.toRadians(carAngle));
x += ax * currentSpeed * deltaSeconds;
y += ay * currentSpeed * deltaSeconds;

Yeah, this is a good point and an important conceptual distinction. I always use Vector2D’s in memory to represent every point I might need because in terms of data members they are exactly the same. In addition, you often need to do the same operations on a Point that are done on Vector2D’s (add, subtract, scale), and convert between them frequently. So in terms of code it makes sense to keep them the same.

If you really want the distinction, you can always make Point a subclass of Vector2D. Then you get both advantages.

A position IS a vector. It is a direction and distance from an arbitrary point in space (the origin).

Edit: It may be more accurate to say you can define the position as a vector in the above way when using the Cartesian coordinate system…

I think you’re mixing ‘is a’, ‘has a’ and ‘is composed of’… further you’re saying: “point = point + direction*distance”, that is recursion :slight_smile:

“a point can be described as a origin+vector”, that doesn’t mean that a point ‘is a’ vector, what you are describing is basically a transformation (only translating), which could just as well be a matrix.

“a plane can be described as a vector+distance”, again, the ‘is composed of’ relation, as you can also describe a plane using two (non collinear) vectors, or a point+normal.

You can define a position as a offset from the origin (a vector) or as several values (a point). There are position vectors, also sometimes called a radius vector, and closely related with displacement vectors. If you are using Cartesian coordinates, these are all pretty much equivalent, but not necessarily so in any other basis.

Ahhhh sorry it took so long guys, was a very busy week / weekend, haven’t really had time to work on my project :-. Anyway if I take a look at what you say dishmoth, and velocity is a unit vector (in my case) - does this mean that the length of velocity will always be 1? for instance it would always be one pixel in length(magnitude)? In addition, when you tell me that velocity would be my carAngle which is direction and currentSpeed which is speed (which is not magnitude) how could this be a vector since I don’t have a magnitude. To me it would represent a Point.

About position though I’ll be honest and say that I wouldn’t get how it could be a vector, since there is no magnitude? This also would make more sense to me if it was only a Point. - since in neither case do I see a translation from the origin to the destination.

For velocity and position to be a vector they both need a magnitude if I understand the definition correctly.

Well, I was saying that in your game (ax,ay) (not velocity) is a unit vector. That vector always has length (magnitude) equal to one. That vector is constructed so that it always points in the direction your car’s facing. You could call (ax,ay) the car’s direction vector. If you like, you can look at how that vector behaves as you turn the car by adding a line like the following to your code:

System.out.println("(" +ax + "," + ay + ") length=" + Math.sqrt(ax*ax+ay*ay)); // length should always be one

Your velocity vector is constructed by multiplying (ax,ay) by the current speed. That is, (vx,vy) = (currSpeedax,currSpeeday). The direction of the velocity vector is the same as the direction of (ax,ay), i.e., the car’s direction. The length (magnitude) of the velocity vector is equal to the car’s speed. Again, you can look at what’s happening in the code:

System.out.println("velocity length=" + Math.sqrt(vx*vx+vy*vy) + " speed=" + currSpeed); // both values should be the same 

Does that make sense?
Simon

If you change position 1 pixel in the horizontal (X direction only) OR 1 pixel in the vertical (Y direction only) in a “frame”, you are moving at a speed or velocity of 1 pixel per frame.

But if you go both 1 pixel X AND 1 pixel Y (45 degrees) in a “frame”, you are moving at a speed or velocity of 1.4+ pixels per frame.

If you want to move 1 pixel, regardless of direction, (in other words, have a magnitude for your vector) you have to store your location as a float or double, and use trig to extract the X & Y components to get the next location. For example, if moving 1 pixel per frame with the direction of 45 degrees, X = 0.7+ and Y= 0.7+

as in

X = 1 * cos (45 degrees)
Y = 1 * sin (45 degrees)

X distance per frame = magnitude * cos(direction),
Y distance per frame = magnitude * sin(direction)

Alright dishmoth, I think I got the grasp of it, I’d just like a few of my thoughts ironed out.

I understand that direction is a unit vector - and velocity is not since we keep adding the acceleration vector to it. Does this leave position to be a unit vector also? Since position doesn’t really have a magnitude (unless its the size of the image?).

I’m guessing I can test if it is a unit vector by simply printing out Math.sqrt(position.x * position.x + position.y * position.y). Is that what this formula is all about? giving the magnitude of a certain vector?

I’ve changed how the car class works to interact with a Vector2D class but I’m not totally sure that I’m doing things the right way. I’ll post a snippet of what I mean.

This is the mathematical vector class


public class Vector2D {
	private double x;
	private double y;
	
	public Vector2D(double x, double y) {
		this.x = x;
		this.y = y;
	}
	
	public static Vector2D add(Vector2D v1, Vector2D v2) {
		return new Vector2D(v1.x + v2.x, v1.y + v2.y);
	}
	
	public double getX() {
		return x;
	}
	
	public double getY() {
		return y;
	}
}

and some implementation in my car’s movement method


...
direction = new Vector2D(Math.sin(Math.toRadians(carAngle)), -Math.cos(Math.toRadians(carAngle)));
velocity = new Vector2D(direction.getX() * currentSpeed * deltaSeconds, direction.getY() * currentSpeed * deltaSeconds);
position = Vector2D.add(velocity, position);
...

and I still don’t fully understand why everything except velocity can be considered a vector - instead of a Point? Or maybe a vector is just a Point with a direction? ahhh I’m so confused. The thing is I know once I can get the theory down the implementation would be no problem.

Oh and by the way, philfrei I understand what you mean, this was taken care of in another thread I posted asking about top down rotation motion :slight_smile: - thanks though!

Im not sure if you’ve got the meaning of Vector and Point properly. This is my understanding.

A Point is something with one variable, such as a speed. This can only give a speed of something, not a direction.

Then you have a Vector. These have two variables. Velocity is the common example as it has a magnitude and a direction.

More examples of points: Distance, Mass, Temperature

More examples of Vectors: Displacement, Force, Acceleration

Hope this helps clear things up. Sorry if you know this already, I made have misread what you think about it all.

[quote=“Kieran_Newland,post:32,topic:36402”]
A scalar is a single variable, eg speed, distance &c.
A point is a point in space eg (x,y) or (x,y,z).

@deadly72: It can be confusing!
I think of it this way; a point is where something is, say (1,0) for a knight on a chessboard. A vector tells you how to move the knight, eg (1,2) - one sideways and two forward - so if you add the vector to the point you get a new point (2,2) which gives the new position of the knight.
In your code ‘position’ is a point (where the car is) and ‘velocity’ is a vector (how to move it).
BTW, why are you making the cos part of ‘direction’ negative?

@SimonH
The example really helps clear up my misconception of vectors, and if I may be so bold to say so - would a simple


public static Point add(Point p, Vector2D v) {
    return new Point(p.x + v.x, p.y + v.x);
}

be the conversion to add the vector to the point?

I’d like one thing still cleared up though. Since a vector equals a direction and a magnitude, then isn’t the following statement wrong?


velocity = new Vector2D(direction.getX() * currentSpeed * deltaSeconds, direction.getY() * currentSpeed * deltaSeconds);

Since there is no magnitude. Also that leaves me to believe that the constructor for Vector2D would need 3 parameters? Right now I only have x,y. I would need to add a third parameter that takes magnitude?

@Kieran
Thanks, but I have to be honest and say that SimonH’s example was a clear explanation that now should keep making me think that a vector is how you move :). But yea I’m thinking the same thing here when you - well everyone says - when you talk about velocity. I understand it has a direction but the magnitude is literally the problem. It is confusing me.

I think of velocity in terms of a point. For example lets say we went ahead and said for a really basic rendering of the move method that x += 5, and y += 5. This makes me think that velocity = 5px/seconds for example, I don’t see the connection to magnitude.

That’s correct :slight_smile:

Yes that is wrong. It should be:


velocity = new Vector2D(Math.sin(angle)*(currentSpeed*deltaSeconds),Math.cos(angle)*(currentSpeed*deltaSeconds));

Magnitude is the hypotenuse of the sides a and b. Direction is the angle.

The magnitude is sqrt(55+55) and the direction is 45 degrees.

Well, you could do it that way, but it’s conventional to use Vector2D to hold both points and vectors and use their naming to remind you which is which (ie position is a point, direction is a vector, though they’re both stored as Vector2D)

There is magnitude - the ‘direction’ vector has a magnitude of 1 (from cos,sin) which you multiply by speeddt, so ‘velocity’ has a magnitude of 1speeddt - which is what you want - the car will move 'speeddt’ units in ‘direction’s direction (if you follow me!)
Example: Vector2D(2,2) can either be a point at 2,2 or it can be a vector with magnitude sqrt(22+22) and a direction of 45’ from where it starts. So the (5,5) velocity vector you mention has a magnitude of sqrt(55+55) = sqrt(50) = 7.07, and has a direction of 45’.
I’d be worth you reading up on vectors to get yourself clear on them - you’re very nearly there!

My attempt at a very basic summary:

A (two-dimensional) vector is a pair of numbers (a,b).
Vectors can be added: (a,b) + (c,d) = (a+c,b+d).
A vector can be multiplied by a scalar: s*(a,b) = (sa,sb).
The magnitude (or length) of vector (a,b) equals sqrt(aa+bb). (The magnitude cannot be less than zero.)
The zero vector is (0,0). Its magnitude is zero.
If a vector has magnitude equal to one, then it is a unit vector.
You can normalize any vector (except the zero vector) by dividing it by its magnitude: (c,d) = (a,b)/sqrt(aa+bb). The result is a unit vector.
Any unit vector can be represented by an angle: (a,b) = (cos(theta),sin(theta)). The angle theta equals atan2(b,a).
You can think of both unit vectors and angles as (equivalent) ways of representing direction.
So, for any vector you can calculate its magnitude, and normalize it to find its direction (unit vector). (The zero vector is the exception; you can’t normalize it, it doesn’t have a direction.)
I think this is what you mean when you say that “a vector equals a direction and a magnitude”. In fact, any pair of numbers equals a direction and a magnitude if you want to think of it like that. :wink:

Are there any bits of that that don’t make sense?
Simon

A position is a vector - but for this to make sense you have to define a special position called the origin. The origin has coordinates (0,0). In your game the origin is the position of the top-left corner of the window, since that’s where Java2D happens to put it.

A vector for a position is the step you have to take to get from the origin to that position. The vector’s magnitude is the distance from the origin to the position. The vector’s direction is the direction from the origin to the position.

Knowing the magnitude and direction of a position relative to the top-left corner of the window is generally not that useful. But, useful or not, a position vector still has a magnitude and direction.

Simon

This is over my head, can someone explain it to me better? http://mathworld.wolfram.com/Reflection.html

cyan this is probably best discussed in another thread, titled reflection, and maybe state what areas dont you understand.
Are you programming in 3D now? Do you understand what “normals” are?

but yeah like I said maybe start it in a new thread and people who have had experience with reflection should be able to help you.