List of usage examples for org.apache.commons.math3.geometry.euclidean.threed Vector3D getX
public double getX()
From source file:org.gearvrf.keyboard.util.Util.java
public static void rotateWithOpenGLLookAt(Vector3D cameraVector, Vector3D parentVector, GVRSceneObject object) { Vector3D globalUpVector = new Vector3D(0, 1, 0); Vector3D lookVector = parentVector.normalize(); Vector3D rightVector = lookVector.crossProduct(globalUpVector); Vector3D upVector = rightVector.crossProduct(lookVector); Vector3D zAxis = cameraVector.subtract(parentVector).normalize(); // Vector3D xAxis = upVector.crossProduct(zAxis).normalize(); Vector3D xAxis = zAxis.crossProduct(upVector).normalize(); Vector3D yAxis = xAxis.crossProduct(zAxis).normalize(); // Vector3D yAxis = xAxis.crossProduct(zAxis).normalize(); zAxis = zAxis.scalarMultiply(-1.f);//from w w w . j a va 2s .c o m float angle = (float) Vector3D.angle(parentVector, cameraVector); angle = (float) Math.toDegrees(angle); object.getTransform().rotateByAxis(angle, (float) xAxis.getX(), (float) xAxis.getY(), (float) xAxis.getZ()); object.getTransform().rotateByAxis(angle, (float) yAxis.getX(), (float) yAxis.getY(), (float) yAxis.getZ()); object.getTransform().rotateByAxis(angle, (float) zAxis.getX(), (float) zAxis.getY(), (float) zAxis.getZ()); }
From source file:org.gearvrf.scene_objects.GVRCylinderSceneObject.java
private void createBody(float bottomRadius, float topRadius, float height, int stackNumber, int sliceNumber) { float difference = bottomRadius - topRadius; float halfHeight = height / 2.0f; for (int stack = 0; stack < stackNumber; stack++) { int initVertexCount = vertexCount; float stackPercentage0 = ((float) (stack) / stackNumber); float stackPercentage1 = ((float) (stack + 1) / stackNumber); float t0 = 1.0f - stackPercentage0; float t1 = 1.0f - stackPercentage1; float y0 = -halfHeight + (stackPercentage0 * height); float y1 = -halfHeight + (stackPercentage1 * height); float nx, ny, nz; for (int slice = 0; slice < sliceNumber; slice++) { float slicePercentage0 = ((float) (slice) / sliceNumber); float slicePercentage1 = ((float) (slice + 1) / sliceNumber); double theta0 = slicePercentage0 * 2.0 * Math.PI; double theta1 = slicePercentage1 * 2.0 * Math.PI; double cosTheta0 = Math.cos(theta0); double sinTheta0 = Math.sin(theta0); double cosTheta1 = Math.cos(theta1); double sinTheta1 = Math.sin(theta1); float radius = (bottomRadius - (difference * stackPercentage0)); float x0 = (float) (radius * cosTheta0); float z0 = (float) (-radius * sinTheta0); float x1 = (float) (radius * cosTheta1); float z1 = (float) (-radius * sinTheta1); radius = (bottomRadius - (difference * stackPercentage1)); float x2 = (float) (radius * cosTheta0); float z2 = (float) (-radius * sinTheta0); float x3 = (float) (radius * cosTheta1); float z3 = (float) (-radius * sinTheta1); float s0 = slicePercentage0; float s1 = slicePercentage1; vertices[vertexCount + 0] = x0; vertices[vertexCount + 1] = y0; vertices[vertexCount + 2] = z0; vertices[vertexCount + 3] = x1; vertices[vertexCount + 4] = y0; vertices[vertexCount + 5] = z1; vertices[vertexCount + 6] = x2; vertices[vertexCount + 7] = y1; vertices[vertexCount + 8] = z2; vertices[vertexCount + 9] = x3; vertices[vertexCount + 10] = y1; vertices[vertexCount + 11] = z3; // calculate normal Vector3D v1 = new Vector3D(x1 - x0, 0, z1 - z0); Vector3D v2 = new Vector3D(x2 - x0, y1 - y0, z2 - z0); Vector3D v3 = v1.crossProduct(v2).normalize(); nx = (float) v3.getX(); ny = (float) v3.getY(); nz = (float) v3.getZ(); normals[vertexCount + 0] = nx; normals[vertexCount + 1] = ny; normals[vertexCount + 2] = nz; normals[vertexCount + 3] = nx; normals[vertexCount + 4] = ny; normals[vertexCount + 5] = nz; normals[vertexCount + 6] = nx; normals[vertexCount + 7] = ny; normals[vertexCount + 8] = nz; normals[vertexCount + 9] = nx; normals[vertexCount + 10] = ny; normals[vertexCount + 11] = nz; texCoords[texCoordCount + 0] = s0; texCoords[texCoordCount + 1] = t0; texCoords[texCoordCount + 2] = s1; texCoords[texCoordCount + 3] = t0; texCoords[texCoordCount + 4] = s0; texCoords[texCoordCount + 5] = t1; texCoords[texCoordCount + 6] = s1; texCoords[texCoordCount + 7] = t1; indices[indexCount + 0] = (char) (triangleCount + 0); // 0 indices[indexCount + 1] = (char) (triangleCount + 1); // 1 indices[indexCount + 2] = (char) (triangleCount + 2); // 2 indices[indexCount + 3] = (char) (triangleCount + 2); // 2 indices[indexCount + 4] = (char) (triangleCount + 1); // 1 indices[indexCount + 5] = (char) (triangleCount + 3); // 3 vertexCount += 12;//from w w w . j a va 2 s .com texCoordCount += 8; indexCount += 6; triangleCount += 4; } for (int i = initVertexCount; i < vertexCount - 12; i += 12) { Vector3D v1 = new Vector3D(normals[i + 3], normals[i + 4], normals[i + 5]); Vector3D v2 = new Vector3D(normals[i + 12], normals[i + 13], normals[i + 14]); Vector3D v3 = v1.add(v2).normalize(); nx = (float) v3.getX(); ny = (float) v3.getY(); nz = (float) v3.getZ(); normals[i + 3] = nx; normals[i + 4] = ny; normals[i + 5] = nz; normals[i + 12] = nx; normals[i + 13] = ny; normals[i + 14] = nz; v1 = new Vector3D(normals[i + 9], normals[i + 10], normals[i + 11]); v2 = new Vector3D(normals[i + 18], normals[i + 19], normals[i + 20]); v3 = v1.add(v2).normalize(); nx = (float) v3.getX(); ny = (float) v3.getY(); nz = (float) v3.getZ(); normals[i + 9] = nx; normals[i + 10] = ny; normals[i + 11] = nz; normals[i + 18] = nx; normals[i + 19] = ny; normals[i + 20] = nz; } int i1 = vertexCount - 12; Vector3D v1 = new Vector3D(normals[i1 + 3], normals[i1 + 4], normals[i1 + 5]); int i2 = initVertexCount; Vector3D v2 = new Vector3D(normals[i2 + 0], normals[i2 + 1], normals[i2 + 2]); Vector3D v3 = v1.add(v2).normalize(); nx = (float) v3.getX(); ny = (float) v3.getY(); nz = (float) v3.getZ(); normals[i1 + 3] = nx; normals[i1 + 4] = ny; normals[i1 + 5] = nz; normals[i2 + 0] = nx; normals[i2 + 1] = ny; normals[i2 + 2] = nz; v1 = new Vector3D(normals[i1 + 9], normals[i1 + 10], normals[i1 + 11]); v2 = new Vector3D(normals[i2 + 6], normals[i2 + 7], normals[i2 + 8]); v3 = v1.add(v2).normalize(); nx = (float) v3.getX(); ny = (float) v3.getY(); nz = (float) v3.getZ(); normals[i1 + 9] = nx; normals[i1 + 10] = ny; normals[i1 + 11] = nz; normals[i2 + 6] = nx; normals[i2 + 7] = ny; normals[i2 + 8] = nz; } }
From source file:org.hakkit.hakkitmc.entity.HakkitEntity.java
@Override public void setVelocity(Vector3D velocity) { //We may want to fire an event here. handle.motionX = velocity.getX(); handle.motionY = velocity.getY();/*from w ww .j a v a 2s .co m*/ handle.motionZ = velocity.getZ(); }
From source file:org.jtrfp.trcl.beh.AutoFiring.java
/** * Adapted from <a href='http://jaran.de/goodbits/2011/07/17/calculating-an-intercept-course-to-a-target-with-constant-direction-and-velocity-in-a-2-dimensional-plane/'>this article.</a> * @param targetPos// w w w . j ava 2 s .co m * @param targetVel * @param attackPos * @param attackSpeed * @return * @since Feb 13, 2014 */ private static Vector3D interceptOf(final Vector3D targetPos, final Vector3D targetVel, final Vector3D attackPos, final double attackSpeed) { final double dX = targetPos.getX() - attackPos.getX(); final double dY = targetPos.getY() - attackPos.getY(); final double dZ = targetPos.getZ() - attackPos.getZ(); final double h1 = targetVel.getX() * targetVel.getX() + targetVel.getY() * targetVel.getY() + targetVel.getZ() * targetVel.getZ() - attackSpeed * attackSpeed; final double h2 = dX * targetVel.getX() + dY * targetVel.getY() + dZ * targetVel.getZ(); double t; if (h1 == 0) t = -(dX * dX + dY * dY + dZ * dZ) / 2 * h2; else { final double minusPHalf = -h2 / h1; final double disc = minusPHalf * minusPHalf - (dX * dX + dY * dY + dZ * dZ) / h1; if (disc < 0) return null; final double root = Math.sqrt(disc); final double t1 = minusPHalf + root; final double t2 = minusPHalf - root; final double tMin = Math.min(t1, t2); final double tMax = Math.max(t1, t2); t = tMin > 0 ? tMin : tMax; if (t < 0) return null; } //end else(calculate full) return new Vector3D(targetPos.getX() + t * targetVel.getX(), targetPos.getY() + t * targetVel.getY(), targetPos.getZ() + t * targetVel.getZ()); }
From source file:org.jtrfp.trcl.beh.AutoLeveling.java
@Override public void _tick(long timeInMillis) { final WorldObject parent = getParent(); final Vector3D oldHeading = parent.getHeading(); final Vector3D oldTop = parent.getTop(); final Vector3D oldCross = oldHeading.crossProduct(oldTop); final Vector3D newHeading = levelingAxis == LevelingAxis.HEADING ? new Vector3D( oldHeading.getX() * retainmentCoeff[0] + levelingVector.getX() * inverseRetainmentCoeff[0], oldHeading.getY() * retainmentCoeff[1] + levelingVector.getY() * inverseRetainmentCoeff[1], oldHeading.getZ() * retainmentCoeff[2] + levelingVector.getZ() * inverseRetainmentCoeff[2]) .normalize() : oldHeading.normalize();/*from ww w .j a v a 2s. c o m*/ final Vector3D newTop = levelingAxis == LevelingAxis.TOP ? new Vector3D( oldTop.getX() * retainmentCoeff[0] + levelingVector.getX() * inverseRetainmentCoeff[0], oldTop.getY() * retainmentCoeff[1] + levelingVector.getY() * inverseRetainmentCoeff[1], oldTop.getZ() * retainmentCoeff[2] + levelingVector.getZ() * inverseRetainmentCoeff[2]).normalize() : oldTop.normalize(); final Vector3D newCross = levelingAxis == LevelingAxis.CROSS ? new Vector3D( oldCross.getX() * retainmentCoeff[0] + levelingVector.getX() * inverseRetainmentCoeff[0], oldCross.getY() * retainmentCoeff[1] + levelingVector.getY() * inverseRetainmentCoeff[1], oldCross.getZ() * retainmentCoeff[2] + levelingVector.getZ() * inverseRetainmentCoeff[2]) .normalize() : oldCross.normalize(); final Rotation topDelta = new Rotation(oldTop, newTop); final Rotation headingDelta = new Rotation(oldHeading, newHeading); final Rotation crossDelta = new Rotation(oldCross, newCross); parent.setHeading(crossDelta.applyTo(headingDelta.applyTo(topDelta.applyTo(oldHeading)))); parent.setTop(crossDelta.applyTo(headingDelta.applyTo(topDelta.applyTo(oldTop)))); }
From source file:org.jtrfp.trcl.beh.CollidesWithTerrain.java
@Override public void _tick(long tickTimeMillis) { if (tickCounter++ % 2 == 0 && !recentlyCollided) return;//from w w w. j a v a2 s . c om recentlyCollided = false; final WorldObject p = getParent(); final TR tr = p.getTr(); final World world = tr.getWorld(); final InterpolatingAltitudeMap aMap; final Mission mission = tr.getGame().getCurrentMission(); try { aMap = mission.getOverworldSystem().getAltitudeMap(); } catch (NullPointerException e) { return; } if (mission.getOverworldSystem().isTunnelMode()) return;//No terrain to collide with while in tunnel mode. if (aMap == null) return; final double[] thisPos = p.getPosition(); final double groundHeightNorm = aMap.heightAt((thisPos[0] / TR.mapSquareSize), (thisPos[2] / TR.mapSquareSize)); final double groundHeight = groundHeightNorm * (world.sizeY / 2); final double ceilingHeight = (1.99 - aMap.heightAt((thisPos[0] / TR.mapSquareSize), (thisPos[2] / TR.mapSquareSize))) * (world.sizeY / 2) + CEILING_Y_NUDGE; final Vector3D groundNormal = (aMap.normalAt((thisPos[0] / TR.mapSquareSize), (thisPos[2] / TR.mapSquareSize))); Vector3D downhillDirectionXZ = new Vector3D(groundNormal.getX(), 0, groundNormal.getZ()); if (downhillDirectionXZ.getNorm() != 0) downhillDirectionXZ = downhillDirectionXZ.normalize(); else downhillDirectionXZ = Vector3D.PLUS_J; final OverworldSystem overworldSystem = tr.getGame().getCurrentMission().getOverworldSystem(); if (overworldSystem == null) return; final boolean terrainMirror = overworldSystem.isChamberMode(); final double thisY = thisPos[1]; boolean groundImpact = thisY < (groundHeight + (autoNudge ? nudgePadding : 0)); final boolean ceilingImpact = (thisY > ceilingHeight && terrainMirror && !ignoreCeiling); final Vector3D ceilingNormal = new Vector3D(groundNormal.getX(), -groundNormal.getY(), groundNormal.getZ()); Vector3D surfaceNormal = groundImpact ? groundNormal : ceilingNormal; final double dot = surfaceNormal.dotProduct(getParent().getHeading()); if (terrainMirror && groundHeightNorm > .97) { groundImpact = true; surfaceNormal = downhillDirectionXZ; } //end if(smushed between floor and ceiling) if (groundLock) { recentlyCollided = true; thisPos[1] = groundHeight; p.notifyPositionChange(); return; } //end if(groundLock) if (tunnelEntryCapable && groundImpact && dot < 0) { final OverworldSystem os = mission.getOverworldSystem(); if (!os.isTunnelMode()) { TunnelEntranceObject teo = mission.getTunnelEntranceObject( new Point((int) (thisPos[0] / TR.mapSquareSize), (int) (thisPos[2] / TR.mapSquareSize))); if (teo != null && !mission.isBossFight()) { mission.enterTunnel(teo.getSourceTunnel()); return; } } //end if(above ground) } //end if(tunnelEntryCapable()) if (groundImpact || ceilingImpact) {// detect collision recentlyCollided = true; double padding = autoNudge ? nudgePadding : 0; padding *= groundImpact ? 1 : -1; thisPos[1] = (groundImpact ? groundHeight : ceilingHeight) + padding; p.notifyPositionChange(); if (dot < 0 || ignoreHeadingForImpact) {//If toward ground, call impact listeners. surfaceNormalVar = surfaceNormal; final Behavior behavior = p.getBehavior(); behavior.probeForBehaviors(sub, SurfaceImpactListener.class); } //end if(pointedTowardGround) } // end if(collision) }
From source file:org.jtrfp.trcl.beh.CubeCollisionBehavior.java
public CubeCollisionBehavior(WorldObject wo) { super();/*ww w . ja v a 2 s . c o m*/ Vector3D max = wo.getModel().getTriangleList().getMaximumVertexDims(); Vector3D min = wo.getModel().getTriangleList().getMinimumVertexDims(); origin = new double[] { (max.getX() + min.getX()) / 2., (max.getY() + min.getY()) / 2., (max.getZ() + min.getZ()) / 2. }; dims = new double[] { max.getX() - min.getX(), max.getY() - min.getY(), max.getZ() - min.getZ() }; }
From source file:org.jtrfp.trcl.beh.HeadingXAlwaysPositiveBehavior.java
@Override public void _tick(long tickTime) { final WorldObject p = getParent(); final Vector3D heading = p.getHeading(); if (heading.getX() < 0) { //Rotate 180 degrees on top axis p.setHeading(new Vector3D(0, heading.getY(), heading.getZ()).normalize()); final Vector3D horiz = p.getHeading().crossProduct(p.getTop()).normalize(); final Vector3D newTop = horiz.crossProduct(p.getHeading()).normalize(); p.setTop(newTop);// w w w . ja va2s.c om } }
From source file:org.jtrfp.trcl.beh.HorizAimAtPlayerBehavior.java
@Override public void _tick(long timeInMillis) { if (chaseTarget != null) { WorldObject thisObject = getParent(); final Player player = thisObject.getTr().getGame().getPlayer(); if (player.getBehavior().probeForBehavior(Cloakable.class).isCloaked()) return; final RotationalMomentumBehavior rmb = thisObject.getBehavior() .probeForBehavior(RotationalMomentumBehavior.class); assert !Vect3D.isAnyEqual(chaseTarget.getPosition(), Double.POSITIVE_INFINITY); assert !Vect3D.isAnyEqual(thisObject.getPosition(), Double.NEGATIVE_INFINITY); TR.twosComplimentSubtract(chaseTarget.getPosition(), thisObject.getPosition(), vectorToTargetVar); assert !Vect3D.isAnyNaN(vectorToTargetVar); assert !Vect3D.isAnyEqual(vectorToTargetVar, Double.POSITIVE_INFINITY); assert !Vect3D.isAnyEqual(vectorToTargetVar, Double.NEGATIVE_INFINITY); vectorToTargetVar[1] = 0;/*from www .j ava2 s . c o m*/ Vect3D.normalize(vectorToTargetVar, vectorToTargetVar); final Vector3D thisHeading = new Vector3D(thisObject.getHeading().getX(), 0, thisObject.getHeading().getZ()).normalize(); Vect3D.subtract(thisHeading.toArray(), vectorToTargetVar, headingVarianceDelta); if (Math.sqrt(headingVarianceDelta[2] * headingVarianceDelta[2] + headingVarianceDelta[0] * headingVarianceDelta[0]) < hysteresis) return; if (!reverse) Vect3D.negate(vectorToTargetVar); Rotation rot = new Rotation(new Vector3D(vectorToTargetVar), thisHeading); final Vector3D deltaVector = rot.applyTo(Vector3D.PLUS_K); if ((deltaVector.getZ() > 0 || deltaVector.getX() < 0) == leftHanded) { rmb.accellerateEquatorialMomentum(-equatorialAccelleration); } else { rmb.accellerateEquatorialMomentum(equatorialAccelleration); } } //end if(target!null) }
From source file:org.jtrfp.trcl.beh.phy.ShiftingObjectBehavior.java
public ShiftingObjectBehavior(int totalShiftPeriodMsec, Vector3D startPos, Vector3D endPos) { seq = new Sequencer(totalShiftPeriodMsec, 2, true); xAnimator = new AttribAnimator(xPos, seq, new double[] { startPos.getX(), endPos.getX() }); yAnimator = new AttribAnimator(yPos, seq, new double[] { startPos.getY(), endPos.getY() }); zAnimator = new AttribAnimator(zPos, seq, new double[] { startPos.getZ(), endPos.getZ() }); }