All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.blastedstudios.gdxworld.world.shape.GDXPolygon Maven / Gradle / Ivy

The newest version!
package com.blastedstudios.gdxworld.world.shape;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.blastedstudios.gdxworld.math.PolygonUtils;
import com.blastedstudios.gdxworld.physics.PhysicsHelper;

public class GDXPolygon extends GDXShape implements Serializable{
	private static final long serialVersionUID = 1L;
	private static int count = 0;
	private boolean repeatable;
	/**
	 * Coordinates for vertices relative to center. To convert to world 
	 * coordinates, use the absolute version, e.g. getVerticesAbsolute
	 */
	private List vertices = new ArrayList();
	/**
	 * Dimensions of poly after getting aabb's max/min x/y. Cached for performance
	 */
	private transient Vector2 dimensions;
	
	public GDXPolygon(){
		name = "Polygon-" + count++;
	}

	public List getVertices() {
		return vertices;
	}

	public List getVerticesAbsolute() {
		if(center == null || vertices.isEmpty())
			return vertices;
		return PolygonUtils.getCenterVerticesReverse(vertices, center);
	}

	public void setVertices(List vertices) {
		this.vertices = vertices;
	}

	public void setVerticesAbsolute(List vertices) {
		this.vertices = PolygonUtils.getCenterVertices(vertices, center);
	}

	public boolean isRepeatable() {
		return repeatable;
	}

	public void setRepeatable(boolean repeatable) {
		this.repeatable = repeatable;
	}
	
	/**
	 * @param overrideStatic use BodyType.StaticBody no matter what bodyType is set to
	 */
	public Body createFixture(World world, boolean overrideStatic){
		FixtureDef fd = new FixtureDef();
		fd.density = density;
		fd.friction = friction;
		fd.restitution = restitution;
		BodyType type = overrideStatic ? BodyType.StaticBody : bodyType;
		Body body = PhysicsHelper.createFixture(world, fd, type, vertices, new PolygonShape(), 
				getFilter().maskBits, getFilter().categoryBits, getFilter().groupIndex);
		if(body != null){
			body.setTransform(center, 0);
			body.setUserData(name);
		}
		return body;
	}

	public Vector2 getClosestVertex(float x, float y) {
		return PolygonUtils.getClosestNode(x, y, getVerticesAbsolute());
	}
	
	@Override public String toString(){
		return "[GDXPolygon name:" + name + " center:" + center + "]";
	}

	@Override public float getDistance(float x, float y) {
		float closestDistance = Float.MAX_VALUE;
		for(Vector2 vertex : vertices)
			closestDistance = Math.min(closestDistance, vertex.cpy().add(center).dst2(x, y));
		return (float) Math.sqrt(closestDistance);//just doing one sqrt, assuming its faster..?
	}

	@Override public Object clone() {
		GDXPolygon clone = new GDXPolygon();
		for(Vector2 vertex : vertices)
			clone.getVertices().add(vertex.cpy());
		clone.setRepeatable(repeatable);
		return super.clone(clone);
	}

	@Override public Vector2 getDimensions() {
		if(dimensions == null)
			dimensions = PolygonUtils.getDimensions(vertices.toArray(new Vector2[vertices.size()]));
		return dimensions;
	}
	
	public void scl(float scalar){
		for(Vector2 vertex : vertices)
			vertex.scl(scalar);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy