Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Vector3f or three floats variables  (Read 728 times)
0 Members and 1 Guest are viewing this topic.
Offline Yemto

Junior Devvie


Exp: 3 years



« Posted 2014-02-17 05:03:49 »

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

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


and

1  
2  
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.
Offline Danny02
« Reply #1 - Posted 2014-02-17 05:07:55 »

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.
Offline nerb
« Reply #2 - Posted 2014-02-17 05:43:43 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Yemto

Junior Devvie


Exp: 3 years



« Reply #3 - Posted 2014-02-17 05:59:08 »

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.
Offline The Lion King
« Reply #4 - Posted 2014-02-17 06:06:20 »

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]

"You have to want it more than you want to breath, then you will be successful"
Offline Yemto

Junior Devvie


Exp: 3 years



« Reply #5 - Posted 2014-02-17 06:27:12 »

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.
Offline The Lion King
« Reply #6 - Posted 2014-02-17 06:46:30 »

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.

"You have to want it more than you want to breath, then you will be successful"
Offline Gibbo3771
« Reply #7 - Posted 2014-02-17 17:18:13 »

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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #8 - Posted 2014-02-18 20:26:55 »

Having the components in single variables has no advantage...
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...

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (43 views)
2014-11-26 06:22:04

toopeicgaming1999 (35 views)
2014-11-26 06:20:36

toopeicgaming1999 (8 views)
2014-11-26 06:20:08

SHC (24 views)
2014-11-25 03:00:59

SHC (24 views)
2014-11-25 02:53:45

Norakomi (25 views)
2014-11-25 02:26:43

Gibbo3771 (23 views)
2014-11-24 10:59:16

trollwarrior1 (36 views)
2014-11-22 03:13:56

xFryIx (75 views)
2014-11-13 03:34:49

digdugdiggy (52 views)
2014-11-12 12:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 13:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 13:36:02

List of Learning Resources
by Longor1996
2014-08-16 01:40:00

List of Learning Resources
by SilverTiger
2014-08-05 10:33:27

Resources for WIP games
by CogWheelz
2014-08-01 07:20:17

Resources for WIP games
by CogWheelz
2014-08-01 07:19:50

List of Learning Resources
by SilverTiger
2014-07-31 07:29:50

List of Learning Resources
by SilverTiger
2014-07-31 07:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!