Slick Forums

Discuss the Slick 2D Library
It is currently Wed Jun 19, 2013 7:34 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How to use Vector2f ?
PostPosted: Sat Jul 14, 2012 12:50 pm 
Offline

Joined: Mon Jan 23, 2012 9:14 pm
Posts: 71
Location: France
Hi,

I've been using Slick for a little while now, I've managed to make some games and all and I was wondering how to use the Vector2f class. I've never really used it, but I've seen many people using it and talking about it and I was wondering how it could be used for my games.

This might sound like a stupid question, but I've never really understood how to use them. So, to understand it more I've looked at the source code but I think I still don't totally get it.

For instance, in most of my games, my Player class would have something like that as attributes to determine his position :
Code:
float x, y;
float vx, vy;

vx and vy being the velocity.

Now if I look the vector class I can see it acts like a point (it just has x and y attributes) as well as some methods, in particular add(Vector2f v).

So if I understand well, I'd be able to replace my previous attributes with something like :
Code:
Vector2f currentPosition;
Vector2f velocity;


So far, ok, it looks interesting.

However it provides many methods I don't understand. For example lengthSquared(), which returns
Code:
(x * x) + (y * y);

What is the use of this ? It could be used to see how far the object is from the origin but I don't know why I would want to do this

Another example, there is getTheta(), which returns something I don't understand.
Same for scale, if a Vector2f is something like a Point, I don't understand how it's possible to scale it since it's a like a dot ?

From what I've seen it's like a Vector that starts from 0, 0 to x, y, but I don't see why..

So I think I don't 100% understand how to use correctly this class.

Sorry if that question looks stupid but I'm curious. Am I wrong about the Vector2f class ? Is it just some sort of Point class with methods to "add" them or does it have another use ?

Thanks


Top
 Profile  
 
PostPosted: Sun Jul 15, 2012 8:01 am 
Offline

Joined: Sat Jun 02, 2012 7:41 am
Posts: 68
Well, Vector2f is what the name says... A vector! :D A vector is basically an arrow, that starts from the origin (0, 0) and ends in a dot specified by its coordinates.

However, sometimes it is better to think that the arrow starts in another point: for example, if you have a velocity, it is more clear if you think that the vector starts exactly at the coordinates of the object. And nothing changes, because the length and the angle of the vector are the same. This way, the velocity represents how much the x an y value of the "position" will be increased.

If you represent it with an arrow (remember that (0, 0) is the top left corner), the angle of the vector is simply the angle between the arrow and the x axis (well, I'm not completely sure because I don't remember where 0 degrees is, but you can check it by creating a vector and getting the angle (with getTheta): if you know where the arrow points (for example, you can put in the coordinates (1, 0) which should be an horizontal, right-headed vector) it is simple to understand where the angle is calculated from).

The vector also has a module, which is the length of the arrow. let's say you want to calcultate the distance beetween two objects of your game. Both have a "Vector2f position" attribute. The "distance" is a new vector with the coordinates: (x1 - x2, y1 - y2). Now if you use the lengthSquared() on the "distance" vector, it will return you basically how far the objects are (well, the distance squared, the actual distance is Math.sqrt(lengthSquared()), that is the length of the "distance" arrow.

Hope now it is more clear :)


Top
 Profile  
 
PostPosted: Mon Jul 16, 2012 10:44 am 
Offline

Joined: Mon Jan 23, 2012 9:14 pm
Posts: 71
Location: France
Ah ok, it's more clear now, thanks :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group