Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start adding gravity calculations for Shoot.
- Loading branch information
1 parent
e35188e
commit 651e752
Showing
7 changed files
with
268 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/net/aufdemrand/denizen/utilities/Velocity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package net.aufdemrand.denizen.utilities; | ||
|
||
import org.bukkit.util.Vector; | ||
|
||
public class Velocity { | ||
|
||
/** | ||
* Calculates the vector between two locations' vectors | ||
* | ||
* Thanks to SethBling. | ||
* | ||
* @param from The origin's vector | ||
* @param to The destination's vector | ||
* @param gravity The gravity value of the entity | ||
* @param heightGain The gain in height | ||
* | ||
* @return A vector | ||
*/ | ||
|
||
public static Vector calculate(Vector from, Vector to, double gravity, double heightGain) { | ||
|
||
// Block locations | ||
int endGain = to.getBlockY() - from.getBlockY(); | ||
double horizDist = Math.sqrt(distanceSquared(from, to)); | ||
|
||
double maxGain = heightGain > (endGain + heightGain) ? heightGain : (endGain + heightGain); | ||
|
||
// Solve quadratic equation for velocity | ||
double a = -horizDist * horizDist / (4 * maxGain); | ||
double b = horizDist; | ||
double c = -endGain; | ||
|
||
double slope = -b / (2 * a) - Math.sqrt(b * b - 4 * a * c) / (2 * a); | ||
|
||
// Vertical velocity | ||
double vy = Math.sqrt(maxGain * gravity); | ||
|
||
// Horizontal velocity | ||
double vh = vy / slope; | ||
|
||
// Calculate horizontal direction | ||
int dx = to.getBlockX() - from.getBlockX(); | ||
int dz = to.getBlockZ() - from.getBlockZ(); | ||
double mag = Math.sqrt(dx * dx + dz * dz); | ||
double dirx = dx / mag; | ||
double dirz = dz / mag; | ||
|
||
// Horizontal velocity components | ||
double vx = vh * dirx; | ||
double vz = vh * dirz; | ||
|
||
return new Vector(vx, vy, vz); | ||
} | ||
|
||
private static double distanceSquared(Vector from, Vector to) { | ||
|
||
double dx = to.getBlockX() - from.getBlockX(); | ||
double dz = to.getBlockZ() - from.getBlockZ(); | ||
|
||
return dx * dx + dz * dz; | ||
} | ||
|
||
} |
Oops, something went wrong.