List of usage examples for org.apache.commons.math3.geometry.euclidean.threed Vector3D getY
public double getY()
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//from ww w . j a v a 2s .com * @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 w ww . j av a2s.com 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 a 2 s.co m*/ 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();//from w w w . ja v a 2 s . co 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);/*ww w . j a v a2 s . co m*/ } }
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() }); }
From source file:org.jtrfp.trcl.beh.ProjectileBehavior.java
@Override public void _tick(long tickTimeMillis) { if (honingTarget != null) { if (honingTarget.get() == null) return; if (honingAdjustmentUpdate++ % 5 == 0) { if (!honingTarget.get().isVisible()) return;// Dead or otherwise. final Vector3D honingVector = new Vector3D(honingTarget.get().getPositionWithOffset()) .subtract(new Vector3D(getParent().getPosition())).normalize(); //Sanity check if (Double.isNaN(honingVector.getX())) return; if (Double.isNaN(honingVector.getY())) return; if (Double.isNaN(honingVector.getZ())) return; getParent().getBehavior().probeForBehavior(AutoLeveling.class).setLevelingVector(honingVector); movesByVelocity.setVelocity(getParent().getHeading().scalarMultiply(speed)); } // end if(updateHoningVector) } // end if(honingTarget) }
From source file:org.jtrfp.trcl.beh.RollLevelingBehavior.java
@Override public void _tick(long tickTimeMillis) { WorldObject p = getParent();//from w w w.j av a 2 s .c o m final double[] initHdng = p.getHeadingArray(); final double[] initTop = p.getTopArray(); //Escape on invalid cases if (initHdng[1] <= -1) return; if (initHdng[1] >= 1) return; if (initTop[1] == 0) return; //Create an imaginary heading/top where heading.y=0 imgHdng[0] = initHdng[0]; imgHdng[1] = 0; imgHdng[2] = initHdng[2]; Vect3D.normalize(imgHdng, imgHdng); //Create a rotation to convert back later after performing the roll adjustment. Rotation rot = new Rotation(new Vector3D(initHdng), new Vector3D(imgHdng)); Vector3D imgTop = rot.applyTo(new Vector3D(initTop)).normalize(); double topY = imgTop.getY(); if (topY == 0) return; //Retainment softener prevents gimbal swing effect when facing up or down. final double retainmentSoftener = Misc.clamp(Math.abs(initHdng[1]), 0, 1); final double softenedRetainment = retainment * (1. - retainmentSoftener) + retainmentSoftener; //Perform the roll adjustment using weighting supplied by softenedRetainer if (topY > 0) {//Rightside up, approach 1. topY = topY * softenedRetainment + 1 * (1. - softenedRetainment); } else {//Upside down, approach -1 topY = topY * softenedRetainment + -1 * (1. - softenedRetainment); } Vector3D newTop = rot.applyInverseTo(new Vector3D(imgTop.getX(), topY, imgTop.getZ()).normalize()); //Apply. This will automatically notify change. No need to change heading as it is intrinsically the same. p.setTop(newTop); }
From source file:org.jtrfp.trcl.Camera.java
/** * @param lookAtVector the lookAtVector to set *///from ww w .j a v a2 s . co m public synchronized void setLookAtVector(Vector3D lookAtVector) { double[] heading = super.getHeadingArray(); heading[0] = lookAtVector.getX(); heading[1] = lookAtVector.getY(); heading[2] = lookAtVector.getZ(); //cameraMatrix=null; }
From source file:org.jtrfp.trcl.Camera.java
/** * @param cameraPosition// w w w . jav a2 s .c om * the cameraPosition to set */ public void setPosition(Vector3D cameraPosition) { this.setPosition(cameraPosition.getX(), cameraPosition.getY(), cameraPosition.getZ()); }