package com.pwnscone.drummachine.actors;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.pwnscone.drummachine.Game;

public class Ball extends Actor {

	@Override
	public void create() {
		super.create();
		if (mMainBody == null) {
			World world = Game.get().getLevel().getWorld();
			BodyDef bodyDef = new BodyDef();
			bodyDef.type = BodyType.DynamicBody;
			bodyDef.bullet = true;
			Body body = world.createBody(bodyDef);
			mMainBody = body;
			mMainBody.setUserData(this);

			CircleShape circle = new CircleShape();
			circle.setRadius(.2f);

			FixtureDef fixtureDef = new FixtureDef();
			fixtureDef.shape = circle;
			fixtureDef.density = 0.5f;
			fixtureDef.friction = 0.4f;
			fixtureDef.restitution = 0.85f;

			body.createFixture(fixtureDef);
			circle.dispose();
		} else {
			mMainBody.setActive(true);
		}
		mMainBody.setLinearVelocity(Vector2.Zero);
		mMainBody.setAngularVelocity(0.0f);
	}

	@Override
	public void update() {
		super.update();
		if (mMainBody.isActive()) {
			if (mMainBody.getPosition().len2() > 100000.0f) {
				Game.get().getLevel().destroyActor(this);
			}
		}
	}

	@Override
	public void collide(Fixture otherFixture) {
		super.collide(otherFixture);
		if (mCollided || otherFixture.isSensor()) {
			return;
		}
		mCollided = true;
		// Game.get().getSynth().hiHatClosed();
	}
}

@@ -455,95 +455,376 @@
 me;%0A
-%0Apublic class Ball extends Actor %7B%0A%0A%09@Override%0A%09public void create() %7B%0A%09%09super.create()
+import com.pwnscone.drummachine.util.Misc;%0A%0Apublic class Ball extends Actor %7B%0A%09private static int POS_HIST_SIZE = 30;%0A%09private static float EPSILON = 0.001f;%0A%0A%09private float%5B%5D xPos;%0A%09private float%5B%5D yPos;%0A%09private int histIndex;%0A%0A%09@Override%0A%09public void create() %7B%0A%09%09super.create();%0A%09%09xPos = new float%5BPOS_HIST_SIZE%5D;%0A%09%09yPos = new float%5BPOS_HIST_SIZE%5D;%0A%09%09histIndex = 0
 ;%0A%09%09
@@ -1613,20 +1613,30 @@
 )) %7B%0A%09%09%09
-if (
+Vector2 pos = 
 mMainBod
@@ -1650,16 +1650,28 @@
 sition()
+;%0A%09%09%09if (pos
 .len2() 
@@ -1731,16 +1731,379 @@
 (this);%0A
+%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%09xPos%5BhistIndex%5D = pos.x;%0A%09%09%09yPos%5BhistIndex%5D = pos.y;%0A%09%09%09histIndex++;%0A%09%09%09if (histIndex == POS_HIST_SIZE) %7B%0A%09%09%09%09histIndex = 0;%0A%09%09%09%09Vector2 posCache = Misc.v2r0;%0A%09%09%09%09for (int i = 0; i %3C POS_HIST_SIZE; i++) %7B%0A%09%09%09%09%09if (pos.dst2(xPos%5BhistIndex%5D, yPos%5BhistIndex%5D) %3E EPSILON) %7B%0A%09%09%09%09%09%09return;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09Game.get().getLevel().destroyActor(this);%0A
 %09%09%09%7D%0A%09%09%7D

Remove Balls that don't move for 30 frames