# 2D vector tutorials

This topic is 3174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have been searching for quite sometime and would like tutorials for anything I've missed. I have a basic vector struct created. It was copied from RPG Toolkit's sourceforge page(edited a little). The toolkit is basically a story passed form person to person. The programming has been warped. It was also programmed using MSV 6. It would take a great deal of reprogramming to compile in any other compiler. I've heard rumors of it acting in strange ways and being less strict as to what you can do. This being said it was almost impossible to figure out what I actually needed to use these vectors. I am starting from the beginning and would like tutorials from creating the struct to the varies used of the vectors themself. I am a fan of formulas and algorithms even though my understanding of them is well... not there. Thanks http://justmathtutoring.com/ http://en.wikipedia.org/wiki/Euclidean_vector http://www.sacredsoftware.net/tutorials/Vectors/Vectors.xhtml Edit Added links [Edited by - rpg gamer 17 on December 4, 2009 2:20:48 PM]

##### Share on other sites
Sounds like you've found a few good sources for vectors. What exactly are you looking for? If you could be more clear, we could help better.

If you just want to understand vectors and how to use them, try making a sim of a charged particle moving in a magnetic field (wiki it). Also, try making a program to calculate the power output of a solar panel (given light vector and panel normal vector). That should cover most of the subtleties of vectors.

As far as structs for vectors go, if you're having trouble with a particular implementation, post it. Otherwise, the struct should just have components x y and z, which correspond to the usual vector components. The struct should have accompanying length(u), dot(u, v), cross(u,v) and vector(x, y, z) functions:
vector is just a constructor
dot(u, v) = u.x*v.x + u.y*v.y + u.z * v.z
length(u) = sqrt(dot(u, u))
cross(u, v) = vector(u.y*v.z - u.z*v.y, u.z*v.x - u.x*v.z, u.x*v.y - u.y*v.x)

I hope this helps, as i said, I don't know what you're looking for.

##### Share on other sites
Those are good ways to learn vectors. The way i learned them is creating my own vector class for actionscript. So that i had to actually think about how they worked.

here is another link, i hope its usefull to you: http://www.euclideanspace.com/maths/geometry/space/vector/index.htm

##### Share on other sites

I'm designing the below for a 2D game. My vectors will have 3 components but the third I do not plan to used.
I am planning on using bounding boxes and vectors for my collision system. The main problem I am having is deciding how to constuct my vector holder. This will hold all vectors that are within the bounding box.

