Author Topic: FlxPoint Additions  (Read 2177 times)

CodeBaier

  • Guest
FlxPoint Additions
« on: Mon, Feb 22, 2010 »
Edit:
About self: New to Flixel, used it for a small tower defense game for my Game Production class. Though I somehow accidentally used a older version of Flixel. The game itself turned out alright, but our artist wasn't that great so it looks shotty.
/Edit

Some additional functionality for FlxPoint (overriding the class altogether), plus three other classes: FlxRadians, FlxDegrees (both of which are just containers for Number and conversions between), and FlxLine. This allows for some good line and point geometries, useful for certain applications (ie more advanced collision physics).

Cannot get the attachment to work, so here is the source code (four separate classes):

<code>
package org.flixel
{
   import org.flixel.data.FlxDegrees;
   import org.flixel.data.FlxRadians;
   import org.flixel.data.FlxLine;
   /**
    * Stores a 2D floating point coordinate.
    */
   public class FlxPoint
   {
      /**
       * @default 0
       */
      public var x:Number;
      /**
       * @default 0
       */
      public var y:Number;
      
      /**
       * Instantiate a new point object.
       *
       * @param   X      The X-coordinate of the point in space.
       * @param   Y      The Y-coordinate of the point in space.
       */
      public function FlxPoint( arg0:Number=0, arg1:Object=null ) {
          if( arg1 is Number ) create_from_points( arg0, Number(arg1) );
          else if( arg1 is int ) create_from_points( arg0, int( arg1 ) );
          else if( arg1 == null ) create_from_points( 0,0 );
          else if( arg1 is FlxRadians ) create_from_FlxRadians( arg0, FlxRadians(arg1) );
          else create_from_FlxDegrees( arg0, FlxDegrees(arg1) );
      }
      public function create_from_points( xx:Number, yy:Number ):void {
         x = xx;
         y = yy;
      }
      public function create_from_FlxRadians( mag:Number, rad:FlxRadians ):void {
         x = mag * Math.cos( rad.value );
         y = mag * Math.sin( rad.value );
      }
      public function create_from_FlxDegrees( mag:Number, deg:FlxDegrees ):void {
         x = mag * Math.cos( deg.to_radians().value );
         y = mag * Math.sin( deg.to_radians().value );
      }
      
      
      // Math Operations on FlxPoint's
      public function v_add( v2:FlxPoint ):FlxPoint { return new FlxPoint( this.x + v2.x, this.y + v2.y ); }
      public function v_sub( v2:FlxPoint ):FlxPoint { return new FlxPoint( this.x - v2.x, this.y - v2.y ); }
      public function v_mul( v2:FlxPoint ):FlxPoint { return new FlxPoint( this.x * v2.x, this.y * v2.y ); }
      public function v_div( v2:FlxPoint ):FlxPoint { return new FlxPoint( this.x / v2.x, this.y / v2.y ); }
      public function v_mod( v2:FlxPoint ):FlxPoint { return new FlxPoint( this.x % v2.x, this.y % v2.y ); }
      
      // Scalar Math Operations
      public function s_add( s:Number ):FlxPoint { return new FlxPoint( this.x + s, this.y + s ); }
      public function s_sub( s:Number ):FlxPoint { return new FlxPoint( this.x - s, this.y - s ); }
      public function s_mul( s:Number ):FlxPoint { return new FlxPoint( this.x * s, this.y * s ); }
      public function s_div( s:Number ):FlxPoint { return new FlxPoint( this.x / s, this.y / s ); }
      public function s_mod( s:Number ):FlxPoint { return new FlxPoint( this.x % s, this.y % s ); }
      
      // Returns a new FlxPoint containing the floor of this FlxPoint      
      public function floor():FlxPoint {
         return new FlxPoint( Math.floor(x), Math.floor(y) );
      }
      
      // Returns a new FlxPoint containing the ceil of this FlxPoint
      public function ceil():FlxPoint {
         return new FlxPoint( Math.ceil(x), Math.ceil(y) );
      }
      
      // Returns the radians angle of this point (considered a vector)
      public function radians():Number {
         return Math.atan2( y, x );
      }
      
      // Returns a copy of the FlxPoint
      public function copy():FlxPoint {
         return new FlxPoint( x,y );
      }
      
      // Return a new FlxPoint containing the abs of this FlxPoint
      public function abs():FlxPoint {
         return new FlxPoint( Math.abs(x), Math.abs(y) );
      }
      
      // Returns "(x,y)"
      public function print():String {
         return new String( "(" + x + "," + y + ")" );
      }
      
      // Geturns the degrees angle of this point (considered a vector)
      public function degrees():Number {
         return new FlxRadians( Math.atan2( y, x ) ).to_degrees().value;
      }
      
      // Get the FlxPoint at same angle with a magnitue of 1
      public function normalized():FlxPoint {
         return new FlxPoint( 1.0, new FlxRadians( this.radians() ) );
      }
      
      // Dot product with another FlxPoint
      public function dot( v2:FlxPoint ):Number {
         return this.x*v2.x + this.y*v2.y;
      }
      
      // Cross Product with another FlxPoint
      public function cross( v2:FlxPoint ):Number {
         return (this.x*v2.y - this.y*v2.x);
      }
      
      // Project this FlxPoint onto a FlxLine
      // Returns the FlxPoint on the FlxLine
      public function projection_on( line:FlxLine ):FlxPoint {
         var p1:FlxPoint = line.pt1;
         var A:FlxPoint = this.v_sub( p1 );
         var B:FlxPoint = line.pt2.v_sub( p1 );
         return p1.v_add( B.s_mul(A.dot(B) / B.dot(B)) );
      }
      
      // Distance to another FlxPoint
      public function distance_to( other:FlxPoint ):Number {
         return (other.v_sub(this)).magnitude();
      }
      
      // Magnitude of this FlxPoint
      public function magnitude():Number {
         return Math.sqrt( x^2 + y^2 );
      }
      
      // Clamps FlxPoint onto a line
      public function clamped( line:FlxLine ):FlxPoint {
         var p1:FlxPoint = line.pt1;
         var p2:FlxPoint = line.pt2;
         var A:FlxPoint = this.v_sub( p1 );
         var B:FlxPoint = p2.v_sub( p1 );
         var t_num:Number = A.dot( B );
         
         if( t_num < 0 ) return p1;
         var t_den:Number = B.dot( B );
         if( t_num > t_den ) return p2;
         var td:Number = t_num/t_den;
         return p1.v_add( (new FlxPoint( td, td )).v_mul( B ) );
      }
      
      // Normal of the FlxPoint
      public function normal():FlxPoint {
         return new FlxPoint( -y, x );
      }
      
      /**
       * Convert object to readable string name.  Useful for debugging, save games, etc.
       */
      public function toString():String
      {
         return FlxU.getClassName(this,true);
      }
   }
}

