Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Design Choice Explanation - Point vs ints  (Read 768 times)
0 Members and 1 Guest are viewing this topic.
Offline mentalfloss

Junior Newbie


Medals: 1



« Posted 2013-09-03 13:59:33 »

Can you guys please explain why you want to use int x,y type mechanics instead of the Point class for your coordinates? I see this code everywhere on here and I just cannot figure out why you don't prefer using the Point class.

Maybe I'm missing something here. I am very new to java and not very experienced with games in general. I just figured that since the class is there, why not use it?

Please help me understand this design choice that is so prevalent in java game programming.
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #1 - Posted 2013-09-03 14:07:55 »

If you have a lot of objects, its a lot of overhead, with little to no benefit.
But most of the time people are using floats (much better looking movement).
Offline Troncoso

JGO Coder


Medals: 20



« Reply #2 - Posted 2013-09-03 14:13:14 »

I'll definitely use a Point class if I have a small set of points that I want to continuously manipulate (as opposed to constantly creating new ones). Honestly though, only for the convenience of writing something like p.x as apposed to p[0].
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StrideColossus
« Reply #3 - Posted 2013-09-03 14:23:11 »

It's horses for courses really.

If you're passing around your point data for manipulation by (say) a set of particle system influences then you'd be nuts not to encapsulate the data in a class.

On the other hand, if you're doing a lot of complex, maths-based fiddling with the data then it's probably easier to deal with the 'raw' X-Y values.

@RobinB - Not sure how encapsulating the data and functionality into a class would be deemed 'little or no benefit'?  Unless your game is very trivial how would you manage that data without getting into a whole world of pain?  Using the particle system mentioned above as an example, how else would you represent the particle locations, velocity, colour, etc?  Yes there would be overhead (bytes per object in addition to the X-Y) but hardly the end of the world (unless maybe specifically aiming for low-end mobiles).  I calculated this for a demo I did the other day with ~100K particles, turned out to be less than the texture I was applying to the particles!
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #4 - Posted 2013-09-03 15:14:22 »

Hmmm I'm reading this differently then you folks.

I think he's asking why not use java.awt.Point instead of defining your own class OR just using your own x and y.

It's a matter of preference, I like creating my own Point class (call it a Vector) because I can add my own methods to it... not to mention I like using floats and java.awt.Point uses ints.

You could use java.awt.geom.Point2D if you want floats or doubles instead of ints.

Offline Troncoso

JGO Coder


Medals: 20



« Reply #5 - Posted 2013-09-03 16:11:18 »

Hmmm I'm reading this differently then you folks.

I think he's asking why not use java.awt.Point instead of defining your own class OR just using your own x and y.

It's a matter of preference, I like creating my own Point class (call it a Vector) because I can add my own methods to it... not to mention I like using floats and java.awt.Point uses ints.

You could use java.awt.geom.Point2D if you want floats or doubles instead of ints.


Can you guys please explain why you want to use int x,y type mechanics instead of the Point class for your coordinates?

That's what makes me think that he's not talking about what you are talking about. Though, if we are on that subject, I would also rather write my own and add some vector manipulation methods in it.
Offline mentalfloss

Junior Newbie


Medals: 1



« Reply #6 - Posted 2013-09-03 17:43:17 »

Quote
I think he's asking why not use java.awt.Point instead of defining your own class OR just using your own x and y.

Yes, that's what I'm asking. I am just not sure what best practices are. I figured you might want to use things like setLocation and distance methods or something for movements and such.

Quote
If you have a lot of objects, its a lot of overhead, with little to no benefit.
But most of the time people are using floats (much better looking movement).

Fine, my questions remains for Point2D as well.

Wouldn't it make sense to just have a base class such as Mobile that had a Point member and then everything that had a location would extend from it to have that location data as well in a well encapsulated object?

Again, I just wanted to know what best practices were. It may be popular to use x,y as separately tracked values but is it right?
Offline Mac70
« Reply #7 - Posted 2013-09-03 18:18:45 »

Using x,y as separate values would make sense only, if you must really care about performance/RAM usage, but I don't think this should be a big problem.

Code organization > premature optimalization

About using "raw" xy values in code examples - this just allows you in some cases to write it faster and more understandable for another coders. Wink

