Slick Forums

Discuss the Slick 2D Library
It is currently Thu May 23, 2013 1:08 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Tue Dec 13, 2011 11:42 pm 
Offline

Joined: Tue Dec 13, 2011 11:11 pm
Posts: 2
Hello,
I think that there is something slightly wrong with Vector2F's method. Look:

Code:
   public void setTheta(double theta) {
      // Next lines are to prevent numbers like -1.8369701E-16
      // when working with negative numbers
      if ((theta < -360) || (theta > 360)) {
         theta = theta % 360;
      }
      if (theta < 0) {
         theta = 360 + theta;
      }
      double oldTheta = getTheta();
      if ((theta < -360) || (theta > 360)) {
         oldTheta = oldTheta % 360;   // Line1
      }
      if (theta < 0) {
         oldTheta = 360 + oldTheta;   // Line 2
      }

      float len = length();
      x = len * (float) FastTrig.cos(StrictMath.toRadians(theta));
      y = len * (float) FastTrig.sin(StrictMath.toRadians(theta));
      
//      x = x / (float) FastTrig.cos(StrictMath.toRadians(oldTheta))
//            * (float) FastTrig.cos(StrictMath.toRadians(theta));
//      y = x / (float) FastTrig.sin(StrictMath.toRadians(oldTheta))
//            * (float) FastTrig.sin(StrictMath.toRadians(theta));
   }


Lines marked by me as Line1 and Line2 will never execute.
It is minor mistake, but IS A mistake and it should be repaired.
Also, it is reasonable to extract method like this:

Code:
public static double normalizeAngle(double originalAngle) {
   double normalizedAngle;
   if ((theta < -360) || (theta > 360)) {
      normalizedAngle = oldTheta % 360;
   }
   if (theta < 0) {
      normalizedAngle = 360 + normalizedAngle;
   }
   return normalizedAngle;
}

... and put it in some math tool class.

Thanks for great lib:)
Never give up.


Top
 Profile  
 
PostPosted: Wed Dec 14, 2011 9:31 am 
Offline
Game Developer

Joined: Sun Nov 12, 2006 11:18 pm
Posts: 890
Location: Germany
Thanks for pointing this out.

But having a closer look at it I'm sure the whole oldTheta stuff can be completely removed because oldTheta isn't involved in the rest of the method code at all :roll:

And what's the purpose of your extracted method? The parameter originalAngle is not used at all. You use theta and oldTheta in your calculations which doesn't make sense: you should use the parameter originalAngle, right?

Something like this:
Code:
public static double normalizeAngle(double originalAngle) {
   double normalizedAngle = originalAngle;
   if ((originalAngle< -360) || (originalAngle> 360)) {
      normalizedAngle = originalAngle% 360;
   }
   if (normalizedAngle < 0) {
      normalizedAngle = 360 + normalizedAngle;
   }
   return normalizedAngle;
}


Cheers,
Tommy

_________________
Right Angle Games | Marte Engine
Back to the past | Star Cleaner | SpiderTrap


Top
 Profile  
 
PostPosted: Wed Dec 14, 2011 11:32 am 
Offline

Joined: Tue Dec 13, 2011 11:11 pm
Posts: 2
Yes, off course you're right. It should look like that. And I should rest sometimes.
Extracted method should be used to avoid code duplication that leads to such errors. If that kind of angle normalization happens once or twice in your code than there is no need for separate method. But if it is more common it's better to extract it, that is my opinion. But hey, it's your library:) You'll do what you think is best;)
Best regards,
halfdan


Top
 Profile  
 
PostPosted: Wed Dec 14, 2011 12:21 pm 
Offline
Game Developer

Joined: Sun Nov 12, 2006 11:18 pm
Posts: 890
Location: Germany
It's not my library :lol:
Kevin Glass created it. I'm just a guy who uses Slick and once in 5 years dares to commit one or the other source change :wink:

And I understood why you wanted to extract that method which does of course make sense. I was just wondering about the sense of the method due to its bugs. Just bad wording on my side. I'm no native english speaker so sometimes my choice of words doesn't match my intended meaning :oops:

Cheers,
Tommy

_________________
Right Angle Games | Marte Engine
Back to the past | Star Cleaner | SpiderTrap


Top
 Profile  
 
PostPosted: Thu Dec 15, 2011 7:38 pm 
Offline
User avatar

Joined: Fri Oct 07, 2011 11:05 pm
Posts: 97
Location: France, Nantes
There's also an issue with the dot product: if the two vector aren't normalise, the result will be just wrong.

We should ensure something like that:
Code:
public float dot(Vector2f other) {

return (getNormal().x * other.getNormal().getX()) + (getNormal().y * other.getNormal().getY());
}

_________________
"The Keeper is aware. The Keeper understands. The Keeper has seen the enemy."


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 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