List of usage examples for com.badlogic.gdx.math Vector3 len2
@Override
public float len2()
From source file:br.cefetmg.games.movement.behavior.Chegar.java
public Direcionamento guiar(Pose agente) { Direcionamento output = new Direcionamento(); Vector3 objetivo = new Vector3(this.alvo.getObjetivo()); Vector3 velocidade = objetivo.sub(agente.posicao); if (velocidade.len2() > 3000) { output.velocidade = velocidade.clamp(maxVelocidade, maxVelocidade); } else {/*w w w .jav a 2 s. c o m*/ output.velocidade = velocidade.clamp(0, maxVelocidade / 2); } return output; }
From source file:com.badlogic.gdx.ai.tests.steer.bullet.SteeringBulletEntity.java
License:Apache License
protected void applySteering(SteeringAcceleration<Vector3> steering, float deltaTime) { boolean anyAccelerations = false; // Update position and linear velocity if (!steeringOutput.linear.isZero()) { body.applyCentralForce(steeringOutput.linear.scl(deltaTime)); anyAccelerations = true;//from w w w . ja va2 s .co m } // Update orientation and angular velocity if (isIndependentFacing()) { if (steeringOutput.angular != 0) { body.applyTorque(tmpVector3.set(0, steeringOutput.angular * deltaTime, 0)); anyAccelerations = true; } } else { // If we haven't got any velocity, then we can do nothing. Vector3 linVel = getLinearVelocity(); if (!linVel.isZero(MathUtils.FLOAT_ROUNDING_ERROR)) { // // TODO: Commented out!!! // Looks like the code below creates troubles in combination with the applyCentralForce above // Maybe we should be more consistent by only applying forces or setting velocities. // // float newOrientation = vectorToAngle(linVel); // Vector3 angVel = body.getAngularVelocity(); // angVel.y = (newOrientation - oldOrientation) % MathUtils.PI2; // if (angVel.y > MathUtils.PI) angVel.y -= MathUtils.PI2; // angVel.y /= deltaTime; // body.setAngularVelocity(angVel); // anyAccelerations = true; // oldOrientation = newOrientation; } } if (anyAccelerations) { body.activate(); // TODO: // Looks like truncating speeds here after applying forces doesn't work as expected. // We should likely cap speeds form inside an InternalTickCallback, see // http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Simulation_Tick_Callbacks // Cap the linear speed Vector3 velocity = body.getLinearVelocity(); float currentSpeedSquare = velocity.len2(); float maxLinearSpeed = getMaxLinearSpeed(); if (currentSpeedSquare > maxLinearSpeed * maxLinearSpeed) { body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float) Math.sqrt(currentSpeedSquare))); } // Cap the angular speed Vector3 angVelocity = body.getAngularVelocity(); if (angVelocity.y > getMaxAngularSpeed()) { angVelocity.y = getMaxAngularSpeed(); body.setAngularVelocity(angVelocity); } } }
From source file:com.lyeeedar.Roguelike3D.Graphics.Lights.PointLight.java
License:Open Source License
public void computeMesh() { Vector3 intensity = new Vector3(getColour().r, getColour().g, getColour().b); float dist = 1; while (intensity.len2() > 0.5f) { intensity.set(getColour().r, getColour().g, getColour().b) .div((attenuation + (attenuation / 5) * dist) * dist); dist++;//from w w w. jav a2 s .co m } dist *= 4; if (area != null) area.dispose(); area = Shapes.genIcosahedronMesh(dist, dist); area.setVertices(Shapes.genIcosahedronVertices(dist, dist)); radius = dist; }
From source file:com.lyeeedar.Roguelike3D.Graphics.Models.Shapes.java
License:Open Source License
public static Mesh insertTangents(Mesh mesh) { VertexAttributes attributes = mesh.getVertexAttributes(); final int vertCount = mesh.getNumVertices(); final int vertexSize = attributes.vertexSize / 4; VertexAttribute[] newAttributes = new VertexAttribute[attributes.size() + 1]; for (int i = 0; i < attributes.size(); i++) { newAttributes[i] = attributes.get(i); }//from ww w . java 2 s. com newAttributes[attributes.size()] = new VertexAttribute(Usage.Generic, 4, "a_tangent"); final int newVertexSize = vertexSize + 4; float[] verts = new float[vertexSize * vertCount]; mesh.getVertices(verts); short[] indices = new short[mesh.getNumIndices()]; mesh.getIndices(indices); float[] newVerts = new float[newVertexSize * vertCount]; int positionOffset = attributes.getOffset(Usage.Position); int normalOffset = attributes.getOffset(Usage.Normal); int textureOffset = attributes.getOffset(Usage.TextureCoordinates); int tangentOffset = 0; for (int i = 0; i < vertCount; i += 3) { int j = 0; for (; j < vertexSize; j++) { newVerts[(i * newVertexSize) + j] = verts[(i * vertexSize) + j]; newVerts[((i + 1) * newVertexSize) + j] = verts[((i + 1) * vertexSize) + j]; newVerts[((i + 2) * newVertexSize) + j] = verts[((i + 2) * vertexSize) + j]; } tangentOffset = j; } for (int i = 0; i < mesh.getNumIndices(); i += 3) { int i1 = indices[i]; int i2 = indices[i + 1]; int i3 = indices[i + 2]; Vector3 v1 = new Vector3(verts[(i1 * vertexSize) + positionOffset], verts[(i1 * vertexSize) + positionOffset] + 1, verts[(i1 * vertexSize) + positionOffset] + 2); Vector3 v2 = new Vector3(verts[(i2 * vertexSize) + positionOffset], verts[(i2 * vertexSize) + positionOffset] + 1, verts[(i2 * vertexSize) + positionOffset] + 2); Vector3 v3 = new Vector3(verts[(i3 * vertexSize) + positionOffset], verts[(i3 * vertexSize) + positionOffset] + 1, verts[(i3 * vertexSize) + positionOffset] + 2); float[] w1 = { verts[(i1 * vertexSize) + textureOffset], verts[(i1 * vertexSize) + textureOffset] + 1 }; float[] w2 = { verts[(i2 * vertexSize) + textureOffset], verts[(i2 * vertexSize) + textureOffset] + 1 }; float[] w3 = { verts[(i3 * vertexSize) + textureOffset], verts[(i3 * vertexSize) + textureOffset] + 1 }; float x1 = v2.x - v1.x; float x2 = v3.x - v1.x; float y1 = v2.y - v1.y; float y2 = v3.y - v1.y; float z1 = v2.z - v1.z; float z2 = v3.z - v1.z; float s1 = w2[0] - w1[0]; float s2 = w3[0] - w1[0]; float t1 = w2[1] - w1[1]; float t2 = w3[1] - w1[1]; float div = s1 * t2 - s2 * t1; float r = div == 0.0f ? 0.0f : 1.0f / div; Vector3 t = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r); Vector3 n = new Vector3(verts[(i1 * vertexSize) + normalOffset], verts[(i1 * vertexSize) + normalOffset] + 1, verts[(i1 * vertexSize) + normalOffset] + 2); //Vector3 tangent = t.cpy().sub(n).mul(n.tmp().dot(t)).nor(); Vector3 tangent = orthoNormalize(n, t); System.out.println(t2); Vector3 tan2 = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r); float handedness = (n.tmp().crs(t).dot(tan2) < 0.0f) ? -1.0f : 1.0f; Vector3 c1 = n.cpy().crs(0.0f, 0.0f, 1.0f); Vector3 c2 = n.cpy().crs(0.0f, 1.0f, 0.0f); if (c1.len2() > c2.len2()) { tangent = c1; } else { tangent = c2; } newVerts[(i1 * newVertexSize) + tangentOffset] = tangent.x; newVerts[(i1 * newVertexSize) + tangentOffset + 1] = tangent.y; newVerts[(i1 * newVertexSize) + tangentOffset + 2] = tangent.z; newVerts[(i1 * newVertexSize) + tangentOffset + 3] = handedness; newVerts[(i2 * newVertexSize) + tangentOffset] = tangent.x; newVerts[(i2 * newVertexSize) + tangentOffset + 1] = tangent.y; newVerts[(i2 * newVertexSize) + tangentOffset + 2] = tangent.z; newVerts[(i2 * newVertexSize) + tangentOffset + 3] = handedness; newVerts[(i3 * newVertexSize) + tangentOffset] = tangent.x; newVerts[(i3 * newVertexSize) + tangentOffset + 1] = tangent.y; newVerts[(i3 * newVertexSize) + tangentOffset + 2] = tangent.z; newVerts[(i3 * newVertexSize) + tangentOffset + 3] = handedness; } Mesh newMesh = new Mesh(true, mesh.getNumVertices(), mesh.getNumIndices(), newAttributes); newMesh.setVertices(newVerts); newMesh.setIndices(indices); return newMesh; }