Package collision

Source Code of collision.CollisionTests

package collision;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

public class CollisionTests {
 
  Point p1;
  Point p2;
  Point p3;
  Point p4;
  Point pOben;
  Point pUnten;
  Point robotPos;
 
  public static void main(String[] args) {
    new CollisionTests();
  }
 
  public CollisionTests() {
    final Display d = new Display();
      final Shell shell = new Shell(d);

      shell.setSize(250, 200);
      shell.setLayout(new GridLayout());

      // Create a canvas
      GridData canGrid = new GridData();
      canGrid.grabExcessHorizontalSpace = true;
      canGrid.horizontalAlignment = SWT.FILL;
      canGrid.grabExcessVerticalSpace = true;
      canGrid.verticalAlignment = SWT.FILL;
      final Canvas canvas = new Canvas(shell, SWT.NONE);
      canvas.setLayoutData(canGrid);
     
      final Spinner heading = new Spinner(shell, SWT.NONE);
      heading.setMinimum(-1);
      heading.setMaximum(360);
     
      heading.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
          if (heading.getSelection() == 360) {
            heading.setSelection(0);
          }
         
          if (heading.getSelection() == -1) {
            heading.setSelection(359);
          }
         
          canvas.redraw();
        }
      });

      canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
          e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLACK));
         
          calcBoundsOfRectangleWithAngle(100, 100, 150, 100, heading.getSelection());
         
          e.gc.drawText("rechts oben", p1.x, p1.y);
          e.gc.drawText("rechts unten", p2.x, p2.y);
          e.gc.drawText("links unten", p3.x, p3.y);
          e.gc.drawText("links oben", p4.x, p4.y);
          e.gc.drawText("oben", pOben.x, pOben.y);
          e.gc.drawText("unten", pUnten.x, pUnten.y);
          e.gc.drawText("Mitte", robotPos.x, robotPos.y);
        }
      });   
     
      shell.setSize(800, 600);
      shell.open();
      while (!shell.isDisposed()) {
        if (!d.readAndDispatch())
          d.sleep();
      }
      d.dispose();
  }

  public void calcBoundsOfRectangleWithAngle(final int robotPosX,
      final int robotPosY, int robotWidht, int robotHeight, int robotHeading) {
//    System.out.println("RobotPos: " + robotPosX + "/" + robotPosY
//        + " width: " + robotWidht + " height: " + robotHeight
//        + " heading: " + robotHeading);
   
    robotPos = new Point(robotPosX, robotPosY);

    double robotWidhtHalf = robotWidht / 2;
    double robotHeightHalf = robotHeight / 2;

    // Die 4 Eckpunkte des Roboters berechnen (als Rechteck)
    // Berechne den ersten Punkt (rechts oben):
    double widthLineRightAngle = Math.toRadians(robotHeading);
    double cosineOfLineRightAngle = Math.cos(widthLineRightAngle);
    double sineOfLineRightAngle =   Math.sin(widthLineRightAngle);
    int p1Xright = (int) (robotWidhtHalf * cosineOfLineRightAngle);
    int p1Yright = (int) (robotWidhtHalf * sineOfLineRightAngle);

    double widthLineRightUpAngle = Math.toRadians(robotHeading - 90);
    double cosineOfLineRightUpAngle = Math.cos(widthLineRightUpAngle);
    double sineOfLineRightUpAngle =   Math.sin(widthLineRightUpAngle);
    int p1XrightUp = (int) (robotHeightHalf * cosineOfLineRightUpAngle);
    int p1YrightUp = (int) (robotHeightHalf * sineOfLineRightUpAngle);

    int p1X = robotPosX + p1Xright + p1XrightUp;
    int p1Y = robotPosY + p1Yright + p1YrightUp;
    p1 = new Point(p1X, p1Y);

    // Berechne den zweiten Punkt (rechts unten):
    int p2Xright = p1Xright; // �bernehmen von p1 da erste Line nach rechts gleich
    int p2Yright = p1Yright; // �bernehmen von p1 da erste Line nach rechts gleich

    double widhtLineRightDownAngle = Math.toRadians(robotHeading + 90);
    double cosineOfLineRightDownAngle = Math.cos(widhtLineRightDownAngle);
    double sineOfLineRightDownAngle =   Math.sin(widhtLineRightDownAngle);
    int p2XrightDown = (int) (robotHeightHalf * cosineOfLineRightDownAngle);
    int p2YrightDown = (int) (robotHeightHalf * sineOfLineRightDownAngle);

    int p2X = robotPosX + p2Xright + p2XrightDown;
    int p2Y = robotPosY + p2Yright + p2YrightDown;
    p2 = new Point(p2X, p2Y);
   
    //Berechne den dritten Punkt (links unten):
    double widthLineLeftAngle = Math.toRadians(robotHeading + 180);
    double cosineOfLineLeftAngle = Math.cos(widthLineLeftAngle);
    double sineOfLineLeftAngle =   Math.sin(widthLineLeftAngle);
    int p3Xleft = (int) (robotWidhtHalf * cosineOfLineLeftAngle);
    int p3Yleft = (int) (robotWidhtHalf * sineOfLineLeftAngle);

    double widthLineLeftDownAngle = Math.toRadians(robotHeading + 90);
    double cosineOfLineLeftDownAngle = Math.cos(widthLineLeftDownAngle);
    double sineOfLineLeftDownAngle =   Math.sin(widthLineLeftDownAngle);
    int p3XleftDown = (int) (robotHeightHalf * cosineOfLineLeftDownAngle);
    int p3YleftDown = (int) (robotHeightHalf * sineOfLineLeftDownAngle);

    int p3X = robotPosX + p3Xleft + p3XleftDown;
    int p3Y = robotPosY + p3Yleft + p3YleftDown;
    p3 = new Point(p3X, p3Y);
   
    //Berechne den vierten Punkt (links oben):
    int p4Xleft = p3Xleft; // �bernehmen von p3 da erste Line nach links gleich
    int p4Yleft = p3Yleft; // �bernehmen von p3 da erste Line nach links gleich
   
    double widthLineLeftUpAngle = Math.toRadians(robotHeading + 270);
    double cosineOfLineLeftUpAngle = Math.cos(widthLineLeftUpAngle);
    double sineOfLineLeftUpAngle =   Math.sin(widthLineLeftUpAngle);
    int p4XleftUp = (int) (robotHeightHalf * cosineOfLineLeftUpAngle);
    int p4YleftUp = (int) (robotHeightHalf * sineOfLineLeftUpAngle);
   
    int p4X = robotPosX + p4Xleft + p4XleftUp;
    int p4Y = robotPosY + p4Yleft + p4YleftUp;
    p4 = new Point(p4X, p4Y);

    // Nur nach oben bzw. unten:
    pOben = new Point(p1XrightUp + robotPosX, p1YrightUp + robotPosY);
    pUnten = new Point(p2XrightDown + robotPosX, p2YrightDown + robotPosY);
   
    // Ausgabe der berechneten Werte:
//    System.out.println("p1 rechts oben: " + p1);
//    System.out.println("p2 rechts unten: " + p2);
//    System.out.println("p3 links unten: " + p3);
//    System.out.println("p4 links oben: " + p4);
//    System.out.println("oben: " + pOben);
//    System.out.println("unten: " + pUnten);
    }
  }
TOP

Related Classes of collision.CollisionTests

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.