本文整理汇总了Java中org.jbox2d.common.MathUtils.sqrt方法的典型用法代码示例。如果您正苦于以下问题:Java MathUtils.sqrt方法的具体用法?Java MathUtils.sqrt怎么用?Java MathUtils.sqrt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jbox2d.common.MathUtils
的用法示例。
在下文中一共展示了MathUtils.sqrt方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: solveSpring
import org.jbox2d.common.MathUtils; //导入方法依赖的package包/类
void solveSpring(final TimeStep step) {
float springStrength = step.inv_dt * m_springStrength;
for (int k = 0; k < m_pairCount; k++) {
final Pair pair = m_pairBuffer[k];
if ((pair.flags & ParticleType.b2_springParticle) != 0) {
int a = pair.indexA;
int b = pair.indexB;
final Vec2 pa = m_positionBuffer.data[a];
final Vec2 pb = m_positionBuffer.data[b];
final float dx = pb.x - pa.x;
final float dy = pb.y - pa.y;
float r0 = pair.distance;
float r1 = MathUtils.sqrt(dx * dx + dy * dy);
if (r1 == 0) r1 = Float.MAX_VALUE;
float strength = springStrength * pair.strength;
final float fx = strength * (r0 - r1) / r1 * dx;
final float fy = strength * (r0 - r1) / r1 * dy;
final Vec2 va = m_velocityBuffer.data[a];
final Vec2 vb = m_velocityBuffer.data[b];
va.x -= fx;
va.y -= fy;
vb.x += fx;
vb.y += fy;
}
}
}
示例2: computeDistanceToOut
import org.jbox2d.common.MathUtils; //导入方法依赖的package包/类
@Override
public float computeDistanceToOut(Transform xf, Vec2 p, int childIndex, Vec2 normalOut) {
final Rot xfq = xf.q;
float centerx = xfq.c * m_p.x - xfq.s * m_p.y + xf.p.x;
float centery = xfq.s * m_p.x + xfq.c * m_p.y + xf.p.y;
float dx = p.x - centerx;
float dy = p.y - centery;
float d1 = MathUtils.sqrt(dx * dx + dy * dy);
normalOut.x = dx * 1 / d1;
normalOut.y = dy * 1 / d1;
return d1 - m_radius;
}
示例3: computeDistanceToOut
import org.jbox2d.common.MathUtils; //导入方法依赖的package包/类
@Override
public float computeDistanceToOut(Transform xf, Vec2 p, int childIndex, Vec2 normalOut) {
float xfqc = xf.q.c;
float xfqs = xf.q.s;
float xfpx = xf.p.x;
float xfpy = xf.p.y;
float v1x = (xfqc * m_vertex1.x - xfqs * m_vertex1.y) + xfpx;
float v1y = (xfqs * m_vertex1.x + xfqc * m_vertex1.y) + xfpy;
float v2x = (xfqc * m_vertex2.x - xfqs * m_vertex2.y) + xfpx;
float v2y = (xfqs * m_vertex2.x + xfqc * m_vertex2.y) + xfpy;
float dx = p.x - v1x;
float dy = p.y - v1y;
float sx = v2x - v1x;
float sy = v2y - v1y;
float ds = dx * sx + dy * sy;
if (ds > 0) {
float s2 = sx * sx + sy * sy;
if (ds > s2) {
dx = p.x - v2x;
dy = p.y - v2y;
} else {
dx -= ds / s2 * sx;
dy -= ds / s2 * sy;
}
}
float d1 = MathUtils.sqrt(dx * dx + dy * dy);
if (d1 > 0) {
normalOut.x = 1 / d1 * dx;
normalOut.y = 1 / d1 * dy;
} else {
normalOut.x = 0;
normalOut.y = 0;
}
return d1;
}
示例4: addContact
import org.jbox2d.common.MathUtils; //导入方法依赖的package包/类
public void addContact(int a, int b) {
assert(a != b);
Vec2 pa = m_positionBuffer.data[a];
Vec2 pb = m_positionBuffer.data[b];
float dx = pb.x - pa.x;
float dy = pb.y - pa.y;
float d2 = dx * dx + dy * dy;
// assert(d2 != 0);
if (d2 < m_squaredDiameter) {
if (m_contactCount >= m_contactCapacity) {
int oldCapacity = m_contactCapacity;
int newCapacity =
m_contactCount != 0 ? 2 * m_contactCount : Settings.minParticleBufferCapacity;
m_contactBuffer =
BufferUtils.reallocateBuffer(ParticleContact.class, m_contactBuffer, oldCapacity,
newCapacity);
m_contactCapacity = newCapacity;
}
float invD = d2 != 0 ? MathUtils.sqrt(1 / d2) : Float.MAX_VALUE;
ParticleContact contact = m_contactBuffer[m_contactCount];
contact.indexA = a;
contact.indexB = b;
contact.flags = m_flagsBuffer.data[a] | m_flagsBuffer.data[b];
contact.weight = 1 - d2 * invD * m_inverseDiameter;
contact.normal.x = invD * dx;
contact.normal.y = invD * dy;
m_contactCount++;
}
}
示例5: raycast
import org.jbox2d.common.MathUtils; //导入方法依赖的package包/类
/**
* @see Shape#raycast(org.jbox2d.collision.RayCastOutput,
* org.jbox2d.collision.RayCastInput, org.jbox2d.common.Transform, int)
*/
@Override
public final boolean raycast(RayCastOutput argOutput, RayCastInput argInput, Transform argTransform) {
final Vec2 position = pool1;
final Vec2 s = pool2;
final Vec2 r = pool3;
Mat22.mulToOut(argTransform.R, m_p, position);
position.addLocal(argTransform.position);
s.set(argInput.p1).subLocal(position);
final float b = Vec2.dot(s, s) - m_radius * m_radius;
// Solve quadratic equation.
r.set(argInput.p2).subLocal(argInput.p1);
final float c = Vec2.dot(s, r);
final float rr = Vec2.dot(r, r);
final float sigma = c * c - rr * b;
// Check for negative discriminant and short segment.
if (sigma < 0.0f || rr < Settings.EPSILON) {
return false;
}
// Find the point of intersection of the line with the circle.
float a = -(c + MathUtils.sqrt(sigma));
// Is the intersection point on the segment?
if (0.0f <= a && a <= argInput.maxFraction * rr) {
a /= rr;
argOutput.fraction = a;
argOutput.normal.set(r).mulLocal(a);
argOutput.normal.addLocal(s);
argOutput.normal.normalize();
return true;
}
return false;
}
示例6: raycast
import org.jbox2d.common.MathUtils; //导入方法依赖的package包/类
/**
* @see Shape#raycast(org.jbox2d.structs.collision.RayCastOutput,
* org.jbox2d.structs.collision.RayCastInput, org.jbox2d.common.Transform, int)
*/
@Override
public final boolean raycast(RayCastOutput argOutput, RayCastInput argInput, Transform argTransform) {
final Vec2 position = pool1;
final Vec2 s = pool2;
final Vec2 r = pool3;
Mat22.mulToOut(argTransform.R, m_p, position);
position.addLocal(argTransform.position);
s.set(argInput.p1).subLocal(position);
final float b = Vec2.dot(s, s) - m_radius * m_radius;
// Solve quadratic equation.
r.set(argInput.p2).subLocal(argInput.p1);
final float c = Vec2.dot(s, r);
final float rr = Vec2.dot(r, r);
final float sigma = c * c - rr * b;
// Check for negative discriminant and short segment.
if (sigma < 0.0f || rr < Settings.EPSILON) {
return false;
}
// Find the point of intersection of the line with the circle.
float a = -(c + MathUtils.sqrt(sigma));
// Is the intersection point on the segment?
if (0.0f <= a && a <= argInput.maxFraction * rr) {
a /= rr;
argOutput.fraction = a;
argOutput.normal.set(r).mulLocal(a);
argOutput.normal.addLocal(s);
argOutput.normal.normalize();
return true;
}
return false;
}
示例7: printResults
import org.jbox2d.common.MathUtils; //导入方法依赖的package包/类
public void printResults() {
printf("%-20s%20s%20s%20s\n", "Test Name", format.name + " Avg", "StdDev", "95% Interval");
for (int i = 0; i < numTests; i++) {
double mean = stats[i].getMean() / format.divisor;
double stddev = stats[i].getStandardDeviation() / format.divisor;
double diff = 1.96 * stddev / MathUtils.sqrt(stats[i].getN());
printf("%-20s%20.3f%20.3f (%7.3f,%7.3f)\n", getTestName(i), mean, stddev, mean - diff, mean
+ diff);
}
}
示例8: solve
import org.jbox2d.common.MathUtils; //导入方法依赖的package包/类
public void solve(TimeStep step) {
++m_timestamp;
if (m_count == 0) {
return;
}
m_allParticleFlags = 0;
for (int i = 0; i < m_count; i++) {
m_allParticleFlags |= m_flagsBuffer.data[i];
}
if ((m_allParticleFlags & ParticleType.b2_zombieParticle) != 0) {
solveZombie();
}
if (m_count == 0) {
return;
}
m_allGroupFlags = 0;
for (ParticleGroup group = m_groupList; group != null; group = group.getNext()) {
m_allGroupFlags |= group.m_groupFlags;
}
final float gravityx = step.dt * m_gravityScale * m_world.getGravity().x;
final float gravityy = step.dt * m_gravityScale * m_world.getGravity().y;
float criticalVelocytySquared = getCriticalVelocitySquared(step);
for (int i = 0; i < m_count; i++) {
Vec2 v = m_velocityBuffer.data[i];
v.x += gravityx;
v.y += gravityy;
float v2 = v.x * v.x + v.y * v.y;
if (v2 > criticalVelocytySquared) {
float a = v2 == 0 ? Float.MAX_VALUE : MathUtils.sqrt(criticalVelocytySquared / v2);
v.x *= a;
v.y *= a;
}
}
solveCollision(step);
if ((m_allGroupFlags & ParticleGroupType.b2_rigidParticleGroup) != 0) {
solveRigid(step);
}
if ((m_allParticleFlags & ParticleType.b2_wallParticle) != 0) {
solveWall(step);
}
for (int i = 0; i < m_count; i++) {
Vec2 pos = m_positionBuffer.data[i];
Vec2 vel = m_velocityBuffer.data[i];
pos.x += step.dt * vel.x;
pos.y += step.dt * vel.y;
}
updateBodyContacts();
updateContacts(false);
if ((m_allParticleFlags & ParticleType.b2_viscousParticle) != 0) {
solveViscous(step);
}
if ((m_allParticleFlags & ParticleType.b2_powderParticle) != 0) {
solvePowder(step);
}
if ((m_allParticleFlags & ParticleType.b2_tensileParticle) != 0) {
solveTensile(step);
}
if ((m_allParticleFlags & ParticleType.b2_elasticParticle) != 0) {
solveElastic(step);
}
if ((m_allParticleFlags & ParticleType.b2_springParticle) != 0) {
solveSpring(step);
}
if ((m_allGroupFlags & ParticleGroupType.b2_solidParticleGroup) != 0) {
solveSolid(step);
}
if ((m_allParticleFlags & ParticleType.b2_colorMixingParticle) != 0) {
solveColorMixing(step);
}
solvePressure(step);
solveDamping(step);
}
示例9: raycast
import org.jbox2d.common.MathUtils; //导入方法依赖的package包/类
/**
* @param callback
* @param point1
* @param point2
*/
public void raycast(ParticleRaycastCallback callback, final Vec2 point1, final Vec2 point2) {
if (m_proxyCount == 0) {
return;
}
int firstProxy =
lowerBound(
m_proxyBuffer,
m_proxyCount,
computeTag(m_inverseDiameter * MathUtils.min(point1.x, point2.x) - 1, m_inverseDiameter
* MathUtils.min(point1.y, point2.y) - 1));
int lastProxy =
upperBound(
m_proxyBuffer,
m_proxyCount,
computeTag(m_inverseDiameter * MathUtils.max(point1.x, point2.x) + 1, m_inverseDiameter
* MathUtils.max(point1.y, point2.y) + 1));
float fraction = 1;
// solving the following equation:
// ((1-t)*point1+t*point2-position)^2=diameter^2
// where t is a potential fraction
final float vx = point2.x - point1.x;
final float vy = point2.y - point1.y;
float v2 = vx * vx + vy * vy;
if (v2 == 0) v2 = Float.MAX_VALUE;
for (int proxy = firstProxy; proxy < lastProxy; ++proxy) {
int i = m_proxyBuffer[proxy].index;
final Vec2 posI = m_positionBuffer.data[i];
final float px = point1.x - posI.x;
final float py = point1.y - posI.y;
float pv = px * vx + py * vy;
float p2 = px * px + py * py;
float determinant = pv * pv - v2 * (p2 - m_squaredDiameter);
if (determinant >= 0) {
float sqrtDeterminant = MathUtils.sqrt(determinant);
// find a solution between 0 and fraction
float t = (-pv - sqrtDeterminant) / v2;
if (t > fraction) {
continue;
}
if (t < 0) {
t = (-pv + sqrtDeterminant) / v2;
if (t < 0 || t > fraction) {
continue;
}
}
final Vec2 n = tempVec;
tempVec.x = px + t * vx;
tempVec.y = py + t * vy;
n.normalize();
final Vec2 point = tempVec2;
point.x = point1.x + t * vx;
point.y = point1.y + t * vy;
float f = callback.reportParticle(i, point, n, t);
fraction = MathUtils.min(fraction, f);
if (fraction <= 0) {
break;
}
}
}
}
示例10: mixFriction
import org.jbox2d.common.MathUtils; //导入方法依赖的package包/类
/**
* Friction mixing law. The idea is to allow either fixture to drive the restitution to zero. For
* example, anything slides on ice.
*
* @param friction1
* @param friction2
* @return
*/
public static final float mixFriction(float friction1, float friction2) {
return MathUtils.sqrt(friction1 * friction2);
}