/** * @return The distance from the center to the max node */ public Vector3f getExtents() { Vector3f dimensions = new Vector3f(max); dimensions.sub(min); dimensions.scale(HALVING_FACTOR); return dimensions; }
protected boolean checkDistance(Vector3f soundPosition) { Vector3f distance = new Vector3f(soundPosition); distance.sub(listenerPosition); return distance.lengthSquared() < MAX_DISTANCE_SQUARED; }
private boolean vectorsAreAboutEqual(Vector3f v1, Vector3f v2) { Vector3f delta = new Vector3f(); delta.add(v1); delta.sub(v2); float epsilon = 0.0001f; float deltaSquared = delta.lengthSquared(); return deltaSquared < epsilon; }
private float score(Vector3i chunkPos) { Vector3f vec = chunkPos.toVector3f(); vec.sub(localPlayer.getPosition()); return vec.lengthSquared(); } }
private static float squaredDistanceToCamera(RenderableChunk chunk, Vector3f cameraPosition) { // For performance reasons, to avoid instantiating too many vectors in a frequently called method, // comments are in use instead of appropriately named vectors. Vector3f result = chunk.getPosition().toVector3f(); // chunk position in chunk coordinates result.add(CHUNK_CENTER_OFFSET); // chunk center in chunk coordinates result.x *= ChunkConstants.SIZE_X; // chunk center in world coordinates result.y *= ChunkConstants.SIZE_Y; result.z *= ChunkConstants.SIZE_Z; result.sub(cameraPosition); // camera to chunk vector return result.lengthSquared(); }
@Override public int compare(EntityRef o1, EntityRef o2) { LocationComponent loc1 = o1.getComponent(LocationComponent.class); LocationComponent loc2 = o2.getComponent(LocationComponent.class); if (loc1 == null && loc2 == null) { return 0; } else if (loc1 == null) { return 1; } else if (loc2 == null) { return -1; } loc1.getWorldPosition(temp); temp.sub(origin); float dis1 = temp.lengthSquared(); loc2.getWorldPosition(temp); temp.sub(origin); float dis2 = temp.lengthSquared(); if (dis1 < dis2) { return -1; } else if (dis2 < dis1) { return 1; } else { //dis1 == dis2 return 0; } }
/** * Creates a new AABB with the given center and extents. * * @param center The center of the AABB. * @param extent The extent of the AABB. * @return The created AABB. */ public static AABB createCenterExtent(Vector3f center, Vector3f extent) { Vector3f min = new Vector3f(center); min.sub(extent); Vector3f max = new Vector3f(center); max.add(extent); return new AABB(min, max); }
private boolean isDistanceToLarge(LocationComponent characterLocation, LocationComponent targetLocation, float maxInteractionRange) { float maxInteractionRangeSquared = maxInteractionRange * maxInteractionRange; Vector3f positionDelta = new Vector3f(); positionDelta.add(characterLocation.getWorldPosition()); positionDelta.sub(targetLocation.getWorldPosition()); float interactionRangeSquared = positionDelta.lengthSquared(); // add a small epsilon to have rounding mistakes be in favor of the player: float epsilon = 0.00001f; return interactionRangeSquared > maxInteractionRangeSquared + epsilon; }
public Vector3f getLocalPosition() { Vector3f pos = new Vector3f(objectSpacePos); if (parent != null) { pos.sub(parent.getObjectPosition()); Quat4f inverseParentRot = new Quat4f(); inverseParentRot.inverse(parent.getObjectRotation()); inverseParentRot.rotate(pos, pos); } return pos; }
private void renderBone(EntityRef boneEntity, Vector3f centerPos) { LocationComponent loc = boneEntity.getComponent(LocationComponent.class); if (loc == null) { return; } LocationComponent parentLoc = loc.getParent().getComponent(LocationComponent.class); if (parentLoc != null) { Vector3f worldPosA = loc.getWorldPosition(); worldPosA.sub(centerPos); Vector3f worldPosB = parentLoc.getWorldPosition(); worldPosB.sub(centerPos); glBegin(GL11.GL_LINES); glVertex3f(worldPosA.x, worldPosA.y, worldPosA.z); glVertex3f(worldPosB.x, worldPosB.y, worldPosB.z); glEnd(); for (EntityRef child : loc.getChildren()) { renderBone(child, centerPos); } } } }
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); } }
public static Side getSecondaryPlacementDirection(Vector3f direction, Vector3f normal) { Side surfaceDir = Side.inDirection(normal); Vector3f attachDir = surfaceDir.reverse().getVector3i().toVector3f(); Vector3f rawDirection = new Vector3f(direction); float dot = rawDirection.dot(attachDir); rawDirection.sub(new Vector3f(dot * attachDir.x, dot * attachDir.y, dot * attachDir.z)); return Side.inDirection(rawDirection.x, rawDirection.y, rawDirection.z).reverse(); }
public void setWorldPosition(Vector3f value) { this.position.set(value); LocationComponent parentLoc = parent.getComponent(LocationComponent.class); if (parentLoc != null) { this.position.sub(parentLoc.getWorldPosition()); this.position.scale(1f / parentLoc.getWorldScale()); Quat4f rot = new Quat4f(0, 0, 0, 1); rot.inverse(parentLoc.getWorldRotation()); rot.rotate(this.position, this.position); } }
@ReceiveEvent(components = {PlayerCharacterComponent.class}) public void checkForBiomeChange(MovedEvent event, EntityRef entity) { final Vector3i newPos = new Vector3i(event.getPosition()); final Vector3i oldPos = new Vector3i(new Vector3f(event.getPosition()).sub(event.getDelta())); if (newPos.equals(oldPos)) { return; } final Biome oldBiome = world.getBiome(oldPos); final Biome newBiome = world.getBiome(newPos); if (!oldBiome.equals(newBiome)) { entity.send(new BiomeChangeEvent(oldBiome, newBiome)); } } }
private void followToParent(final CharacterStateEvent state, EntityRef entity) { LocationComponent locationComponent = entity.getComponent(LocationComponent.class); if (!locationComponent.getParent().equals(EntityRef.NULL)) { Vector3f velocity = new Vector3f(locationComponent.getWorldPosition()); velocity.sub(state.getPosition()); state.getVelocity().set(velocity); state.getPosition().set(locationComponent.getWorldPosition()); } }
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 renderOverlay() { if (entityToRegionOutlineMap.isEmpty()) { return; // skip everything if there is nothing to do to avoid possibly costly draw mode changes } glDisable(GL_DEPTH_TEST); glLineWidth(2); Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition(); FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16); FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12); material.setFloat("sunlight", 1.0f, true); material.setFloat("blockLight", 1.0f, true); material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix()); Vector3f worldPos = new Vector3f(); Vector3f worldPositionCameraSpace = new Vector3f(); worldPositionCameraSpace.sub(worldPos, cameraPosition); Matrix4f matrixCameraSpace = new Matrix4f(new Quat4f(0, 0, 0, 1), worldPositionCameraSpace, 1.0f); Matrix4f modelViewMatrix = MatrixUtils.calcModelViewMatrix(worldRenderer.getActiveCamera().getViewMatrix(), matrixCameraSpace); MatrixUtils.matrixToFloatBuffer(modelViewMatrix, tempMatrixBuffer44); material.setMatrix4("worldViewMatrix", tempMatrixBuffer44, true); MatrixUtils.matrixToFloatBuffer(MatrixUtils.calcNormalMatrix(modelViewMatrix), tempMatrixBuffer33); material.setMatrix3("normalMatrix", tempMatrixBuffer33, true); for (RegionOutlineComponent regionOutline: entityToRegionOutlineMap.values()) { material.setFloat3("colorOffset", regionOutline.color.rf(), regionOutline.color.gf(), regionOutline.color.bf(), true); drawRegionOutline(regionOutline); } glEnable(GL_DEPTH_TEST); }
@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); } }