Commit fbfe7d9f authored by ronan's avatar ronan
Browse files

Save work

parent ffa8d05d
Pipeline #2303 failed with stage
in 30 seconds
......@@ -8,8 +8,6 @@ public class Circle extends RoundForm
*/
private static final long serialVersionUID = 1L;
private float radius;
public Circle()
{
super();
......@@ -36,17 +34,14 @@ public class Circle extends RoundForm
this.radius = radius;
}
public Circle(Circle circle)
public Circle(Form form)
{
super();
this.radius = circle.getRadius();
orientation.setPos(circle.getCenter());
super(form);
}
public void set(Circle circle)
{
this.radius = circle.getRadius();
orientation.setPos(circle.getCenter());
super.set(circle);
}
public void set(Vector2D center, float radius)
......@@ -62,28 +57,16 @@ public class Circle extends RoundForm
}
@Override
public Object clone()
public Circle clone()
{
return new Circle(this);
}
@Override
public void scale(float factor, Vector2D center)
{
super.scale(factor, center);
this.radius *= factor;
}
public void setRadius(float radius)
{
this.radius = radius;
}
public float getRadius()
{
return radius;
}
public float getMinX()
{
return getCenterX() - radius;
......@@ -144,7 +127,7 @@ public class Circle extends RoundForm
public boolean isColliding(Circle circle)
{
Vector2D vec = new Vector2D(circle.getCenter(), this.getCenter());
if (vec.getMagnitude() <= this.radius + circle.radius)
if (vec.getSqMagnitude() <= (this.radius + circle.radius)*(this.radius + circle.radius))
return true;
return false;
}
......
......@@ -15,13 +15,16 @@ public class Form implements Serializable, Cloneable
protected Matrix3 orientation;
protected ArrayList<Vector2D> points;
protected float radius;
protected float xMin, xMax, yMin, yMax;
protected ArrayList<Vector2D> savedVectorsLocal;
/**
* @param size number of points
*/
public Form(int size)
{
points = new ArrayList<Vector2D>(size);
for (int i = 0; i < size; i++)
{
......@@ -33,6 +36,7 @@ public class Form implements Serializable, Cloneable
scale = 1f;
flipH = false;
flipV = false;
radius = 0;
orientation = Matrix3.orientation(omega, scale, flipH, flipV,
new Vector2D());
......@@ -40,7 +44,6 @@ public class Form implements Serializable, Cloneable
public Form()
{
points = new ArrayList<Vector2D>();
omega = 0;
......@@ -71,17 +74,42 @@ public class Form implements Serializable, Cloneable
this.scale = form.getScale();
this.flipH = form.getFlipH();
this.flipV = form.getFlipV();
this.radius = form.getOriginalRadius();
this.xMin = form.getXMinRel();
this.xMax = form.getXMaxRel();
this.yMin = form.getYMinRel();
this.yMax = form.getYMaxRel();
orientation = Matrix3.orientation(omega, scale, flipH, flipV,
form.getCenter());
}
public Object clone()
@Override
public Form clone()
{
Form form = new Form(this);
return form;
}
/**
*
* @return scale with scaling
*/
public float getRadius()
{
return radius*scale;
}
/**
*
* @return scale without scaling
*/
public float getOriginalRadius()
{
return radius;
}
/**
* Clear all transformations
*/
......@@ -165,6 +193,7 @@ public class Form implements Serializable, Cloneable
}
Vector2D center = new Vector2D();
float x0 = 0, y0 = 0, x1 = 0, y1 = 0, signedArea = 0, a = 0;
ArrayList<Vector2D> points = this.getPointsLocal();
for (int j = points.size() - 1, i = 0; i < points.size(); j = i, i++)
{
......@@ -178,7 +207,7 @@ public class Form implements Serializable, Cloneable
center.y += (y0 + y1) * a;
}
signedArea *= 0.5;
signedArea *= 0.5f;
center.x /= (6f * signedArea);
center.y /= (6f * signedArea);
......@@ -198,6 +227,8 @@ public class Form implements Serializable, Cloneable
Matrix3 inverse = newCoor.inverse();
Matrix3 result = inverse.multiply(lastCoor);
radius = 0;
for (int i = 0; i < points.size(); i++)
{
/*
......@@ -207,14 +238,167 @@ public class Form implements Serializable, Cloneable
* orientation.multiply(p); Vector2D pN = inverse.multiply(pW);
*/
points.get(i).set(result.multiply(points.get(i)));
radius = Math.max(points.get(i).getSqMagnitude(), radius);
}
radius = (float) Math.sqrt(radius);
orientation.setPos(newCenter);
updateVectorsLocal();
}
/**
* Update local vectors
*/
public void updateVectorsLocal()
{
savedVectorsLocal.clear();
for (int j = size() - 1, i = 0; i < size(); j = i, i++)
{
Vector2D v = new Vector2D(points.get(j), points.get(i));
savedVectorsLocal.add(v.getPerpendicular());
}
// On enleve les vectors colinéaires
for (int i = 0; i < savedVectorsLocal.size() - 1; i++)
{
// Si on est en dessous de 2 vecteurs ca sert à rien de continuer,
// On sait que ces 2 vecteurs (ou moins) ne sont pas colinéaires
if (savedVectorsLocal.size() < 3)
break;
for (int j = i + 1; j < savedVectorsLocal.size(); j++)
{
if (savedVectorsLocal.get(i).isColinear(savedVectorsLocal.get(j)))
{
savedVectorsLocal.remove(i);
i--;
break;
}
}
}
}
/**
* Update min max x, y
*/
public void updateBound()
{
Vector2D v = points.get(0).getRotatedRadians(omega);
xMin = yMin = v.x;
xMax = yMax = v.y;
for (int i = 1; i < points.size(); i++)
{
v = points.get(i).getRotatedRadians(omega);
if (v.x < xMin)
{
xMin = v.x;
}
else if (v.x > xMax)
{
xMax = v.x;
}
if (v.y < yMin)
{
yMin = v.y;
}
else if (v.y > yMax)
{
yMax = v.y;
}
}
xMin *= scale * ((flipV) ? 1 : -1);
xMax *= scale * ((flipV) ? 1 : -1);
if (flipV)
{
float temp = xMin;
xMin = - xMax * scale;
xMax = - temp * scale;
}
else
{
xMin *= scale;
xMax *= scale;
}
if (flipH)
{
float temp = yMin;
yMin = - yMax * scale;
yMax = - temp * scale;
}
else
{
yMin *= scale;
yMax *= scale;
}
}
/**
* Get saved edge vector in local coordinates
* @return list of vec
*/
public ArrayList<Vector2D> getSavedVectorsLocal()
{
return savedVectorsLocal;
}
/**
* Call update Bound first
* Get saved x Min in a relative coordinate
* With scale transformation
* @return xMin
*/
public float getXMinRel()
{
return xMin;
}
/**
* Call update Bound first
* Get saved x Max in a relative coordinate
* With scale transformation
* @return xMax
*/
public float getXMaxRel()
{
return xMax;
}
/**
* Call update Bound first
* Get saved y Min in a relative coordinate
* With scale transformation
* @return xMin
*/
public float getYMinRel()
{
return yMin;
}
/**
* Call update Bound first
* Get saved y Max in a relative coordinate
* With scale transformation
* @return yMax
*/
public float getYMaxRel()
{
return yMax;
}
/**
* return min and max projection of the polygon on the vector axis
*
* @param axis
* @param axis projection axis
*
* @return MinMax
*/
......
......@@ -35,6 +35,7 @@ public class Rectangle extends Form
side.set(points.get(0), points.get(1));
length.y = side.getMagnitude();
}
@Override
public Rectangle clone()
{
......@@ -53,6 +54,17 @@ public class Rectangle extends Form
set(center, length, omega);
}
@Override
public void updateVectorsLocal()
{
savedVectorsLocal.clear();
for (int i = 0; i < size(); i++)
{
savedVectorsLocal.add(new Vector2D(points.get(i+1), points.get(i)).getPerpendicular());
}
}
/**
* @param x center of the rectangle
* @param y center of the rectangle
......
package org.cora.maths;
public abstract class RoundForm extends Form
public class RoundForm extends Form
{
/**
*
*/
private static final long serialVersionUID = 1L;
public RoundForm()
{
super();
}
public RoundForm(Form form)
{
super(form);
}
public void set(Form form)
{
set(form);
}
}
......@@ -62,9 +62,20 @@ public class Vector2D implements Serializable, Cloneable
z = p.z;
}
public Object clone()
public Vector2D clone()
{
return new Vector2D(this);
Vector2D vec;
try
{
vec = (Vector2D) super.clone();
}
catch (CloneNotSupportedException e)
{
return null;
}
return vec;
}
public void reset()
......@@ -140,7 +151,7 @@ public class Vector2D implements Serializable, Cloneable
public boolean isColinear(Vector2D v)
{
if (x * v.y - y * v.x == 0) // vectoriel
if (Math.abs(x * v.y - y * v.x) < 0.0001f) // vectoriel
return true;
else
return false;
......@@ -225,10 +236,17 @@ public class Vector2D implements Serializable, Cloneable
}
// Transformations
public void translate(Vector2D p)
public void translate(Vector2D vec)
{
this.x += p.x;
this.y += p.y;
this.x += vec.x;
this.y += vec.y;
}
public Vector2D getTranslated(Vector2D vec)
{
Vector2D v = (Vector2D) clone();
v.translate(vec);
return v;
}
public void translateX(float vecX)
......@@ -236,11 +254,25 @@ public class Vector2D implements Serializable, Cloneable
x += vecX;
}
public Vector2D getTranslatedX(float vecX)
{
Vector2D vec = (Vector2D) clone();
vec.translateX(vecX);
return vec;
}
public void translateY(float vecY)
{
y += vecY;
}
public Vector2D getTranslatedY(float vecY)
{
Vector2D vec = (Vector2D) clone();
vec.translateY(vecY);
return vec;
}
public void rotateDegrees(float omega, Vector2D center)
{
// omega = Extend.modulo(omega, 360f);
......@@ -256,22 +288,98 @@ public class Vector2D implements Serializable, Cloneable
y = (float) (Math.cos(omega) * (l_y - center.y) + Math.sin(omega) * (l_x - center.x) + center.y);
}
public Vector2D getRotatedRadians(float omega, Vector2D center)
{
Vector2D vec = (Vector2D) clone();
vec.rotateRadians(omega, center);
return vec;
}
public void rotateRadians(float omega)
{
omega = -omega;
float l_x = x;
float l_y = y;
x = (float) (Math.cos(omega) * l_x - Math.sin(omega) * l_y);
y = (float) (Math.cos(omega) * l_y + Math.sin(omega) * l_x);
}
public Vector2D getRotatedRadians(float omega)
{
Vector2D vec = (Vector2D) clone();
vec.rotateRadians(omega);
return vec;
}
public void scale(float factor, Vector2D center)
{
x = factor * (x - center.x) + center.x;
y = factor * (y - center.y) + center.y;
}
public Vector2D getScaled(float factor, Vector2D center)
{
Vector2D vec = (Vector2D) clone();
vec.scale(factor, center);
return vec;
}
public void scale(float factor)
{
x = factor * x;
y = factor * y;
}
public Vector2D getScaled(float factor)
{
Vector2D vec = (Vector2D) clone();
vec.scale(factor);
return vec;
}
public void flipV(Vector2D center)
{
x = -x + 2 * center.x;
}
public Vector2D getFlippedV(Vector2D center)
{
Vector2D vec = (Vector2D) clone();
vec.flipV(center);
return vec;
}
public void flipV() { x = - x; }
public Vector2D getFlippedV()
{
Vector2D vec = (Vector2D) clone();
vec.flipV();
return vec;
}
public void flipH(Vector2D center)
{
y = -y + 2 * center.y;
}
public Vector2D getFlippedH(Vector2D center)
{
Vector2D vec = (Vector2D) clone();
vec.flipH(center);
return vec;
}
public void flipH() { y = - y; }
public Vector2D getFlippedH()
{
Vector2D vec = (Vector2D) clone();
vec.flipH();
return vec;
}
public float getAngle(Vector2D A, Vector2D C)
{
// http://math.stackexchange.com/questions/149959/how-to-find-the-interior-angle-of-an-irregular-pentagon-or-polygon
......
......@@ -16,7 +16,7 @@ public class CollisionDetector
* @param VA velocity of object A
* @param VB velocity of object B
* @param push a blank vector to store the penetration vector
* @param t a blank float to store the result of amount of penetration
* @param t dt
* @return result of collision detection testing
*/
public static boolean isColliding(Form A, Form B, Vector2D VA, Vector2D VB,
......@@ -34,13 +34,13 @@ public class CollisionDetector
* @param VA velocity of object A
* @param VB velocity of object B
* @param push a blank vector to store the penetration vector
* @param t a blank float to store the result of amount of penetration
* @param t dt
* @return result of collision detection testing
*/
public static boolean isCollidingOptimised(Form A, Form B, Circle cA, Circle cB, Vector2D VA, Vector2D VB,
Vector2D push, FloatA t)
{
return collisionSat(cA, cB, VA, VB, push, t) && collisionSat(A, B, VA, VB, push, t);
return collisionSat(cA, cB, VA, VB, push, new FloatA(t.v)) && collisionSat(A, B, VA, VB, push, t);
}
private static boolean collisionSat(Form A, Form B, Vector2D VA,
......@@ -72,7 +72,7 @@ public class CollisionDetector
AxesSat axesSat = new AxesSat();
float squaredVel = relVel.getSqMagnitude();
if (squaredVel > 0.000001f)
if (squaredVel > 0.00001f)
{
if (!intervalIntersection(A, B, relVel.getPerpendicular(), relPos,
relVel, orientI, axesSat, t))
......@@ -101,7 +101,6 @@ public class CollisionDetector
push.set(OB.multiply(push));
// System.out.println("Collision");
return true;
}
......@@ -109,7 +108,6 @@ public class CollisionDetector
Vector2D relPos, Vector2D relVel, Matrix2 orientI, AxesSat axesSat,
FloatA t)
{
axis.normalize();
Vector2D minMaxA = A.getInterval(orientI.<