Slick Forums

Discuss the Slick 2D Library
It is currently Fri May 24, 2013 2:50 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed May 18, 2011 4:31 pm 
Offline

Joined: Wed May 18, 2011 4:16 pm
Posts: 2
I was reading through the Vector2f code the other day and found a few things that could be improved:

in the setTheta method these lines of code are unused.
Code:
double oldTheta = getTheta();
if ((theta < -360) || (theta > 360)) {
   oldTheta = oldTheta % 360;
}
if (theta < 0) {
   oldTheta = 360 + oldTheta;
}


also in getTheta these lines are unnecessary as atan2 wont return a value outside +-PI.
Code:
if ((theta < -360) || (theta > 360)) {
   theta = theta % 360;   
}


While i was playing around i also noticed that slick uses a FastTrig lib for cos and sin. So i figured i might post this here:
http://http.developer.nvidia.com/Cg/atan2.html

Translated to java:
Code:
private float atan2(float y, float x)
   {
     float t0, t1, t3, t4;

     float xAbs = Math.abs(x);
     float yAbs = Math.abs(y);
    
     t3 = xAbs;
     t1 = yAbs;
     t0 = Math.max(t3, t1);
     t1 = Math.min(t3, t1);
     t3 = 1f / t0;
     t3 = t1 * t3;

     t4 = t3 * t3;
     t0 =         - 0.013480470f;
     t0 = t0 * t4 + 0.057477314f;
     t0 = t0 * t4 - 0.121239071f;
     t0 = t0 * t4 + 0.195635925f;
     t0 = t0 * t4 - 0.332994597f;
     t0 = t0 * t4 + 0.999995630f;
     t3 = t0 * t3;

     t3 = (yAbs > xAbs) ? 1.570796327f - t3 : t3;
     t3 = (x < 0) ?  3.141592654f - t3 : t3;
     t3 = (y < 0) ? -t3 : t3;

     return t3;
   }


In the particles code i found that slick doesn't use java generics and casts to ParticleEmitter and ParticlePool a whole lot.
A while back i rewrote the particle code myself and could get rid of all the casts, though i did not test if there was any performance gain.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2011 5:24 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1173
Java generics just cause the compiler to insert the casts for you.

Instead of trying a custom version of ATAN2 (which also looks kind of slow) it would be better to get rid of atan2 directly.
The following code:
Code:
float angle = (float)Math.atan2(dx, dy);
g.rotate(angle);

could be replaced by:
Code:
float len = (float)Math.sqrt(dx*dx + dy*dy);
dx /= len;
dy /= len;
g.rotateFromSinCos(dx, dy);

This works because the normalized vector is already sin/cos of the desired angle.
(of course this method would need to be added first).

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2011 9:52 pm 
Offline

Joined: Wed May 18, 2011 4:16 pm
Posts: 2
Well i made a test just now Math lib vs the nvidia version, and the nvidia one is two times faster, and it looks fast as there is almost only addition and multiplication in the implementation.

The use of sqrt instead might be even faster though, especially if the vector is already normalized.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group