本文整理汇总了Java中com.jme3.math.FastMath.sqrt方法的典型用法代码示例。如果您正苦于以下问题:Java FastMath.sqrt方法的具体用法?Java FastMath.sqrt怎么用?Java FastMath.sqrt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jme3.math.FastMath
的用法示例。
在下文中一共展示了FastMath.sqrt方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLowestRoot
import com.jme3.math.FastMath; //导入方法依赖的package包/类
private static float getLowestRoot(float a, float b, float c, float maxR) {
float determinant = b * b - 4f * a * c;
if (determinant < 0){
return Float.NaN;
}
float sqrtd = FastMath.sqrt(determinant);
float r1 = (-b - sqrtd) / (2f * a);
float r2 = (-b + sqrtd) / (2f * a);
if (r1 > r2){
float temp = r2;
r2 = r1;
r1 = temp;
}
if (r1 > 0 && r1 < maxR){
return r1;
}
if (r2 > 0 && r2 < maxR){
return r2;
}
return Float.NaN;
}
示例2: createHeightSubBlock
import com.jme3.math.FastMath; //导入方法依赖的package包/类
public float[] createHeightSubBlock(float[] heightMap, int x,
int y, int side) {
float[] rVal = new float[side * side];
int bsize = (int) FastMath.sqrt(heightMap.length);
int count = 0;
for (int i = y; i < side + y; i++) {
for (int j = x; j < side + x; j++) {
if (j < bsize && i < bsize)
rVal[count] = heightMap[j + (i * bsize)];
count++;
}
}
return rVal;
}
示例3: addLineFault
import com.jme3.math.FastMath; //导入方法依赖的package包/类
protected void addLineFault(float[][] tempBuffer, Random random, float faultHeight, float range) {
int x1 = random.nextInt(size);
int x2 = random.nextInt(size);
int y1 = random.nextInt(size);
int y2 = random.nextInt(size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
float dist = ((x2 - x1) * (j - y1) - (y2 - y1) * (i - x1))
/ (FastMath.sqrt(FastMath.sqr(x2 - x1) + FastMath.sqr(y2 - y1)));
tempBuffer[i][j] += calcHeight(dist, random, faultHeight, range);
}
}
}
示例4: addCircleFault
import com.jme3.math.FastMath; //导入方法依赖的package包/类
protected void addCircleFault(float[][] tempBuffer, Random random, float faultHeight, float range) {
float radius = random.nextFloat() * (maxRadius - minRadius) + minRadius;
int intRadius = (int) FastMath.floor(radius);
// Allox circle center to be out of map if not by more than radius.
// Unlucky cases will put them in the far corner, with the circle
// entirely outside heightmap
int x = random.nextInt(size + 2 * intRadius) - intRadius;
int y = random.nextInt(size + 2 * intRadius) - intRadius;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
float dist;
if (i != x || j != y) {
int dx = i - x;
int dy = j - y;
float dmag = FastMath.sqrt(FastMath.sqr(dx) + FastMath.sqr(dy));
float rx = x + dx / dmag * radius;
float ry = y + dy / dmag * radius;
dist = FastMath.sign(dmag - radius)
* FastMath.sqrt(FastMath.sqr(i - rx) + FastMath.sqr(j - ry));
} else {
dist = 0;
}
tempBuffer[i][j] += calcHeight(dist, random, faultHeight, range);
}
}
}
示例5: createCollisionHeightfield
import com.jme3.math.FastMath; //导入方法依赖的package包/类
protected void createCollisionHeightfield(float[] heightmap, Vector3f worldScale) {
this.scale = worldScale;
this.heightScale = 1;//don't change away from 1, we use worldScale instead to scale
this.heightfieldData = heightmap;
float min = heightfieldData[0];
float max = heightfieldData[0];
// calculate min and max height
for (int i=0; i<heightfieldData.length; i++) {
if (heightfieldData[i] < min)
min = heightfieldData[i];
if (heightfieldData[i] > max)
max = heightfieldData[i];
}
// we need to center the terrain collision box at 0,0,0 for BulletPhysics. And to do that we need to set the
// min and max height to be equal on either side of the y axis, otherwise it gets shifted and collision is incorrect.
if (max < 0)
max = -min;
else {
if (Math.abs(max) > Math.abs(min))
min = -max;
else
max = -min;
}
this.minHeight = min;
this.maxHeight = max;
this.upAxis = HeightfieldTerrainShape.YAXIS;
this.flipQuadEdges = false;
heightStickWidth = (int) FastMath.sqrt(heightfieldData.length);
heightStickLength = heightStickWidth;
createShape();
}
示例6: distanceToEdge
import com.jme3.math.FastMath; //导入方法依赖的package包/类
public float distanceToEdge(Vector3f point) {
// compute coordinates of point in box coordinate system
Vector3f closest = point.subtract(center);
// project test point onto box
float sqrDistance = 0.0f;
float delta;
if (closest.x < -xExtent) {
delta = closest.x + xExtent;
sqrDistance += delta * delta;
closest.x = -xExtent;
} else if (closest.x > xExtent) {
delta = closest.x - xExtent;
sqrDistance += delta * delta;
closest.x = xExtent;
}
if (closest.y < -yExtent) {
delta = closest.y + yExtent;
sqrDistance += delta * delta;
closest.y = -yExtent;
} else if (closest.y > yExtent) {
delta = closest.y - yExtent;
sqrDistance += delta * delta;
closest.y = yExtent;
}
if (closest.z < -zExtent) {
delta = closest.z + zExtent;
sqrDistance += delta * delta;
closest.z = -zExtent;
} else if (closest.z > zExtent) {
delta = closest.z - zExtent;
sqrDistance += delta * delta;
closest.z = zExtent;
}
return FastMath.sqrt(sqrDistance);
}
示例7: apply
import com.jme3.math.FastMath; //导入方法依赖的package包/类
@Override
public float apply (float a) {
if (a <= 0.5f) {
a *= 2;
return (1 - FastMath.sqrt(1 - a * a)) / 2;
}
a--;
a *= 2;
return (FastMath.sqrt(1 - a * a) + 1) / 2;
}
示例8: update
import com.jme3.math.FastMath; //导入方法依赖的package包/类
@Override
public void update() {
//update the character state based on the needed components
//first collect some necessary information
PhysicsCharacter character = get(PhysicsCharacter.class);
Vector2f move = get(PhysicsCharacterMovement.class).getDirection();
set(new PhysicsCharacterMovementForce(new Vector3f(
move.x*character.getMass()*character.getAcceleration(), 0,
move.y*character.getMass()*character.getAcceleration())));
float capsuleHeight = character.getHeight()-2*character.getRadius()-character.getStepHeight();
float bodyHeight = capsuleHeight+2*character.getRadius();
float jumpSpeed = FastMath.sqrt(2*9.81f*character.getJumpHeight());
boolean jumping = get(PhysicsCharacterState.class).isJumping();
boolean ducking = get(PhysicsCharacterState.class).isDucking();
set(new PhysicsCharacterState(false, false)); //TODO remove
int jumpCount = get(PhysicsCharacterJumpCount.class).getCount();
boolean onGround = false;
Vector3f velocity = get(LinearVelocity.class).getVelocity();
PhysicsPosition pos = get(PhysicsPosition.class);
//the most important part is the ray system which keeps the capsule floating
//in the air over the ground
List<PhysicsRayTestResult> rayTestResultList = bulletSystem.getPhysicsSpace()
.rayTest(pos.getLocation(), pos.getLocation().add(0,-50,0));
if(rayTestResultList.size() > 0){
float len = 60;
for (PhysicsRayTestResult physicsRayTestResult : rayTestResultList) {
if(physicsRayTestResult.getHitFraction()*50 < len){
len = physicsRayTestResult.getHitFraction()*50;
}
}
//if the character is near the ground or below it push it to the min step height.
if(len <= character.getStepHeight() + bodyHeight/2){
float diff = character.getStepHeight() + bodyHeight/2-len;
set(new WarpPosition(pos.getLocation().add(0, diff, 0), pos.getRotation()));
if(velocity.y < 0) {
set(new WarpVelocity(velocity.clone().setY(0), Vector3f.ZERO));
}
onGround = true;
set(new PhysicsCharacterJumpCount(0));
jumpCount = 0;
}
}
Vector3f impulse = new Vector3f();
//apply jump
if(jumping && (onGround || jumpCount < character.getMaxJumpNumber())){
set(new PhysicsCharacterJumpCount(jumpCount+1));
impulse.addLocal(new Vector3f(0, character.getMass()*jumpSpeed, 0));
}
//slow the character down if no movement is applied
if(onGround){
Vector3f slowDownImpulse = new Vector3f();
if(FastMath.approximateEquals(move.x, 0)){
slowDownImpulse.setX(-velocity.x*character.getMass()*0.05f);
}
if(FastMath.approximateEquals(move.y, 0)){
slowDownImpulse.setZ(-velocity.z*character.getMass()*0.05f);
}
impulse.addLocal(slowDownImpulse);
}
//TODO use combined impulse
set(new Impulse(impulse, new Vector3f()));
}
示例9: RawHeightMap
import com.jme3.math.FastMath; //导入方法依赖的package包/类
public RawHeightMap(float heightData[]) {
this.heightData = heightData;
this.size = (int) FastMath.sqrt(heightData.length);
this.format = FORMAT_8BIT;
}
示例10: getPolar
import com.jme3.math.FastMath; //导入方法依赖的package包/类
private float[] getPolar(float vX, float vY) {
return new float[] { FastMath.sqrt(vX * vX + vY * vY), FastMath.atan2(vY, vX) };
}
示例11: terrain2mesh
import com.jme3.math.FastMath; //导入方法依赖的package包/类
public Mesh terrain2mesh(Terrain terr) {
float[] heights = terr.getHeightMap();
int length = heights.length;
int side = (int) FastMath.sqrt(heights.length);
float[] vertices = new float[length * 3];
int[] indices = new int[(side - 1) * (side - 1) * 6];
//Vector3f trans = ((Node) terr).getWorldTranslation().clone();
Vector3f trans = new Vector3f(0,0,0);
trans.x -= terr.getTerrainSize() / 2f;
trans.z -= terr.getTerrainSize() / 2f;
float offsetX = trans.x;
float offsetZ = trans.z;
// do vertices
int i = 0;
for (int z = 0; z < side; z++) {
for (int x = 0; x < side; x++) {
vertices[i++] = x + offsetX;
vertices[i++] = heights[z * side + x];
vertices[i++] = z + offsetZ;
}
}
// do indexes
i = 0;
for (int z = 0; z < side - 1; z++) {
for (int x = 0; x < side - 1; x++) {
// triangle 1
indices[i++] = z * side + x;
indices[i++] = (z + 1) * side + x;
indices[i++] = (z + 1) * side + x + 1;
// triangle 2
indices[i++] = z * side + x;
indices[i++] = (z + 1) * side + x + 1;
indices[i++] = z * side + x + 1;
}
}
Mesh mesh2 = new Mesh();
mesh2.setBuffer(Type.Position, 3, vertices);
mesh2.setBuffer(Type.Index, 3, indices);
mesh2.updateBound();
mesh2.updateCounts();
return mesh2;
}
示例12: setSphere
import com.jme3.math.FastMath; //导入方法依赖的package包/类
/**
* Calculates the minimum bounding sphere of 2 points. Used in welzl's
* algorithm.
*
* @param O
* The 1st point inside the sphere.
* @param A
* The 2nd point inside the sphere.
* @see #calcWelzl(java.nio.FloatBuffer)
*/
private void setSphere(Vector3f O, Vector3f A) {
radius = FastMath.sqrt(((A.x - O.x) * (A.x - O.x) + (A.y - O.y)
* (A.y - O.y) + (A.z - O.z) * (A.z - O.z)) / 4f) + RADIUS_EPSILON - 1f;
center.interpolate(O, A, .5f);
}