Skip to content

Commit

Permalink
Change polygon, store vertices in local coords, compute world coords …
Browse files Browse the repository at this point in the history
…as needed.

git-svn-id: https://libgdx.googlecode.com/svn/trunk@4209 6c4fd544-2939-11df-bb46-9574ba5d0bfa
  • Loading branch information
nathan.sweet committed Jun 19, 2012
1 parent 29da84b commit c64f7f8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 47 deletions.
3 changes: 2 additions & 1 deletion gdx/src/com/badlogic/gdx/math/Interpolation.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

package com.badlogic.gdx.math;

/** Takes a linear value in the range of 0-1 and outputs a (usually) non-linear, interpolated value. */
/** Takes a linear value in the range of 0-1 and outputs a (usually) non-linear, interpolated value.
* @author Nathan Sweet */
public abstract class Interpolation {
abstract public float apply (float a);

Expand Down
4 changes: 2 additions & 2 deletions gdx/src/com/badlogic/gdx/math/Intersector.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ public static boolean overlapConvexPolygons (Polygon p1, Polygon p2) {
* @param separation Normalized vector defining a direction of the separation axis (optional).
* @return Whether polygons overlap. */
public static boolean overlapConvexPolygons (Polygon p1, Polygon p2, Vector2 separation) {
final float[] verts1 = p1.getVertices();
final float[] verts2 = p2.getVertices();
final float[] verts1 = p1.getWorldVertices();
final float[] verts2 = p2.getWorldVertices();
return !separateConvexPolygons(verts1, verts2, separation) && !separateConvexPolygons(verts2, verts1, separation);
}

Expand Down
7 changes: 4 additions & 3 deletions gdx/src/com/badlogic/gdx/math/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

import com.badlogic.gdx.utils.NumberUtils;

/** Utility and fast math functions.<br>
* <br>
* Thanks to Riven on JavaGaming.org for sin/cos/atan2/floor/ceil.<br> */
/** Utility and fast math functions.
* <p>
* Thanks to Riven on JavaGaming.org for the basis of sin/cos/atan2/floor/ceil.
* @author Nathan Sweet */
public class MathUtils {
static public final float PI = 3.1415927f;

Expand Down
69 changes: 36 additions & 33 deletions gdx/src/com/badlogic/gdx/math/Polygon.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,51 @@
package com.badlogic.gdx.math;

public class Polygon {

private final float[] vertices;
private final float[] localVertices;
private float[] worldVertices;
private float x, y;
private float originX, originY;
private float rotation;
private float scaleX = 1, scaleY = 1;
private boolean dirty;
private Rectangle bounds = new Rectangle();
private boolean dirty = true;
private Rectangle bounds;

public Polygon (float[] vertices) {
if (vertices.length < 6) throw new IllegalArgumentException("polygons must contain at least 3 points.");
this.vertices = vertices;
this.localVertices = vertices;
}

public float[] getVertices () {
if (!dirty) return vertices;
/** Returns vertices without scaling or rotation and without being offset by the polygon position. */
public float[] getLocalVertices () {
return localVertices;
}

float[] vertices = this.vertices;
final int numFloats = vertices.length;
/** Returns vertices scaled, rotated, and offset by the polygon position. */
public float[] getWorldVertices () {
if (!dirty) return localVertices;
dirty = false;

final float translateX = x + originX;
final float translateY = y + originY;
final float[] localVertices = this.localVertices;
if (worldVertices == null || worldVertices.length < localVertices.length) worldVertices = new float[localVertices.length];

final float[] worldVertices = this.worldVertices;
final float positionX = x;
final float positionY = y;
final float originX = this.originX;
final float originY = this.originY;
final float scaleX = this.scaleX;
final float scaleY = this.scaleY;
final boolean scale = scaleX != 1 || scaleY != 1;
final float rotation = this.rotation;
final float cos = MathUtils.cosDeg(rotation);
final float sin = MathUtils.sinDeg(rotation);
float x, y;
for (int i = 0; i < numFloats; i += 2) {
x = vertices[i];
y = vertices[i + 1];

// move vertices to local coordinates
x -= translateX;
y -= translateY;
for (int i = 0, n = localVertices.length; i < n; i += 2) {
float x = localVertices[i] - originX;
float y = localVertices[i + 1] - originY;

// scale if needed
if (scaleX != 1 || scaleY != 1) {
if (scale) {
x *= scaleX;
y *= scaleY;
}
Expand All @@ -63,17 +73,10 @@ public float[] getVertices () {
y = sin * oldX + cos * y;
}

// move vertices back to world coordinates
x += translateX;
y += translateY;

vertices[i] = x;
vertices[i + 1] = y;
worldVertices[i] = positionX + x + originX;
worldVertices[i + 1] = positionY + y + originY;
}

dirty = false;

return vertices;
return worldVertices;
}

public void setOrigin (float originX, float originY) {
Expand Down Expand Up @@ -119,7 +122,7 @@ public void scale (float amount) {
public float area () {
float area = 0;

float[] vertices = getVertices();
float[] vertices = getWorldVertices();
final int numFloats = vertices.length;

int x1, y1, x2, y2;
Expand All @@ -137,8 +140,7 @@ public float area () {
}

public Rectangle getBoundingRectangle () {

float[] vertices = getVertices();
float[] vertices = getWorldVertices();

float minX = vertices[0];
float minY = vertices[1];
Expand All @@ -153,6 +155,7 @@ public Rectangle getBoundingRectangle () {
maxY = maxY < vertices[i + 1] ? vertices[i + 1] : maxY;
}

if (bounds == null) bounds = new Rectangle();
bounds.x = minX;
bounds.y = minY;
bounds.width = maxX - minX;
Expand All @@ -162,7 +165,7 @@ public Rectangle getBoundingRectangle () {
}

public boolean contains (float x, float y) {
final float[] vertices = getVertices();
final float[] vertices = getWorldVertices();
final int numFloats = vertices.length;
int intersects = 0;

Expand Down
2 changes: 1 addition & 1 deletion gdx/src/com/badlogic/gdx/math/Vector2.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Vector2 implements Serializable {
/** Static temporary vector. Use with care! Use only when sure other code will not also use this.
* @see #tmp() **/
public final static Vector2 tmp = new Vector2();

public final static Vector2 X = new Vector2(1, 0);
public final static Vector2 Y = new Vector2(0, 1);
public final static Vector2 Zero = new Vector2(0, 0);
Expand All @@ -39,7 +40,6 @@ public class Vector2 implements Serializable {

/** Constructs a new vector at (0,0) */
public Vector2 () {

}

/** Constructs a vector with the given components
Expand Down
19 changes: 12 additions & 7 deletions gdx/src/com/badlogic/gdx/math/Vector3.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,28 @@

import com.badlogic.gdx.utils.NumberUtils;

/** Encapsulates a 3D vector. Allows chaining operations by returning a reference to it self in all modification methods.
*
/** Encapsulates a 3D vector. Allows chaining operations by returning a reference to itself in all modification methods.
* @author badlogicgames@gmail.com */
public class Vector3 implements Serializable {
private static final long serialVersionUID = 3840054589595372522L;

/** the x-component of this vector **/
public float x;
/** the x-component of this vector **/
public float y;
/** the x-component of this vector **/
public float z;

private static Vector3 tmp = new Vector3();
private static Vector3 tmp2 = new Vector3();
private static Vector3 tmp3 = new Vector3();
/** Static temporary vector. Use with care! Use only when sure other code will not also use this.
* @see #tmp() **/
public final static Vector3 tmp = new Vector3();
/** Static temporary vector. Use with care! Use only when sure other code will not also use this.
* @see #tmp() **/
public final static Vector3 tmp2 = new Vector3();
/** Static temporary vector. Use with care! Use only when sure other code will not also use this.
* @see #tmp() **/
public final static Vector3 tmp3 = new Vector3();

public final static Vector3 X = new Vector3(1, 0, 0);
public final static Vector3 Y = new Vector3(0, 1, 0);
public final static Vector3 Z = new Vector3(0, 0, 1);
Expand All @@ -45,7 +52,6 @@ public Vector3 () {
}

/** Creates a vector with the given components
*
* @param x The x-component
* @param y The y-component
* @param z The z-component */
Expand All @@ -54,7 +60,6 @@ public Vector3 (float x, float y, float z) {
}

/** Creates a vector from the given vector
*
* @param vector The vector */
public Vector3 (Vector3 vector) {
this.set(vector);
Expand Down

0 comments on commit c64f7f8

Please sign in to comment.