Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
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  
  Vector2D class. float or double? [noob-alert]  (Read 830 times)
0 Members and 1 Guest are viewing this topic.
Offline relminator
« Posted 2013-02-22 11:10:31 »

I'm kinda new to Java so bear with the ignorance as I'm still in the "experimental" stage (currently experimenting with eclipse and how inheritance in Java works).

I generally use floats(or Fixed-point) for all my gaming data but noticed that almost all Math.* constructs return doubles.   I know that on 32-bit machines, floats are generally faster than doubles (I have a 32bit machine) but I don't know if this is true in Java being emulated (or even on jit).  I also need a lot of float cast to make things work.  Does casting have a speed penalty in Java since it's emulated?

Anyway feel free to make suggestions regarding my code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
/**
 *
 */

package net.phatcode.rel;

/**
 * @author Anya
 *
 */


//import java.lang.Math.*;
//import java.util.Random;


public class Vector2D
{

   public static final float EPSILON = 0.0000000001f;

   public float x;
   public float y;
   
   public Vector2D()
   {
      x = 0;
      y = 0;
   }
   
   public Vector2D( float a, float b )
   {
      x = a;
      y = b;
   }
   
   public Vector2D( Vector2D v )
   {
      x = v.x;
      y = v.y;
   }
   
   
   public float magnitude()
   {
      return (float)Math.sqrt( (x * x) + (y * y) );
   }
   
   public float magnitudeSquared()
   {
      return ( (x * x) + (y * y) );
   }
   
   public float dot( Vector2D v )
   {
      return ( (x * v.x) + (y * v.y) );
   }
   
   public Vector2D normal( boolean side )
   {
      return ( side == false ) ? new Vector2D( -y, x ) : new Vector2D( y, -x );
   }
   
   public float normalize()
   {
      float length = magnitude();
      float invMag = 1.0f/length;
      if( length <= EPSILON ) return 0.0f;
      x *= invMag;
      y *= invMag;
      return length;
   }
   
   public void scale( float s )
   {
      x *= s;
      y *= s;
   }
   
   public float angleBetween( Vector2D v )
   {
      return (float)(Math.atan2(v.y - y, v.x - x));
   }
   
   public Vector2D reflect( Vector2D n )
   {
      // n -> normal of the vector *this would reflect from
     // Assumes normal(n) is normalized
     // -2 * (V dot N) * N + V
     // Or
     // -2 * (V dot N)/|N| *N + V
     
      float dot = (x * n.x) + (y * n.y);
      Vector2D n2 = new Vector2D( n.x * -2.0f, n.y * -2.0f );
      n2.scale(dot);
      n2.x += x;
      n2.y += y;
     
      return n2;
   }
   
   public void displayComponents()
   {
      System.out.println( "v.x = " + x + " :: v.y = " + y );
   }
   
   public String toString()
   {
      return ( "v.x = " + x + " :: v.y = " + y );
   }
   
   
   // Static
 
   public static float magnitude( Vector2D v )
   {
      return v.magnitude();
   }
   
   public static float magnitudeSquared( Vector2D v )
   {
      return v.magnitudeSquared();
   }
   
   public static float normalize( Vector2D v )
   {
      float length = v.magnitude();
      float invMag = 1.0f/length;
      if( length <= EPSILON ) return 0.0f;
      v.x *= invMag;
      v.y *= invMag;
      return length;
   }
   
   public static float dot( Vector2D a, Vector2D b )
   {
      return ( (a.x * b.x) + (a.y * b.y) );
   }
   
   public static Vector2D Projection( Vector2D u, Vector2D v )
   {
      float dotUV = dot(u,v);
      float dotUU = dot(u,u);
      u.scale( dotUV/dotUU );
      return u;
   }
   
   public static Vector2D Normal( Vector2D v, boolean side )
   {
      return ( side == false ) ? new Vector2D( -v.y, v.x ) : new Vector2D( v.y, -v.x );
   }
   