In order to do that I need to know which type of vector I should use. I was thinking of using free vectors(vectors who's inital position is not needed). I am guessing an offset value stating the actual posistion of the point would be needed. This seems redundant.

Thanks

EDIT:
REEDIT:
I'm looking around for line collision tutorials.

EDIT:
Alright I have box vs box collision implemented.

There is a problem with it though:

Red Box = person
Green Outline = wall

Here is how I calculate the size of the bounding box:
void BoundingBox::Update(void){	std::vector<Vector> vVector = m_shape.VECTOR(); //returns vector list from shape	unsigned int type = m_shape.TYPE();	switch(type)	{	case SH_LINE:		//unsure some may be slanted		break;	case SH_CIRCLE:		//x is the center point vVector[0] is the radius		left = ((float)x) - CVector::Magnitude(vVector[0]);		right = ((float)x) + CVector::Magnitude(vVector[0]);		top = ((float)y) - CVector::Magnitude(vVector[0]);		bottom = ((float)y) + CVector::Magnitude(vVector[0]);		break;	case SH_POLYGON:		left = (float)x;		top = (float)y;		for(std::vector<Vector>::const_iterator ci = vVector.begin(); ci != vVector.end(); ci++)		{			//find the farthest right			if(x + (*ci).x > right)				right = x + (*ci).x;			//find the farthest up			if(y + (*ci).y > bottom)				bottom = y + (*ci).y;		}		break;	}}

here is how I check to see if there is a collision. This part will need to be extended to see if the object does cross the vector it contains but I'm unsure how to do that.
bool PB_Collision::BoxWithBox(BoundingBox box){	unsigned int i = 0;	for(unsigned int i = 0; i < vBox.size(); i++)	{		bool Possible = true;		if(Possible)			if(box.bottom <= vBox.top)				Possible = false;		if(Possible)			if(box.top >= vBox.bottom)				Possible = false;		if(Possible)			if(box.right <= vBox.left)				Possible = false;		if(Possible)			if(box.left >= vBox.right)				Possible = false;		if(Possible)			return true;	}	return false;}

I was manipulating LazyFoo's collision detection.
Under the last if(Possible) would be a CheckVector(BoundingBox) function.

[Edited by - rpg gamer 17 on December 7, 2009 7:52:03 PM]

##### Share on other sites
Hey,
it looks like you might be confusing vectors with line segments. A line segment is constructed from two vectors (begin, end).

There are no "types" of vectors, unless you want to use vectors of floats vs vectors of ints.

What do you mean by a point lying on a vector? Or past a vector? a point is a vector, so you can just test them for equality, though I assume that is not what you want; please clarify.

For AABBs, you need 2 vectors (low corner, high corner). The pairs ((lowx, highx), (lowy, highy) ...) define intervals; if you want to test if a point is in a box, you just test if it is within those 3 intervals. To test two AABBs against each other, you have to test the three pairs of intervals, they all have to collide for there to be overlap.

Also looks like you want to test line segments against each other. To test line segments, you have to test if the two points that define line 'A' are on opposite side of line 'B', and visa versa. You'll have to look up the opposite test, I can't pull it off the top of my head. It has to do with testing the clockwise/counterclockwise of two triples of points defined by the two segments. Keep in mind that line segment intersection is meaningless in 3D.

Again I'm not sure what you are asking, so i don't know if this helps. If you can state questions more clearly, I can give better help.

##### Share on other sites
I edited the previous post. I guess while you were posting. Sorry about that.

I'm still using vectors(I think). I'm just moving the position of the bounding box:
class Shape{private:	unsigned int m_type;	std::vector<Vector> vVector; //list of vectorspublic:	Shape(void);	Shape(unsigned int type);	~Shape(void);	void push_back(Vector v); //ways to add vectors to the list	void push_back(float x, float y, float z = 0);	inline std::vector<Vector> VECTOR()	{		return vVector;	}	inline unsigned int TYPE()	{		return m_type;	}};class BoundingBox{public:	void Update(void); //update bounding box right, left, top, bottom coordinates	Shape m_shape;	BoundingBox(void);	BoundingBox(int _x, int _y, Shape shape);	BoundingBox(Vector pos, Shape shape);	~BoundingBox(void);	int x;	int y;	float right;	float left;	float top;	float bottom;};

I'm using the thought behind the RPG Toolkit's collision system as what I know about vectors. The vectors used in the collision system may be a bunch of line segments grouped together. The toolkit had a point function(in it's source code) that was used to see if the sprite bounding box was on the vectors contained inside of there bounding box. I'll see if I can find the function again and decipher it.

If I need to convert this to use line segments I will do so.

I basically want to check:
If a point(vector) is between the left and right of the box.
->If a point(vector) is between the top and bottom of the box.
->->If so a collision has been met. The vector is inside the box.

EDIT:
I found the function:
Line number 294-345
Line number 566-645 is used to determine intersection
Line number 350-378 PointOnLine Function
The bottom one contains several other functions most are located a little above 566.
RPG Toolkit Sourceforge
Click on CVector.cpp->Revision 1.30 view

[Edited by - rpg gamer 17 on December 7, 2009 6:31:29 PM]

##### Share on other sites
I had a similair problem today. What you could do is determine the distance between 2 vectors(object, collisionObject) and then do the appropriate action of the distance is between a certain value. I used this in unity to create a primitive navigation system between nodes.

I bought the 3D Math Primer for Graphics and Games Development(Catchy title) after reading the chapter about vectors online, dont remember the site, but its a great way to get to understand all that's needed. Could have been Amazon or Google books, it appears on both of those sites and i think its worth checking. If you have any specific questions feel free to ask.

What you could do is see if the vector distance between the 2 is equal to half the width of the box you need to collide with. So that you know when its touching.

##### Share on other sites
I am about to do an epic restructuring of my current code.
Please disregard any posted code as they have or will be tweaked or restructured.

@The Almighty Snark
Thanks. I'm looking for the book. If I happen to find the site that you might be talking about I'll post it.

EDIT:
I think I found the site:
http://www.scribd.com/doc/11997032/3D-Math-Primer-for-Graphics-and-Game-Development

^^

EDIT:
I have a stupid question...
How do you find the top right corner of a box... if you have a circle inside it and you only know the radius?

Hold on... I'll draw a picture...
EDIT:
Never Mind... I figured it out...
>.<
The diameter is the width and height of the circle.
EDIT:
Restructoring completed.
Now I'm looking at the book.

[Edited by - rpg gamer 17 on December 9, 2009 9:11:27 AM]

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 10
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632148
• Total Posts
3004450

×