Check out my Devblog! Smiley
Offline Jeremy
« Reply #8 - Posted 2013-09-03 19:32:38 »

Vectors are one of those classes in your game that are used everywhere, you don't want them to be doing more work than they have to. They must be us-able for performance critical code as well. So people tend to write their own because:

- They're very simple to write.
- It gives them complete control over the code - limiting it to only what it needs to do. For example, a Point can be transformed by an AffineTransform, which is a 3x3 matrix, some people only need to worry about 2X2 matrixes (and there are various tricks to finding the inverse of a 2x2 matrix that aren't do-able with a 3x3 matrix) Other simplifications are making the x\y components publicly accessible since get\set accessors for such a simple object is really not required.

Finally, my experience with java.awt.geom is that there are a lot of performance issues around that package and I've already run in to a couple bugs there. I don't want to have to touch them.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #9 - Posted 2013-09-03 21:06:16 »

I do not know any compelling reasons to use an additional class in many situations. In certain situations where it would be marginally easier to use classes it is also marginally more efficient or much more efficient to not do so, so if you know what you're doing or have to worry about scaling, you just start with separate components. In favor of separate variables:

1. Java's big missing feature is pass by value record types. There is a performance penalty for using wrapper classes indiscriminately.

2. What's the purpose of using those classes? Maybe there is the impulse for beginner computer science students to nest classes as deeply as possible, but I like to represent the state of an object one level deep if the relationship is one to one. I would only nest objects if the state of the inner object were disconnected from the outer object or if it were a complicated class to implement. I don't think of an entity as having a 3D position child object, 3D velocity child object, and health child object. I think of it as a seven dimensional object because it has seven orthogonal state variables.

A class like this has and needs all these components so it seems superfluous to segment it into smaller dimensions, especially if you are going to look at xVelocity and xPosition (and maybe xAcceleration) as often as you look at xPosition and yPosition (and zPosition.)

If you thought about movement in terms of position, velocity, and acceleration instead of instantaneous position in discrete time (which depends on how one learned physics) you might be asking the question why don't people use the ValueFirstDerivativeSecondDerivative class (Or a Vector3F containing these values.)

3. If you know the math, know how to touch type, and have a modern IDE, then it doesn't involve more work to treat variables separately. If you know what you are doing, you use the exact minimum number of operations and reuse calculations - rather than scratch your head over how to mix built in vector operations like Lego blocks - It is easier and more efficient to use ints or floats or doubles.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CommanderKeith
« Reply #10 - Posted 2013-09-03 23:09:08 »

Hi,
I came across this issue when i was making my path-finding / lights project https://code.google.com/p/straightedge/.
java.awt.Path uses arrays or lists of floats instead of points and I didn't like that so I made my own point class, and each polygon had an ArrayList of Points. I found this easier to deal with and like Mac70 said, the extra RAM needed is minimal.
The only reason I think that it might be advantageous to use lists of raw x and y values might be when dealing with opengl since I think you can just feed the list of x and y floats straight to the graphics card without having to unmarshal them out of the point objects. But I might be completely talking thru my hat, I didn't use opengl in my project and I barely have any experience with openGL.
But I agree that it's an interesting design issue.

Offline relminator
« Reply #11 - Posted 2013-09-04 01:54:48 »

You could pass vectors directly to opengl.  They are sequential in memory anyways.
Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #12 - Posted 2013-09-04 03:00:44 »

He did not say minimal. Objects have several bytes of overhead. (Of course people usually have enough RAM so that you don't need to worry. It's not a waste, so use more RAM if it's worth it.) You do need to worry about cache performance and dereferencing overhead. You can also use your GC budget for something more helpful like nodes in tree, list, or other graph structures.

The performance of copying floats from a single Object to a Buffer is the same as copying three floats from an array to a Buffer. OpenGL receives them in a sequential buffer anyway. The problem comes from whether the source is sequential or not. Iterating over an array of primitives is an an order of magnitude faster than an array of Objects for large data sets. However, that is only relevant to arrays. The difference is smaller for one object versus two, neglecting memory management problems, but they're still just vectors. It's not like you have to implement a HashMap.
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.

Dwinin (23 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (25 views)
2014-09-07 01:12:14

Longarmx (31 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

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

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

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

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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!