Vector3f or three floats variables

I have just began with 3D in java, using LWJGL. I have so far manage to create a camera, and some objects. But after looking around on the internet I noticed that some people uses codes like this

private Vector3f location = new Vector3f(0, 0, 0);
private Vector3f rotation = new Vector3f(0, 0, 0);

and


private float x, y, z;
private float rx, ry, tz;

Is it just a preference thing? whats the advantages, and disadvantages? I personally like the Vector3f method, since then I can use a method that returns the vector and use the variables x y and z instead of using get and set methods. It also allows me to use .set for things like projectiles, and to put the camera where I want it to be.

Use Vector objects!
Having the components in single variables has no advantage and is only done by beginners which are not familiar with existing math libs.

Vector classes can be easier to handle, and more to the point you can then cleanly implement your own vector math utilities. Matrix classes are also worth a thought for the same reasons. Roll your own vector &/or matrix classes once, and you will get continual re-use of it in your projects.

Thanks, and since my question have been answered I’m going to go a bit off topic.

I haven’t gotten my head around matrixes yet. So far I just see them as a space in a 2D or 3D environment telling objects inside that environment to render inside the matrixes buffer. I use gluPerspective for my cameras, and glOrtho for the hud. I have used glGetFloat on them so I can use glLoadMatrix to switch between them when I want.

First i’ll just quickly mention that some of the functions you are using are sorta outdated, your just learning tho, so its not a big deal.

Anyway, Matrices are simply a mathematical tool to move points in a certain way. You can think of it this way. You can store rotations and/or translations inside of Matrices. Now Im not sure if you use shaders, but if you do this will be more obvious if you don’t ill explain anyway.

A point is multiplied by (usually) multiple matrices in openGL to transform the point. For example a couple of matrix multiplications can turn a random point in 3D/2D space, to a point relative to your camera, to a point taking into account perspective (like changing the point to how it would look like out of a camera lens), and eventually to a point relative to your screen.

[edit: fixed typos]

Thanks, I haven’t looked into shaders yet, so far I have trying to get the cameras working, and objects rotating the way I want.

I have seen camera objects in a tutorial ( http://www.java-gaming.org/index.php?topic=30871.0 ) that uses two Matrix4f objects named “projection”, and “view”. I would have followed that tutorial if It didn’t had used the m variables inside the Matrix4f object, m00, m11, m22, m23, m32, m33 makes no sense to me, and I felt like just copy and paste the code wouldn’t do me any good.

EDIT: Anyway, I think I’ll read up on matrix later, and uses Vector3f for my camera/objects while i learning matrixes.

A Matrix4f, is a 4x4 matrix of float values, imagine a two dimensional array, that’s pretty much what it is.

m00 is the top left corner, m10 is the next row over, m01 is the next column down. The view matrix describes where the camera is in space and its rotation. You can mix these two into one matrix, I usually do 2 separate matrices, a rotation one and a translation one (because I use quaternions which is outside the scope of this).

The projection matrix is a little harder to explain its like a lens it turns things from 3D points to how your eye would see it. The set up is also explained. these matricies usually are put together in a camera class.

http://lwjgl.org/wiki/index.php?title=The_Quad_with_Projection,_View_and_Model_matrices might have more explanation.

Use Vectors, you might not see a lot of advantages at the moment but as you program more and more, Vectors make it easier to apply mathematical calculations.

One to note is getting the angle from 1 vector to another using cosine/sine, makes it pretty much a 2 line algorithm.

Not true actually. Let’s say you have a Player object which needs a position.

player --> x, y, z
player --> positionVector --> x, y, z

The positionVector is a completely different object, and can therefore end up anywhere in the Java heap, meaning that this adds an extra cache miss which cannot be prefetched when accessing the position. This is the same reason why linked lists are slower to loop over than simple arrays. When looping over an array, the processor sees your array of references and can preload a large number of upcoming objects into the CPU cache. When you have a linked list, the objects have to be loaded one by one since you only ever know where the current and next object are located in memory. Similarly, it’s impossible for the CPU to know where the positionVector is located without first waiting for the player object to be loaded into memory.

With that said, you should definitely still use vector objects for convenience since the performance difference is ridiculously small anyway. I just wanted to point out that there is an actual difference…