Package org.samcrow.vehicle

Source Code of org.samcrow.vehicle.SteerableWheel

/**
*
*/
package org.samcrow.vehicle;

import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.LinkedList;
import java.util.List;

import org.lekan.graphics.SGLine;
import org.lekan.graphics.SGObject;


/**
* @author scamper
*
*/
public class SteerableWheel extends Wheel {
 
  /**
   * The rotation, clockwise looking from above, relative to the vehicle front, of this wheel.
   * This is rotation is applied after the X and Y translations described by localX and localY.
   */
  private double angle;

  /**
   * Construct a Wheel with a given location relative to the Vehicle origin
   * @param inX The X-axis (toward the right of the car) locaton relative to the car origin
   * @param inY The Y-axis (toward the front of the car) location relative to the car origin
   * @param inParent the Vehicle that this Wheel is attached to
   */
  public SteerableWheel(double inX, double inY, Vehicle inParent) {
    super(inX, inY, inParent);
  }
 
  public void setAngle(double inAngle){
    angle = inAngle;
  }
 
  public double getAngle(){
    return angle;
  }

  /* (non-Javadoc)
   * @see org.samcrow.vehicle.Wheel#getDrawObjects()
   */
  public List<SGObject> getDrawObjects(){
    List<SGObject> objects = new LinkedList<SGObject>();
    //for regular wheels: rotation is parent.getHeading()
    final double vehicleHeading = parent.getHeading();
   
    //create a rectangle in the center of the vehicle facing north
    final Rectangle2D.Double rect = new Rectangle2D.Double(
        parent.getX() - WIDTH / 2d,
        parent.getY() - LENGTH / 2d,
        WIDTH,
        LENGTH);
   
    AffineTransform transform = new AffineTransform();
    transform.translate(parent.getX(), parent.getY());
    transform.rotate(Math.toRadians(vehicleHeading));//rotate
    transform.translate(-parent.getX(), -parent.getY());
    transform.translate(localX, localY + LENGTH / 2);//translate to local position
    transform.translate(parent.getX(), parent.getY());
    transform.rotate(Math.toRadians(parent.getSteeringAngle()));
    transform.translate(-parent.getX(), -parent.getY());
    final Shape result = transform.createTransformedShape(rect);
   
    PathIterator iterator = result.getPathIterator(null);
    Point2D.Double lastPoint = null;
    while(!iterator.isDone()){//iterate through all points
      double[] coords = new double[8];
      int type = iterator.currentSegment(coords);
     
      if(type == PathIterator.SEG_MOVETO){
        lastPoint = new Point2D.Double(coords[0], coords[1]);
      }else if(type == PathIterator.SEG_LINETO){
        assert(lastPoint != null);//this must be after a point of type 0
        SGLine line = new SGLine((int)Math.round(lastPoint.x), (int)Math.round(lastPoint.y), (int)Math.round(coords[0]), (int)Math.round(coords[1]));
        objects.add(line);
        lastPoint = new Point2D.Double(coords[0], coords[1]);
        //System.out.println("Adding line "+line);
      }
     
      iterator.next();
    }
   
    return objects;
  }

}
TOP

Related Classes of org.samcrow.vehicle.SteerableWheel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.