Rev 1260 |
Rev 1262 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package com.gebauz.bauzoid.math.collision;
import java.util.Vector;
import com.gebauz.bauzoid.graphics.sprite.SpriteTransform;
import com.gebauz.bauzoid.math.Vector2;
/** Axis-aligned bounding box. */
public class AABoundingBox
{
// Constants========================================================================================
// Embedded Types===================================================================================
// Fields===========================================================================================
public Vector2 min =
new Vector2
();
public Vector2 max =
new Vector2
();
// Methods==========================================================================================
public AABoundingBox
(float left,
float top,
float right,
float bottom
)
{
min.
x = min.
x; min.
y = min.
y; max.
x = max.
x; max.
y = max.
y;
}
public AABoundingBox
(Vector2 minimum, Vector2 maximum
)
{
min.
x = minimum.
x;
min.
y = minimum.
y;
max.
x = maximum.
x;
max.
y = maximum.
y;
}
public AABoundingBox
(Vector<Vector2
> points
)
{
min.
x =
Float.
MAX_VALUE;
min.
y =
Float.
MAX_VALUE;
max.
x = -
Float.
MAX_VALUE;
max.
y = -
Float.
MAX_VALUE;
for (int i =
0; i
< points.
size(); i++
)
{
Vector2 v = points.
get(i
);
if (v.
x < min.
x)
min.
x = v.
x;
if (v.
x > max.
x)
max.
x = v.
x;
if (v.
y < min.
y)
min.
y = v.
y;
if (v.
y > max.
y)
max.
y = v.
y;
}
}
public AABoundingBox
(Vector2
[] points
)
{
min.
x =
Float.
MAX_VALUE;
min.
y =
Float.
MAX_VALUE;
max.
x = -
Float.
MAX_VALUE;
max.
y = -
Float.
MAX_VALUE;
for (int i =
0; i
< points.
length; i++
)
{
Vector2 v = points
[i
];
if (v.
x < min.
x)
min.
x = v.
x;
if (v.
x > max.
x)
max.
x = v.
x;
if (v.
y < min.
y)
min.
y = v.
y;
if (v.
y > max.
y)
max.
y = v.
y;
}
}
public boolean isInside
(Vector2 point
)
{
return ((point.
x >= min.
x) &&
(point.
x <= max.
x) &&
(point.
y >= min.
y) &&
(point.
y <= max.
y));
}
public boolean intersetcs
(AABoundingBox other
)
{
return !((other.
min.
x > max.
x) ||
(other.
max.
x < min.
x) ||
(other.
min.
y > max.
y) ||
(other.
max.
y < min.
y));
}
public void transform
(SpriteTransform t, AABoundingBox result
)
{
t.
spriteToWorld(min.
x, min.
y, result.
min);
t.
spriteToWorld(max.
x, max.
y, result.
max);
}
public AABoundingBox createFromTransformed
(SpriteTransform t
)
{
Vector2 points
[] =
new Vector2
[4];
for (int i =
0; i
< 4; i++
)
{
points
[i
] =
new Vector2
();
}
t.
spriteToWorld(min.
x * t.
w, min.
y * t.
h, points
[0]);
t.
spriteToWorld(min.
x * t.
w, max.
y * t.
h, points
[1]);
t.
spriteToWorld(max.
x * t.
w, max.
y * t.
h, points
[2]);
t.
spriteToWorld(max.
x * t.
w, min.
y * t.
h, points
[2]);
return new AABoundingBox
(points
);
}
/*
public AABoundingBox transform(SpriteTransform t)
{
Vector2 points[] = new Vector2[4];
points[0] = t.spriteToWorld(min.x * t.w, min.y * t.w);
points[1] = t.spriteToWorld(max.x * t.w, min.y * t.w);
points[2] = t.spriteToWorld(max.x * t.w, max.y * t.w);
points[3] = t.spriteToWorld(min.x * t.w, max.y * t.w);
return new AABoundingBox(points);
}*/
/** Create a bounding box from a ray. */
public static AABoundingBox fromRay
(Vector2 pos, Vector2 velocity
)
{
Vector2 points
[] =
new Vector2
[4];
points
[0] =
new Vector2
(pos.
x, pos.
y);
points
[1] =
new Vector2
(pos.
x + velocity.
x, pos.
y);
points
[2] =
new Vector2
(pos.
x + velocity.
x, pos.
y + velocity.
y);
points
[3] =
new Vector2
(pos.
x, pos.
y + velocity.
y);
return new AABoundingBox
(points
);
}
// Getters/Setters==================================================================================
}