package org.flixel.data
{
   import org.flixel.FlxPoint;
   
   public class FlxLine {
      public var pt1:FlxPoint;
      public var pt2:FlxPoint;
      
      public function FlxLine( p1:FlxPoint, p2:FlxPoint ) {
         pt1 = p1;
         pt2 = p2;
      }
      
      // Returns the center of the line
      public function center():FlxPoint {
         return (pt1.v_add( pt2 ) ).s_div(2.0);
      }
      
      // Returns the midpoint of the line (yes, same as center)
      public function midpoint():FlxPoint {
         return (pt1.v_add( pt2 ) ).s_div(2.0);
      }
      
      // Returns length of the line
      public function length():Number {
         return (pt2.v_sub( pt1 ) ).magnitude();
      }
      
      // Return the point of intersection with another line
      public function intersection( line:FlxLine ):FlxPoint {
         var P1:FlxPoint = this.pt1;
         var P2:FlxPoint = this.pt2;
         var P3:FlxPoint = line.pt1;
         var P4:FlxPoint = line.pt2;
         var A:FlxPoint = P2.v_sub( P1 );
         var B:FlxPoint = P4.v_sub( P3 );
         var C:FlxPoint = P3.v_sub( P1 );
         var den:Number = B.cross( A );
         var t0:Number = B.cross( C ) / den;
         var t1:Number = A.cross( C ) / den;
         if( t0 < 0.0 || t0 > 1.0 || t1 < 0.0 || t1 > 1.0 ) return null;
         return P1.v_add( A.s_mul( t0 ) );
      }
      
      // Return whether or not this line intersects with another
      public function intersects_line( line:FlxLine ):Boolean {
         if( this.intersection( line ) is null ) return false;
         else return true;
      }
      
   }
}

package org.flixel.data
{
   public class FlxRadians {
      public var value:Number=0;
      public function FlxRadians( input:Number=0 ) { value = input; }
      public function to_degrees():FlxDegrees {
         return new FlxDegrees( (value*180)/Math.PI );
      }
   }
}

package org.flixel.data
{
   public class FlxDegrees {
      public var value:Number=0;
      public function FlxDegrees( input:Number=0 ) { value = input; }
      public function to_radians():FlxRadians {
         return new FlxRadians( (value*Math.PI)/180 );
      }
   }
}
</code>
« Last Edit: Mon, Feb 22, 2010 by CodeBaier »