Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Structuring the components of an engine that I am developing along with a friend (learning purposes), I came to this doubt.

Initially we had a Point constructor, like the following:

var Point = function( x, y ) {
    this.x = x;
    this.y = y;
};

But them we started to add some Vector math to it, and them decided to rename it to Vector2d.

But now, some methods are a bit confusing (at least in my opinion), such as the following, which is used to make a line:

//before the renaming of Point to Vector2, the parameters were startingPoint and endingPoint
Geometry.Line = function( startingVector, endingVector ) {
    //...
};

I should make a specific constructor for the Point object, or there are no problems in defining a point as a vector?

I know a vector have magnitude and direction, but I see so many people using a vector to just represent the position of an object.

share|improve this question
1  
As position is just a vector from (0,0{,0}), a vector is fine to use. –  andrew Nov 24 '12 at 20:25
add comment

7 Answers

up vote 8 down vote accepted

It's unlikely there would be any problems with conflating the definitions and treating points as vectors — but be a little careful, because some APIs have a 'Point' class that you might need to use (for representing, e.g., vertices of polygons) and if you define your own class you'll want to be able to port them back and forth.

What I would do, though, is treat them equivalently in your code; if you do use vector and point interchangeably, then there's no reason that your declaration for the Line() function should be talking about 'startingVector' and 'endingVector'. I would strongly encourage going back to

Geometry.Line = function( Vector startingPoint, Vector endingPoint ) {
    //...
};

Points are what those parameters represent, even if they're using class Vector to do so.

share|improve this answer
    
I would go a step further and have a typedef Vector Point somewhere in a header that defines points. That way it looks and feels like a Point, nobody is left scratching their head when using it why it's called Vector but on the internals side it's only one code base. –  tpg2114 Nov 24 '12 at 3:43
add comment

Just use the Vector object. Even if you feel this is/has been used incorrectly in the past, it's what people expect. Plus, it's not necessarily incorrect to use a vector as a point. And would difficult to do outside of discussion since both data structures require the same primitive types. The only differences will be the member functions, and it's easy enough to put those in the same class, since there's not much in the way of conflicting methods between the two.

Since you're learning, it's good to learn that doing what people expect is often the best way to do things (for trivial choices like the one being presented).

share|improve this answer
    
The member functions aren't the only difference between points and vectors; so is the w component, which is critical. –  ktodisco Nov 23 '12 at 19:20
add comment

To define a point, you need only one vector. To define a line, you need two. Typically, you have either two points that lie on the line or one vector representing a point that lies on the line and the other vector representing the direction of the line.

There are no problems defining a point as a vector, because a point IS a vector with direction equal to its coordinates and magnitude equal to its distance from 0,0.

So you don't need two separate classes to represent a Point and "Vector2d", although a Point could presumably be a subclass of Vector2d with different member functions directly related to your drawing or processing, while Vector2d might only do strict vector math functions like dot products.

share|improve this answer
add comment

Points are locations in space. Once you have a coordinate system, you can describe those points as a distance and a direction from the origin (a vector). So it is perfectly reasonable to use vectors to describe the start and end points of a line.

Just be aware that all points can be represented as a vector, but vectors are not points. Most vectors, e.g velocity, normal etc, don't have any sense of location.

Think of the wind, you can talk about it's direction and it's strength, but you can't talk about its position or location. This is how you should think of vectors when they are NOT being used to describe points in space.

share|improve this answer
add comment

Points and vectors can be thought of as the same thing. If it makes sense to you, you can think about vectors that represent position this way, and then it is logical to use a Vector2 class everywhere you would have otherwise used a Point class.

In mathematics, vectors are sometimes used to represent position. When used in this sense, the vector represents where some entity is located relative to an origin point. For example, suppose you are making a sh'mup and you want to keep track of where the player's ship is in the playing area. If you treat the bottom-left hand corner of the play area as (0, 0), then you could represent the player's location with a Vector:

   * Player (3,3)
  /
 /
. (0,0)

What the vector means in this case is that the ship is 3 units to the right of and 3 units above the origin. (Note that you can also use a vector for the player's speed, in which case the player would move 3 units right and 3 units up every second or frame. Both position and speed would be represented by the same vector class, but their vectors would be processed differently in the player's script.)

To use your line example, the position vector represents where the starting "point" and ending "point" are located relative to the origin. If your origin is the center of the playing area, you could determine a line like this:

             * (8, 2)
             |
     . (0,0) |
             |
             * (8, -2)

So one end of the line is 8 units to the right of and 2 units above the center of the playing field, and the other one is 8 units right and 2 units down.

Just to be clear, this isn't to say you have to use a Vector class instead of a Point class. This is just one way of thinking about this situation that can make it easier to decide how to implement those ideas.

share|improve this answer
    
The vector as an offset to another point in space still must be thought of as a point in relative coordinates. Calling it a vector is wrong, as a far as a game engine is concerned. –  ktodisco Nov 23 '12 at 22:59
add comment

Yes, it's ok to use a single Vector class to define both points and vectors, as long as you ensure that the w component of the vector is 0.

The key difference between a point and a vector is that a point represents a physical location in space, offset from the origin, whereas a vector represents a direction. A point can be translated, a vector can not. The w component of a 2- or 3-dimensional Vector class is what allows the translation component of a transformation matrix to take effect. If w is 1, then translation and rotation will be applied; if it is 0, then only rotation will be applied.

Not having the w component of a vector set 0 can come back to bite you; it leads to bugs that are rather difficult to track down. To be safe, you can make a Point class that inherits from the Vector class and explicitly sets w to 1, where the Vector class defaults w to 0.

share|improve this answer
2  
Many vector classes don't use the w component explicitly; quite a few treat it as an implicit element or have what amounts to a 'ProjectiveVector' class that implements it, but so much of an engine's code (everything outside rendering, essentially) uses the 'classical' 3-vector that carrying around a w component is entirely superfluous. This is an implementation detail, not an essential part of vectors or points. –  Steven Stadnicki Nov 23 '12 at 19:31
    
@StevenStadnicki That's true, but in order to have a class represent both a point and a vector, the w component must be present, otherwise there is no way to determine how the object should be used. –  ktodisco Nov 23 '12 at 19:47
1  
@ktodisco I think the way to determine how it should be used, is looking at what context it's being used. –  JCM Nov 23 '12 at 19:58
1  
Ran out of time to edit the comment :(. Was going to say that if the portions of the engine which use the class define the context in which it will be used, then it works. –  ktodisco Nov 23 '12 at 20:02
add comment

It depends on the implementation. If you have to have methods on the Vector2d instead of its prototype, then there's a cost for that, given the amount of points you'd need in a game, a major one. It's just how JavaScript works. In that situation it's better to create a Point2d class that doesn't have all the unnecessary functions.

share|improve this answer
    
The methods are being added to the prototype, so this is not a issue. –  JCM Nov 23 '12 at 19:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.