MathUtil distance

  ... the case you feel the need for a new feature or want to submit one.

MathUtil distance

Postby NightJumper » Sun Oct 10, 2010 5:07 pm

Would be nice to be able to get the distance between two points, I did some code already if this helps:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public float distance(float X1, float X2, float Y1, float Y2){
  2.         return FloatMath.sqrt(((X2-X1)*(X2-X1))+((Y2-Y1)*(Y2-Y1)));
  3.     }
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


This could be used like this to get distance between two sprites:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final float[] sprite1Coordinates = sprite1.convertLocalToSceneCoordinates(sprite1.getWidth()/2, sprite1.getHeight()/2);
  2.         final float X1 =sprite1Coordinates[0];
  3.         final float Y1 = sprite1Coordinates[1];
  4.         final float[] sprite2Coordinates = sprite2.convertLocalToSceneCoordinates(sprite2.getWidth()/2, sprite2.getHeight()/2);
  5.         final float X2 = sprite2Coordinates[0];
  6.         final float Y2 = sprite2Coordinates[1];
  7. final dist=MathUtils.distance(X1, X2, Y1, Y2)
Parsed in 0.010 seconds, using GeSHi 1.0.8.4
Last edited by NightJumper on Mon Oct 11, 2010 2:05 am, edited 1 time in total.
NightJumper
 
Posts: 98
Joined: Wed Sep 01, 2010 9:42 pm

Re: MathUtil distance

Postby Nicolas Gramlich » Sun Oct 10, 2010 7:48 pm

Hi,

some tips:

Use FloatMath.xyz instead of Math.xyz where available.

Did you check if Math.pow(x, 2) is faster than just x*x :?:

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: MathUtil distance

Postby NightJumper » Mon Oct 11, 2010 2:06 am

Thanks for the tip I updated my first post, and did some research (didnt actually run test just looked it up) and with low powers like 2,3,4 its about the same but most said probably a little better to just multiply it out so I changed that as well.
NightJumper
 
Posts: 98
Joined: Wed Sep 01, 2010 9:42 pm

Re: MathUtil distance

Postby Nicolas Gramlich » Mon Oct 11, 2010 2:30 am

Okay cool. Added it with a minor improvement. Will push it soon.

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany


Return to Features

Who is online

Users browsing this forum: No registered users and 10 guests