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 boolean moveHorizontal(Vector3f horizMove, CharacterCollider collider, Vector3f position, float slopeFactor, float stepHeight) { float remainingFraction = 1.0f; float dist = horizMove.length(); if (dist < physics.getEpsilon()) { return false; Vector3f modHorizDir = new Vector3f(newDir); modHorizDir.y = 0; newDir.scale(newHorizDir.length() / modHorizDir.length());
/** * Returns a Vector3f with a given size whose components can range from -size (inclusive) to +size (inclusive) * * @param size * @return The vector */ public Vector3f nextVector3f(float size) { // Create a vector whose length is not zero Vector3f vector = new Vector3f(); do { nextVector3f(vector); } while (vector.x == 0.0f && vector.y == 0.0f && vector.z == 0.0f); float length = vector.length(); vector.scale(size / length); return vector; }
float speed = desiredVelocity.length(); desiredVelocity.y = 0; if (desiredVelocity.x != 0 || desiredVelocity.z != 0) { distanceMoved.sub(state.getPosition()); state.getPosition().set(moveResult.getFinalPosition()); if (input.isFirstRun() && distanceMoved.length() > 0) { entity.send(new MovedEvent(distanceMoved, state.getPosition())); if (state.isGrounded() || movementComp.mode == MovementMode.SWIMMING || movementComp.mode == MovementMode.DIVING) { state.setFootstepDelta( state.getFootstepDelta() + distanceMoved.length() / movementComp.distanceBetweenFootsteps); if (state.getFootstepDelta() > 1) { state.setFootstepDelta(state.getFootstepDelta() - 1);
private float distanceToCamera(RenderableChunk chunk) { Vector3f result = new Vector3f((chunk.getPosition().x + 0.5f) * ChunkConstants.SIZE_X, 0, (chunk.getPosition().z + 0.5f) * ChunkConstants.SIZE_Z); Vector3f cameraPos = getActiveCamera().getPosition(); result.x -= cameraPos.x; result.z -= cameraPos.z; return result.length(); }
diff.sub(origin); float distance = diff.length();
@ReceiveEvent(components = {HealthComponent.class}) public void onCrash(HorizontalCollisionEvent event, EntityRef entity) { HealthComponent health = entity.getComponent(HealthComponent.class); Vector3f vel = new Vector3f(event.getVelocity()); vel.y = 0; float speed = vel.length(); if (speed > health.horizontalDamageSpeedThreshold) { int damage = (int) ((speed - health.horizontalDamageSpeedThreshold) * health.excessSpeedDamageMultiplier); if (damage > 0) { checkDamage(entity, damage, EngineDamageTypes.PHYSICAL.get(), EntityRef.NULL, EntityRef.NULL); } } }
private void updateFocalDistance(HitResult hitInfo, float delta) { float focusRate = 4.0f; //how fast the focus distance is updated //if the hit result from a trace has a recorded a hit if (hitInfo.isHit()) { Vector3f playerToTargetRay = new Vector3f(); //calculate the distance from the player to the hit point playerToTargetRay.sub(hitInfo.getHitPoint(), localPlayer.getViewPosition()); //gradually adjust focalDistance from it's current value to the hit point distance focalDistance = TeraMath.lerp(focalDistance, playerToTargetRay.length(), delta * focusRate); //if nothing was hit, gradually adjust the focusDistance to the maximum length of the update function trace } else { focalDistance = TeraMath.lerp(focalDistance, targetDistance, delta * focusRate); } }
@ReceiveEvent public void onCrash(HorizontalCollisionEvent event, EntityRef entity, CharacterSoundComponent characterSounds, HealthComponent healthComponent) { Vector3f horizVelocity = new Vector3f(event.getVelocity()); horizVelocity.y = 0; float velocity = horizVelocity.length(); if (velocity > healthComponent.horizontalDamageSpeedThreshold) { if (characterSounds.lastSoundTime + CharacterSoundSystem.MIN_TIME < time.getGameTimeInMs()) { StaticSound sound = random.nextItem(characterSounds.landingSounds); if (sound != null) { entity.send(new PlaySoundEvent(sound, characterSounds.landingVolume)); characterSounds.lastSoundTime = time.getGameTimeInMs(); entity.saveComponent(characterSounds); } } } }
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 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; } } }
@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)); } } } } }
@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); } }