use of org.jbox2d.common.Vec2 in project libgdx by libgdx.
the class DebugDraw method getWorldToScreen.
/**
* takes the world coordinate (argWorld) and returns the screen coordinates.
*
* @param argWorld
*/
public Vec2 getWorldToScreen(Vec2 argWorld) {
Vec2 screen = new Vec2();
viewportTransform.getWorldToScreen(argWorld, screen);
return screen;
}
use of org.jbox2d.common.Vec2 in project libgdx by libgdx.
the class AABB method raycast.
/**
* From Real-time Collision Detection, p179.
*
* @param output
* @param input
*/
public final boolean raycast(final RayCastOutput output, final RayCastInput input, IWorldPool argPool) {
float tmin = -Float.MAX_VALUE;
float tmax = Float.MAX_VALUE;
final Vec2 p = argPool.popVec2();
final Vec2 d = argPool.popVec2();
final Vec2 absD = argPool.popVec2();
final Vec2 normal = argPool.popVec2();
p.set(input.p1);
d.set(input.p2).subLocal(input.p1);
Vec2.absToOut(d, absD);
// x then y
if (absD.x < Settings.EPSILON) {
// Parallel.
if (p.x < lowerBound.x || upperBound.x < p.x) {
argPool.pushVec2(4);
return false;
}
} else {
final float inv_d = 1.0f / d.x;
float t1 = (lowerBound.x - p.x) * inv_d;
float t2 = (upperBound.x - p.x) * inv_d;
// Sign of the normal vector.
float s = -1.0f;
if (t1 > t2) {
final float temp = t1;
t1 = t2;
t2 = temp;
s = 1.0f;
}
// Push the min up
if (t1 > tmin) {
normal.setZero();
normal.x = s;
tmin = t1;
}
// Pull the max down
tmax = MathUtils.min(tmax, t2);
if (tmin > tmax) {
argPool.pushVec2(4);
return false;
}
}
if (absD.y < Settings.EPSILON) {
// Parallel.
if (p.y < lowerBound.y || upperBound.y < p.y) {
argPool.pushVec2(4);
return false;
}
} else {
final float inv_d = 1.0f / d.y;
float t1 = (lowerBound.y - p.y) * inv_d;
float t2 = (upperBound.y - p.y) * inv_d;
// Sign of the normal vector.
float s = -1.0f;
if (t1 > t2) {
final float temp = t1;
t1 = t2;
t2 = temp;
s = 1.0f;
}
// Push the min up
if (t1 > tmin) {
normal.setZero();
normal.y = s;
tmin = t1;
}
// Pull the max down
tmax = MathUtils.min(tmax, t2);
if (tmin > tmax) {
argPool.pushVec2(4);
return false;
}
}
// Does the ray intersect beyond the max fraction?
if (tmin < 0.0f || input.maxFraction < tmin) {
argPool.pushVec2(4);
return false;
}
// Intersection.
output.fraction = tmin;
output.normal.x = normal.x;
output.normal.y = normal.y;
argPool.pushVec2(4);
return true;
}
use of org.jbox2d.common.Vec2 in project libgdx by libgdx.
the class AABB method getCenter.
/**
* Get the center of the AABB
*
* @return
*/
public final Vec2 getCenter() {
final Vec2 center = new Vec2(lowerBound);
center.addLocal(upperBound);
center.mulLocal(.5f);
return center;
}
use of org.jbox2d.common.Vec2 in project libgdx by libgdx.
the class AABB method set.
/**
* Sets this object from the given object
*
* @param aabb the object to copy from
*/
public final void set(final AABB aabb) {
Vec2 v = aabb.lowerBound;
lowerBound.x = v.x;
lowerBound.y = v.y;
Vec2 v1 = aabb.upperBound;
upperBound.x = v1.x;
upperBound.y = v1.y;
}
use of org.jbox2d.common.Vec2 in project libgdx by libgdx.
the class Collision method findMaxSeparation.
/**
* Find the max separation between poly1 and poly2 using edge normals from poly1.
*
* @param edgeIndex
* @param poly1
* @param xf1
* @param poly2
* @param xf2
* @return
*/
public final void findMaxSeparation(EdgeResults results, final PolygonShape poly1, final Transform xf1, final PolygonShape poly2, final Transform xf2) {
int count1 = poly1.m_count;
int count2 = poly2.m_count;
Vec2[] n1s = poly1.m_normals;
Vec2[] v1s = poly1.m_vertices;
Vec2[] v2s = poly2.m_vertices;
Transform.mulTransToOutUnsafe(xf2, xf1, xf);
final Rot xfq = xf.q;
int bestIndex = 0;
float maxSeparation = -Float.MAX_VALUE;
for (int i = 0; i < count1; i++) {
// Get poly1 normal in frame2.
Rot.mulToOutUnsafe(xfq, n1s[i], n);
Transform.mulToOutUnsafe(xf, v1s[i], v1);
// Find deepest point for normal i.
float si = Float.MAX_VALUE;
for (int j = 0; j < count2; ++j) {
Vec2 v2sj = v2s[j];
float sij = n.x * (v2sj.x - v1.x) + n.y * (v2sj.y - v1.y);
if (sij < si) {
si = sij;
}
}
if (si > maxSeparation) {
maxSeparation = si;
bestIndex = i;
}
}
results.edgeIndex = bestIndex;
results.separation = maxSeparation;
}
Aggregations