rotation stuff is failing pretty hard, shelfing

rotation
Marcus Gosselin 2016-02-24 01:27:50 -05:00
parent 07c4572ce3
commit 54de4b5ee2
5 changed files with 85 additions and 27 deletions

View File

@ -1,5 +1,7 @@
package diveengine2d;
import java.awt.Polygon;
public class DiveMath {
public static boolean inTriangle(Vector2 a, Vector2 b, Vector2 c, Vector2 p) {
@ -11,12 +13,27 @@ public class DiveMath {
Vector2 p1ma = p1.subtract(a);
Vector2 p2ma = p2.subtract(a);
float cp1 = bma.x*p1ma.y-bma.y*p1ma.x;
float cp2 = bma.x*p2ma.y-bma.y*p2ma.x;
double cp1 = bma.x*p1ma.y-bma.y*p1ma.x;
double cp2 = bma.x*p2ma.y-bma.y*p2ma.x;
return cp1/cp2 >= 0;
}
public static Polygon rotatePolygon(Polygon p, double angle, Vector2 center) {
//p.translate((int)center.x, (int)center.y);
for(int i = 0; i < p.npoints; i ++) {
Vector2 oldPosition = new Vector2(p.xpoints[i], p.ypoints[i]).toPolar();
Vector2 newPosition = new Vector2(oldPosition.x + angle, oldPosition.y).toRect();
p.xpoints[i] = (int) newPosition.x;
p.ypoints[i] = (int) newPosition.y;
}
//p.translate((int)-center.x, (int)-center.y);
return p;
}
/*
function SameSide(p1,p2, a,b)
cp1 = CrossProduct(b-a, p1-a)

View File

@ -101,18 +101,7 @@ public class Engine extends Canvas {
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
render(g);
DebugSettings.render(g);
Vector2 a = new Vector2(512, 100);
Vector2 b = new Vector2(256, 200);
Vector2 c = new Vector2(768, 400);
Vector2 mouse = new Vector2(Input.mouseX, Input.mouseY);
g.setColor(DiveMath.inTriangle(a, b, c, mouse) ? Color.GREEN : Color.RED);
g.fillOval((int)a.x, (int)a.y, 10, 10);
g.fillOval((int)b.x, (int)b.y, 10, 10);
g.fillOval((int)c.x, (int)c.y, 10, 10);
Tests.run(g);
bs.show();
}

View File

@ -17,14 +17,18 @@ public class RectRenderer extends DiveScript{
public void render(Graphics2D g) {
//g.rotate(Math.toRadians(i+=Time.deltaTime));
g.setColor(color);
Polygon p = new Polygon(new int[]{0, width, width, 0}, new int[]{0, 0, height, height}, 4);
for(int i = 0; i < p.npoints; i ++) {
}
p.translate((int)entity.x, (int)entity.y);
g.fillPolygon(p);
//g.fillRect((int)entity.x, (int)entity.y, width, height);
/* MAINTAINED FOR USAGE IN ROTATION BRANCH
Polygon polygon = new Polygon(new int[]{0, width, width, 0}, new int[]{0, 0, height, height}, 4);
if(entity.name.equals("Player"))
polygon = DiveMath.rotatePolygon(polygon, (float)Math.toRadians(i+=Time.deltaTime), new Vector2(entity.x + width/2, entity.y + height/2));
polygon = DiveMath.rotatePolygon(polygon, 0, new Vector2(entity.x + width/2, entity.y + height/2));
polygon.translate((int)entity.x, (int)entity.y);
g.fillPolygon(polygon);
*/
g.fillRect((int)entity.x, (int)entity.y, width, height);
//g.rotate(Math.toRadians(-i));
}
}

View File

@ -0,0 +1,41 @@
package diveengine2d;
import java.awt.Color;
import java.awt.Graphics2D;
public class Tests {
public static void run(Graphics2D g) {
triangleTest(g);
coordinateTest(g);
}
private static void coordinateTest(Graphics2D g) {
Vector2 a = new Vector2(512, 300);
Vector2 mouse = new Vector2(Input.mouseX, Input.mouseY);
g.setColor(Color.RED);
g.fillOval((int)a.x, (int)a.y, 10, 10);
Vector2 polar = mouse.subtract(a).toPolar();
g.setColor(Color.BLACK);
g.drawString("r: " + polar.y, 100, 200);
g.drawString("theta: " + polar.x, 100, 220);
}
private static void triangleTest(Graphics2D g) {
Vector2 a = new Vector2(512, 100);
Vector2 b = new Vector2(256, 200);
Vector2 c = new Vector2(768, 400);
Vector2 mouse = new Vector2(Input.mouseX, Input.mouseY);
g.setColor(DiveMath.inTriangle(a, b, c, mouse) ? Color.GREEN : Color.RED);
g.fillOval((int)a.x, (int)a.y, 10, 10);
g.fillOval((int)b.x, (int)b.y, 10, 10);
g.fillOval((int)c.x, (int)c.y, 10, 10);
}
}

View File

@ -1,8 +1,8 @@
package diveengine2d;
public class Vector2 {
public float x, y;
public Vector2(float x, float y) {
public double x, y;
public Vector2(double x, double y) {
this.x = x;
this.y = y;
}
@ -13,9 +13,16 @@ public class Vector2 {
return _return;
}
public Vector2 rectToPolar(Vector2 b) {
Vector2 _return = new Vector2(x, y);
return _return;
public Vector2 toPolar() {
double radius = (double) Math.sqrt( x * x + y * y );
double angleInRadians = (double) Math.acos( x / radius );
if(this.y > 0) angleInRadians = 2*(double)Math.PI - ((double) Math.acos( x / radius ));
return new Vector2(angleInRadians, radius);
}
public Vector2 toRect() {
double x = (double) (Math.cos( this.x ) * this.y);
double y = (double) (-Math.sin( this.x ) * this.y);
return new Vector2(x, y);
}
}