public static Vector3f safeNormalize(Vector3f v, Vector3f out) { final float EPSILON = 0.000001f; out.set(v); out.normalize(); if (out.length() < EPSILON) { out.set(0, 0, 0); } return out; }
private Texture generateNoiseTexture() { Optional<Texture> texture = Assets.getTexture("engine:ssaoNoise"); if (!texture.isPresent()) { ByteBuffer noiseValues = BufferUtils.createByteBuffer(SSAO_NOISE_SIZE * SSAO_NOISE_SIZE * 4); for (int i = 0; i < SSAO_NOISE_SIZE * SSAO_NOISE_SIZE; ++i) { Vector3f noiseVector = new Vector3f(randomGenerator.nextFloat(-1.0f, 1.0f), randomGenerator.nextFloat(-1.0f, 1.0f), 0.0f); noiseVector.normalize(); noiseValues.put((byte) ((noiseVector.x * 0.5 + 0.5) * 255.0f)); noiseValues.put((byte) ((noiseVector.y * 0.5 + 0.5) * 255.0f)); noiseValues.put((byte) ((noiseVector.z * 0.5 + 0.5) * 255.0f)); noiseValues.put((byte) 0x0); } noiseValues.flip(); return Assets.generateAsset(new ResourceUrn("engine:ssaoNoise"), new TextureData(SSAO_NOISE_SIZE, SSAO_NOISE_SIZE, new ByteBuffer[]{noiseValues}, Texture.WrapMode.REPEAT, Texture.FilterMode.NEAREST), Texture.class); } return texture.get(); } }
public BlockMeshPart rotate(Quat4f rotation) { Vector3f[] newVertices = new Vector3f[vertices.length]; Vector3f[] newNormals = new Vector3f[normals.length]; for (int i = 0; i < newVertices.length; ++i) { newVertices[i] = rotation.rotate(vertices[i], new Vector3f()); newNormals[i] = rotation.rotate(normals[i], new Vector3f()); newNormals[i].normalize(); } return new BlockMeshPart(newVertices, newNormals, texCoords, indices); } }
private Vector3f extractResidualMovement(Vector3f hitNormal, Vector3f direction, float normalMag) { float movementLength = direction.length(); if (movementLength > physics.getEpsilon()) { direction.normalize(); Vector3f reflectDir = Vector3fUtil.reflect(direction, hitNormal, new Vector3f()); reflectDir.normalize(); Vector3f perpendicularDir = Vector3fUtil.getPerpendicularComponent(reflectDir, hitNormal, new Vector3f()); if (normalMag != 0.0f) { Vector3f perpComponent = new Vector3f(perpendicularDir); perpComponent.scale(normalMag * movementLength); direction.set(perpComponent); } } return direction; }
private void createSamplesBuffer() { ssaoSamples = BufferUtils.createFloatBuffer(SSAO_KERNEL_ELEMENTS * 3); for (int i = 0; i < SSAO_KERNEL_ELEMENTS; ++i) { Vector3f vec = new Vector3f(); vec.x = randomGenerator.nextFloat(-1.0f, 1.0f); vec.y = randomGenerator.nextFloat(-1.0f, 1.0f); vec.z = randomGenerator.nextFloat(); vec.normalize(); vec.scale(randomGenerator.nextFloat(0.0f, 1.0f)); float scale = i / (float) SSAO_KERNEL_ELEMENTS; scale = TeraMath.lerp(0.25f, 1.0f, scale * scale); vec.scale(scale); ssaoSamples.put(vec.x); ssaoSamples.put(vec.y); ssaoSamples.put(vec.z); } ssaoSamples.flip(); }
private void checkCollision(final ParticlePool pool, final int offset) { final Vector3f vel = new Vector3f(); final Vector3f halfVelDir = new Vector3f(); final Vector3f curr = new Vector3f(); for (int i = offset; i < pool.livingParticles(); i += PHYSICS_SKIP_NR) { int i3 = i * 3; curr.set(pool.position[i3 + 0], pool.position[i3 + 1], pool.position[i3 + 2]); vel.set(pool.velocity[i3 + 0], pool.velocity[i3 + 1], pool.velocity[i3 + 2]); halfVelDir.scale(0).add(vel).normalize().scale(0.5f); curr.sub(halfVelDir); float dist = (vel.length() + 0.5f) * movingAvgDelta * PHYSICS_SKIP_NR * 1.5f; vel.normalize(); HitResult hitResult = physics.rayTrace(curr, vel, dist, StandardCollisionGroup.WORLD); if (hitResult.isHit()) { pool.energy[i] = 0; } } }
@ReceiveEvent public void onItemImpact(ImpactEvent event, EntityRef entity) { RigidBody rigidBody = physics.getRigidBody(entity); if (rigidBody != null) { Vector3f vImpactNormal = new Vector3f(event.getImpactNormal()); Vector3f vImpactPoint = new Vector3f(event.getImpactPoint()); Vector3f vImpactSpeed = new Vector3f(event.getImpactSpeed()); float speedFactor = vImpactSpeed.length(); vImpactNormal.normalize(); vImpactSpeed.normalize(); float dotImpactNormal = vImpactSpeed.dot(vImpactNormal); Vector3f impactResult = vImpactNormal.mul(dotImpactNormal); impactResult = vImpactSpeed.sub(impactResult.mul(2.0f)); impactResult.normalize(); Vector3f vNewLocationVector = (new Vector3f(impactResult)).mul(event.getTravelDistance()); Vector3f vNewPosition = (new Vector3f(vImpactPoint)).add(vNewLocationVector); Vector3f vNewVelocity = (new Vector3f(impactResult)).mul(speedFactor * COLLISION_DAMPENING_MULTIPLIER); rigidBody.setLocation(vNewPosition); rigidBody.setLinearVelocity(vNewVelocity); rigidBody.setAngularVelocity(vNewVelocity); } }
dir.y = 0; if (dir.lengthSquared() > 0.001f) { dir.normalize(); } else { dir.set(Direction.FORWARD.getVector3f());
@Override public void update(float delta) { EntityRef charEntity = player.getCharacterEntity(); if (charEntity.exists()) { Vector3f cameraPos = player.getViewPosition(); CharacterComponent charComp = charEntity.getComponent(CharacterComponent.class); if (charComp != null) { Vector3f dir = player.getViewDirection(); float maxDist = charComp.interactionRange; FirstPersonHeldItemMountPointComponent heldItemMountPoint = player.getCameraEntity().getComponent(FirstPersonHeldItemMountPointComponent.class); if (heldItemMountPoint != null && heldItemMountPoint.isTracked()) { maxDist = heldItemMountPoint.translate.length() + 0.25f; dir = heldItemMountPoint.translate.normalize(); } if (targetSystem.updateTarget(cameraPos, dir, maxDist)) { EntityRef oldTarget = targetSystem.getPreviousTarget(); EntityRef newTarget = targetSystem.getTarget(); charEntity.send(new PlayerTargetChangedEvent(oldTarget, newTarget)); } } } } }
expectedMove.sub(position); if (expectedMove.lengthSquared() > physics.getEpsilon()) { expectedMove.normalize(); expectedMove.scale(actualDist); position.add(expectedMove); float sqrDist = expectedMove.lengthSquared(); if (sqrDist > physics.getEpsilon()) { expectedMove.normalize(); if (expectedMove.dot(normalizedDir) <= 0.0f) { hit = true;
targetDirection.normalize(); drive.set(targetDirection);
vDirection.normalize();
targetDirection.normalize(); drive.set(targetDirection);