package com.gebauz.bauzoid.math.collision;
import com.gebauz.bauzoid.graphics.Graphics;
import com.gebauz.bauzoid.graphics.RenderUtil;
import com.gebauz.bauzoid.graphics.sprite.SpriteTransform;
import com.gebauz.bauzoid.math.Line2;
import com.gebauz.bauzoid.math.MathUtil;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.Vector4;
public class EllipseElement
extends BaseShapeElement
{
// Constants========================================================================================
public static final int NUM_LINE_SEGMENTS = RenderUtil.
NUM_ELLIPSE_STEPS;
// Embedded Types===================================================================================
// Fields===========================================================================================
public float x
;
public float y
;
public float radiusX
;
public float radiusY
;
// Methods==========================================================================================
public EllipseElement
(Shape shape,
float initX,
float initY,
float initRadiusX,
float initRadiusY
)
{
super(shape
);
x = initX
; y = initY
;
radiusX = initRadiusX
; radiusY = initRadiusY
;
}
@
Override
public BaseShapeElement copy
(Shape newOwner
)
{
return new EllipseElement
(newOwner, x, y, radiusX, radiusY
);
}
/*
@Override
public CollisionResult collide(Shape shape)
{
CollisionResult result = new CollisionResult();
return result;
}*/
/* @Override
public CollisionResult collideRayCast(Line2 ray)
{
return new CollisionResult();
}*/
@
Override
public void renderDebug
(Graphics graphics, SpriteTransform t
)
{
RenderUtil.
drawEllipse(graphics,
(x-radiusX
) * t.
w,
(y-radiusY
) * t.
h,
(x+radiusX
) * t.
w,
(y+radiusY
) * t.
h, t,
new Vector4
(1,
0,
0,
1));
}
@
Override
public Vector2
[] getUntransformedPoints
()
{
final float angleStep = 360.0f / NUM_LINE_SEGMENTS
;
Vector2
[] points =
new Vector2
[NUM_LINE_SEGMENTS
];
for (int i =
0; i
< NUM_LINE_SEGMENTS
; i++
)
{
points
[i
] =
new Vector2
(x + MathUtil.
sin(angleStep
* i
) * radiusX, y + MathUtil.
cos(angleStep
* i
) * radiusY
);
}
return points
;
}
@
Override
public Vector2
[] getPoints
()
{
final float angleStep = 360.0f / NUM_LINE_SEGMENTS
;
Vector2
[] points =
new Vector2
[NUM_LINE_SEGMENTS
];
SpriteTransform t = getOwner
().
transform;
for (int i =
0; i
< NUM_LINE_SEGMENTS
; i++
)
{
points
[i
] = t.
spriteToWorld(x + MathUtil.
sin(angleStep
* i
) * radiusX, y + MathUtil.
cos(angleStep
* i
) * radiusY
);
points
[i
].
x *= t.
w;
points
[i
].
y *= t.
h;
}
return points
;
}
@
Override
public Line2
[] getUntransformedLineSegments
()
{
final float angleStep = 360.0f / NUM_LINE_SEGMENTS
;
Line2
[] lines =
new Line2
[NUM_LINE_SEGMENTS
];
SpriteTransform t = getOwner
().
transform;
for (int i =
0; i
< NUM_LINE_SEGMENTS
; i++
)
{
float x1 = x + MathUtil.
sin(angleStep
* i
) * radiusX
;
float y1 = y + MathUtil.
cos(angleStep
* i
) * radiusY
;
float x2 = x + MathUtil.
sin(angleStep
* (i +
1)) * radiusX
;
float y2 = y + MathUtil.
cos(angleStep
* (i +
1)) * radiusY
;
x1
*= t.
w;
x2
*= t.
w;
y1
*= t.
h;
y2
*= t.
h;
lines
[i
] =
new Line2
(x1, y1, x2, y2
);
}
return lines
;
}
@
Override
public Line2
[] getLineSegments
()
{
final float angleStep = 360.0f / NUM_LINE_SEGMENTS
;
Line2
[] lines =
new Line2
[NUM_LINE_SEGMENTS
];
SpriteTransform t = getOwner
().
transform;
for (int i =
0; i
< NUM_LINE_SEGMENTS
; i++
)
{
float x1 = x + MathUtil.
sin(angleStep
* i
) * radiusX
;
float y1 = y + MathUtil.
cos(angleStep
* i
) * radiusY
;
float x2 = x + MathUtil.
sin(angleStep
* (i +
1)) * radiusX
;
float y2 = y + MathUtil.
cos(angleStep
* (i +
1)) * radiusY
;
lines
[i
] =
new Line2
(t.
spriteToWorld(x1
* t.
w, y1
* t.
h), t.
spriteToWorld(x2
* t.
w, y2
* t.
h));
}
return lines
;
}
@
Override
public Line2
[] getSweepLineSegments
(Vector2 move
)
{
final float angleStep = 360.0f / NUM_LINE_SEGMENTS
;
Line2
[] lines =
new Line2
[NUM_LINE_SEGMENTS
*2];
SpriteTransform t = getOwner
().
transform;
for (int i =
0; i
< NUM_LINE_SEGMENTS
; i++
)
{
float x1 = x + MathUtil.
sin(angleStep
* i
) * radiusX
;
float y1 = y + MathUtil.
cos(angleStep
* i
) * radiusY
;
float x2 = x + MathUtil.
sin(angleStep
* (i +
1)) * radiusX
;
float y2 = y + MathUtil.
cos(angleStep
* (i +
1)) * radiusY
;
lines
[i
] =
new Line2
(t.
spriteToWorld(x1
* t.
w, y1
* t.
h), t.
spriteToWorld(x2
* t.
w, y2
* t.
h));
}
for (int i =
0; i
< NUM_LINE_SEGMENTS
; i++
)
{
float x1 = x + MathUtil.
sin(angleStep
* i
) * radiusX + move.
x;
float y1 = y + MathUtil.
cos(angleStep
* i
) * radiusY + move.
y;
float x2 = x + MathUtil.
sin(angleStep
* (i +
1)) * radiusX + move.
x;
float y2 = y + MathUtil.
cos(angleStep
* (i +
1)) * radiusY + move.
y;
lines
[i + NUM_LINE_SEGMENTS
] =
new Line2
(t.
spriteToWorld(x1
* t.
w, y1
* t.
h), t.
spriteToWorld(x2
* t.
w, y2
* t.
h));
}
return lines
;
}
@
Override
public Vector2
[] getProjectionAxes
()
{
final float angleStep = 360.0f / NUM_LINE_SEGMENTS
;
Vector2
[] axes =
new Vector2
[NUM_LINE_SEGMENTS/
2];
SpriteTransform t = getOwner
().
transform;
// half suffice
for (int i =
0; i
< (NUM_LINE_SEGMENTS/
2); i++
)
{
float x1 = x + MathUtil.
sin(angleStep
* i
) * radiusX
;
float y1 = y + MathUtil.
cos(angleStep
* i
) * radiusY
;
float x2 = x + MathUtil.
sin(angleStep
* (i +
1)) * radiusX
;
float y2 = y + MathUtil.
cos(angleStep
* (i +
1)) * radiusY
;
Vector2 a = t.
spriteToWorld(x1
* t.
w, y1
* t.
h);
Vector2 b = t.
spriteToWorld(x2
* t.
w, y2
* t.
h);
axes
[i
] =
new Vector2
(b.
x-a.
x, b.
y-a.
y);
}
for (int i =
0; i
< axes.
length; i++
)
{
axes
[0].
set(axes
[0].
y, -axes
[0].
x);
}
return axes
;
}
// Getters/Setters==================================================================================
}