Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (601)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
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  
  Do we really need templates to work with primitives?  (Read 1559 times)
0 Members and 1 Guest are viewing this topic.
Offline zingbat

Senior Devvie

Medals: 1

Java games rock!

« Posted 2007-03-05 21:53:56 »

I was thinking about this for an api about Vector i restarted to work on recently. Do we really need templates that would support primitive data types?

It would be nice if we could do this.

public class Tuple<primitive T> {
T x, y, z, w;

But theres an alternative that may work just as well.

We can enrich the tuple class with type coercion methods:

public interface Tuple
int length();
void setLength(int i);
int getInt(int i);
int getDouble(int i);
void setInt(int i, int val);
void setDouble(int i, double val);
void parse(String s)
void parse(String s, int dim)
...etc for other primitive types

Internally the vector may be implemented in any way. This interface would work no matter what it's implementation was.

Any ideas, sugestions?
Offline CaptainJester

JGO Knight

Medals: 12
Projects: 2
Exp: 14 years

Make it work; make it better.

« Reply #1 - Posted 2007-03-06 02:58:09 »


Offline Riven
« League of Dukes »

« JGO Overlord »

Medals: 1019
Projects: 4
Exp: 16 years

Hand over your head.

« Reply #2 - Posted 2007-03-06 10:28:26 »


Those methods are not exactly performant.

I usually work around issues like this by writing a little code-generator that spits out the structure I want for all primitives.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight

Medals: 12
Projects: 2
Exp: 14 years

Make it work; make it better.

« Reply #3 - Posted 2007-03-06 12:35:25 »

Then use the appropriate buffer type.


Those would be fast enough.

Offline zingbat

Senior Devvie

Medals: 1

Java games rock!

« Reply #4 - Posted 2007-03-06 16:57:47 »

I usually work around issues like this by writing a little code-generator that spits out the structure I want for all primitives.

I did a python script once that would process Java source decorated with special tags to simulate C++ templates and that worked best for performance. The problem however is that it's hard to maintain code this way and integrate it with Eclipse.

I thought about having a hierarchy of Tuples and classes that extend it for all vectors no mater their dimension or representation. So we would have a Tuple interface then other interfaces that extend it like Vector, Point, Quaternion, but unlike javax.vecmath where a Tuple2f is the base class of Vector2f but not a Vector3f or Vector4d in my idea a Tuple would be a base class for every Vector class no what their representation or dimension is.

interface Tuple
int getSize();
Tuple setSize(int sz);

Class getDataType(); // the data type used to implement the tupe, ex. Integer.TYPE

<type> get<type>(int i);
<type> get<type>X(); // faster
<type> get<type>Y();
<type> get<type>Z();
<type> get<type>W();

Tuple set(int i, <type> val);
Tuple setX(<type> val); // faster
Tuple setY(<type> val);
Tuple setZ(<type> val);
Tuple setW(<type> val);

boolean equals(Tuple u2);
boolean equals(Tuple u2, float delta); // ignored by integer representations

long hashCode();
String toString();

Implementing the add operation.

Vector v1 = new FloatVector4(); // FloatVector4 implements Vector interface
Vector v2 = new FloatBufferVector(4); // FloatBufferVector implements Vector interface also
import static LinearAlgebra.*;
Vector v = add(v1,v2);
Point p1 = addVector(v1,v2); // wrong: can't cast vector to point
Point p1 = new FloatPoint4();
Point p = addPoint(p1,v);
Point p = addPoint(v1,v2); // wrong: can only add a vector to a point (not two vectors) to obtain a point

How would the add function be implemented in so that using multiple vector dimensions and representations would not conflict?

My idea is to use type conversions before or after doing calculations:

public class FloatVector4 implements Vector {
float x, y, w, z;
public double getDoubleX() { return x; }
public double getDoubleY() { return y; }
public double getDoubleZ() { return z; }
public double getDoubleW() { return w; }

public class LinearAlgebra { // only deals with interfaces
public Vector addVector(Vector v1, Vector v2) {
// since we don't know at this point how v2 is represented we simply use doubles and let their values
// be casted when using get/set methods with any lost of data being ignored
double x1 = v1.getDoubleX();
double y1 = v1.getDoubleY();
double z1 = v1.getDoubleZ();
double w1 = v1.getDoubleW();

double x2 = v2.getDoubleX();
double y2 = v2.getDoubleY();
double z2 = v2.getDoubleZ();
double w2 = v2.getDoubleW();


return this;

The only performance issue is the type casts, but since all Math functions work with double arguments how much would this be a problem?
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Jesse (12 views)
2015-07-29 04:35:27

Riven (35 views)
2015-07-27 16:38:00

Riven (18 views)
2015-07-27 15:35:20

Riven (20 views)
2015-07-27 12:26:13

Riven (11 views)
2015-07-27 12:23:39

BurntPizza (30 views)
2015-07-25 00:14:37

BurntPizza (41 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (27 views)
2015-07-22 22:16:53

NoxInc (18 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!