// (c) MIT 2003.  All rights reserved.

package rec.simulate.widgets.wm;
// -*-Mode: Java-*- 
// WMSquare.java -- 
// Author: Christine Alvarado <calvarad@ai.mit.edu> 
// Maintainer: Christine Alvarado <calvarad@ai.mit.edu> 
// Version: 
// Created: <Wed Mar 10 19:09:22 1999> 
// Time-stamp: <2001-09-10 18:28:38 moltmans> 
// Keywords: 


import rec.util.*;
import java.io.*;
import rec.geo.*;
import rec.core.widgets.*;
import java.lang.*;
import rec.meche.WMForce;
import rec.simulate.*;
import rec.simulate.widgets.*;

// class used when an interface to Working Model is desired.
public class WMSimForce extends WSimForce
{

  public WMSimForce( WMForce s )
  {
    super( s );
  }

  // the key to this class is the load funciton, which "loads"
  // the object into the simulator.  that is, prints it to the file.
  // it takes a WMSimulator so that it knows the conversion factors
  // that it should use to convert dimensions.
  // it needs another parameter to resolve name conflicts.  The system
  // is seriously limited at the moment.
  public String load( WMSimulatorRegistryBase reg ) 
  {
    GPoint newEnd1 = reg.convert( getHeadPoint() );
    String output;

    Console.writeln( "loading force" );
    
    if ( isGravity ) {
	return( "" );
    }

    output = "Dim " + m_Name + " as WMConstraint\n";
   
    // attach the endpoints to bodies, if they are attached to bodies
    // we assume at this point that the bodies have already been loaded
    // otherwise this does not work.
    if ( m_body1 != null ) {
      GPoint newBody1 = reg.convert( m_body1.getLocation().getCenter() );
      newEnd1.x -= newBody1.x;
      newEnd1.y -= newBody1.y;
    }
    
    // since working model doesn't seem to want to let me use polar coords,
    // we have to do the conversion here
    // for now the default strength of the force will be 10 newtons.
    // much more conplicated stuff will have to be done later to change this

    double length = ((GSegment) m_Components[0]).getLength();
    System.out.println(length);

    
    double force = length/10; //10.0;
    double f_x = Math.cos( m_Angle ) * force;
    double f_y = Math.sin( m_Angle ) * force;
    
    output += "t1.x = " + newEnd1.x + " : t1.y = " + newEnd1.y + " \n";
    output +=  "Set " + m_Name + " = Draw_Force( t1, " + f_x + ", " +
      f_y + " )\n" ;
    if ( m_body1 != null ) {
      output +=  "Set " + m_Name + ".Point(1).Body = " + 
	m_body1.getName() + "\n";
      Console.writeln( "attatching " + m_Name + " to " + m_body1.getName() );
    }

    return output;

  }

///////////////////////////////////////////////////////////////////////////  
//

}