   public static Vector2D Normal( Vector2D v1, Vector2D v2, boolean side )
   {
      Vector2D v = new Vector2D( v2.x - v1.x, v2.y - v1.y );
      return ( side == false ) ? new Vector2D( -v.y, v.x ) : new Vector2D( v.y, -v.x );
   }
   
   public static float Distance( Vector2D a, Vector2D b )
   {
      Vector2D d = new Vector2D( a.x - b.x, a.y - b.y );
      return d.magnitude();
   }

   public static float angleBetween( Vector2D a, Vector2D b )
   {
      return (float)(Math.atan2(b.y - a.y, b.x - a.x));
   }
   
   public static Vector2D reflect( Vector2D v, Vector2D n )
   {
      // Assumes normal(n) is normalized
     // -2 * (V dot N) * N + V
     // Or
     // -2 * (V dot N)/|N| *N + V
     
      float dot = (v.x * n.x) + (v.y * n.y);
      Vector2D n2 = new Vector2D( n.x * -2.0f, n.y * -2.0f );
      n2.scale(dot);
      n2.x += v.x;
      n2.y += v.y;
     
      return n2;
   }
   
   public static Vector2D midPoint( Vector2D a, Vector2D b )
   {
      return new Vector2D( (b.x - a.x)/2.0f, (b.y - a.y)/2.0f );
   }
   
   public static Vector2D randomVector( float maxX, float maxY )
   {
      return new Vector2D( (float)(Math.random() * maxX), (float)(Math.random() * maxY) );
   }

   
}  // end class



Thanks!

Offline Mac70
« Reply #1 - Posted 2013-02-22 11:32:55 »

You can make your own float Math class. I suggest you using mostly floats, use doubles only when you need bigger precision.

Check out my Devblog! Smiley
Offline relminator
« Reply #2 - Posted 2013-02-22 11:57:26 »

You can make your own float Math class. I suggest you using mostly floats, use doubles only when you need bigger precision.

I have no idea how to make that (unless you are referring to something like a native code using FPU calls) for Trig transcendental functions like sin, cos, etc.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #3 - Posted 2013-02-23 11:28:26 »

I don't know why the Math.* stuff returns mostly doubles, but generally we all simply cast those doubles into floats.

With "creating your own Math class" Mac70 probably means, you could create your own Math class ontop of Math. So it uses Math.sin, but actually caches the results in an array, so you could access them without actually computing them. LibGDX has done something similar: "MathUtils" on github

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline relminator
« Reply #4 - Posted 2013-02-26 14:11:39 »

You mean LUTs(LookUp Tables)?  I used to do that during the DOS days and when coding for the Nintendo DS but I thought FPU calls are now much faster than RAM access these days.  I'm assuming that Math.* directly calls the FPU for trig funks.

Thanks!

Just found out that casting from a double to float has a speed penalty. ;*(
Online Roquen
« Reply #5 - Posted 2013-02-26 14:49:52 »

In most real world usages, look-up tables will be much slower than basic computation.
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2013-02-26 14:59:00 »

In most real world usages, look-up tables will be much slower than basic computation.

atan2(y, x)
is one exception of them

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline SwampChicken
« Reply #7 - Posted 2013-02-27 02:56:35 »

In most real world usages, look-up tables will be much slower than basic computation.

I disagree. Look up tables are usually faster because there is no computation needed.
Online Roquen
« Reply #8 - Posted 2013-02-27 11:01:34 »

@matheus23: kids and their angles.  I don't see why you need a table lookup for atan2.

@SwampChicken: Basic ALU ops are very fast.  Memory access is very slow.  The fastest memory read is cached 32-bit data chunk from L1 into a register, which takes 5 cycles.  A cache miss might (there a abillion factors here) cost you around 500 cycles.  Take a sandybridge machine which (again this is horseshoes and hand-grenades) can retire 4 ops per cycle, you perform 20 basic ops for the first case (simple load of 32-bits of cached data) and around 2000 in the second.
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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
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!