Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (306)
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 1412 times)
0 Members and 1 Guest are viewing this topic.
Offline zingbat

Senior Member




Java games rock!


« Posted 2004-12-14 21: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 07: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
« League of Dukes »

JGO Kernel


Medals: 197
Projects: 3


Eh? Who? What? ... Me?


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

Performance will suck. Memory usage will suck.

Cas Smiley

Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline zingbat

Senior Member




Java games rock!


« Reply #3 - Posted 2004-12-15 10: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 Member




Java games rock!


« Reply #4 - Posted 2005-01-20 00: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 Member




Come get some


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

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

Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #6 - Posted 2005-01-20 07: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
« League of Dukes »

JGO Kernel


Medals: 197
Projects: 3


Eh? Who? What? ... Me?


« Reply #7 - Posted 2005-01-20 10: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
HeroesGraveDev (42 views)
2013-06-15 23:35:23

Vermeer (51 views)
2013-06-14 20:08:06

davedes (48 views)
2013-06-14 16:03:55

alaslipknot (44 views)
2013-06-13 07:56:31

Roquen (61 views)
2013-06-12 04:12:32

alaslipknot (50 views)
2013-06-10 19:30:18

HeroesGraveDev (67 views)
2013-06-09 04:36:03

alaslipknot (54 views)
2013-06-09 03:40:19

CodeHead (54 views)
2013-06-09 02:55:41

GabrielBailey74 (66 views)
2013-06-09 00:02:25
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
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!