Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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 class style  (Read 1718 times)
0 Members and 1 Guest are viewing this topic.
Offline zingbat

Senior Devvie




Java games rock!


« Posted 2004-12-14 20:01:44 »

With the inclusion of generics it would be more obvious and much more clear to have a Vector class defined with generics like:

Vector<n><t>

with n being the dimension and t the primitive or non-primitive type.

Thus we could avoid having a package with different classes that do almost exactly the same (Vector2f, Vector2d, Vector3f, Vector3d, ...) poluting design.

The problem is that Java generics are only 1/2 generics so no primitive int types and no values passed has template parameters.

To try to solve this problem i come up with a scheme. A Vector class is defined like:

Vector(int dimension, Class clazz)

where clazz is a value like Float.TYPE that represents a primitive type.

The constructor body would be something like:

// A an array object representing a tuple
// Notice i don't use the [] notation
Object tuple;

Vector(int dimension, Class clazz)  {
    // validate args
    ...
    // create tuple
    tuple = Array.newInstance(clazz, dimension);
}


Methods accessing the data structure use
reflection. For example:

void add(Vector v2) {
  // check if this this is compatible with v2
   Vector v1 = this;
   int length = Array.getLength(v1.tuple);
   switch (tuple.class.getComponentType()) {
      case Float.TYPE:
           for (int i=0; i < length; i++) {
                float f1 = Array.getFloat(v1.tuple, i);
                float f2 = Array.getFloat(v2.tuple, i);
                Array.setFloat(v1.tuple, i, f1+f2);
           }
           break;    
       case ... :
             ....
   }

}



A more simple solution woud be to have:

FloatPoint(int dimension)
DoublePoint(int dimension)

this would avoid reflection but would still have to work with an array:

void add(DoubleVector v2) {
    // check lengths
   int length = tuple.length;
   for (int i=0; i < length; i++)
        this.tuple += v2.tuple;
}


Of course that this would be the most efficient but at a great cost in terms of a clean design as you can see from the vecmath package clutered with similar classes:

void add(Vector2d v2) {
    this.x += v2.x;
     this.y += v2.y;
}


My question is how of a performance difference would we get from using reflection methods and from using the array method when compared to vecmath way ? Im asking this in case anyone has done a benchmark since im not very good at doing them.
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #1 - Posted 2004-12-15 06:23:18 »

Up to and including 1.4, reflection is very slow.

And...it's many times faster than it was a few versions previously!

Even trivial stuff that - to any normal human being - would seem to take only as much time as a few tens of method calls (i.e. nanoseconds), takes (close to) milliseconds (IIRC).

e.g. if you discover a method by reflection, and cache it, and want to invoke it, *each invocation* takes masses of time, even though yuo're not having to do *any* discovery.

Java 5 was meant to make some major improvements in this area. Since I have no intention of using 5 for as long as I can possibly avoid doing so, I'm afraid I'll have to leave that to someone else Sad

malloc will be first against the wall when the revolution comes...
Offline princec

« JGO Spiffy Duke »


Medals: 422
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2004-12-15 07:47:45 »

Performance will suck. Memory usage will suck.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zingbat

Senior Devvie




Java games rock!


« Reply #3 - Posted 2004-12-15 09:59:33 »

Thanks for the feedback. Reflection is out of the question to me then. I hope that using arrays doesn't become too much of a speed cap.
Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #4 - Posted 2005-01-19 23:06:58 »

Quote
Thanks for the feedback. Reflection is out of the question to me then. I hope that using arrays doesn't become too much of a speed cap.


Server VM argument passed to the hotspot compiler will ensure bounds checking is turned off with arrays which will give you a performance boost.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline phazer

Junior Devvie




Come get some


« Reply #5 - Posted 2005-01-20 04:31:53 »

No, array bounds checking is never turned off. It might be optimized in some cases.

Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #6 - Posted 2005-01-20 06:50:21 »

Quote
No, array bounds checking is never turned off. It might be optimized in some cases.



Nuts. It's just that I've heard otherwise.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline princec

« JGO Spiffy Duke »


Medals: 422
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2005-01-20 09:37:54 »

Well, it can be optimised away, in a few rare circumstances which don't seem to occur too often Sad

Cas Smiley

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.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (69 views)
2014-11-03 22:04:50

BurntPizza (67 views)
2014-11-03 18:54:52

moogie (82 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

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

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
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!