From 54de4b5ee205255606b1eeb775ed3926510ab7f1 Mon Sep 17 00:00:00 2001 From: Marcus Gosselin Date: Wed, 24 Feb 2016 01:27:50 -0500 Subject: [PATCH] rotation stuff is failing pretty hard, shelfing --- src/diveengine2d/DiveMath.java | 21 +++++++++++++-- src/diveengine2d/Engine.java | 13 +--------- src/diveengine2d/RectRenderer.java | 18 ++++++++----- src/diveengine2d/Tests.java | 41 ++++++++++++++++++++++++++++++ src/diveengine2d/Vector2.java | 19 +++++++++----- 5 files changed, 85 insertions(+), 27 deletions(-) create mode 100644 src/diveengine2d/Tests.java diff --git a/src/diveengine2d/DiveMath.java b/src/diveengine2d/DiveMath.java index 4524f96..4bcd367 100644 --- a/src/diveengine2d/DiveMath.java +++ b/src/diveengine2d/DiveMath.java @@ -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) diff --git a/src/diveengine2d/Engine.java b/src/diveengine2d/Engine.java index 1c74324..0f62ce2 100644 --- a/src/diveengine2d/Engine.java +++ b/src/diveengine2d/Engine.java @@ -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(); } diff --git a/src/diveengine2d/RectRenderer.java b/src/diveengine2d/RectRenderer.java index 8dd67d0..4e62af9 100644 --- a/src/diveengine2d/RectRenderer.java +++ b/src/diveengine2d/RectRenderer.java @@ -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)); } } diff --git a/src/diveengine2d/Tests.java b/src/diveengine2d/Tests.java new file mode 100644 index 0000000..5735ac6 --- /dev/null +++ b/src/diveengine2d/Tests.java @@ -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); + } + +} diff --git a/src/diveengine2d/Vector2.java b/src/diveengine2d/Vector2.java index 7dd18f7..33f2af3 100644 --- a/src/diveengine2d/Vector2.java +++ b/src/diveengine2d/Vector2.java @@ -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); } }