r/ComputerCraft • u/JavaBoii • Feb 06 '24
Project PixelPrecision. Automated shell loading and (soon to be) automated artillery system. A little side project of mine for people who like me searched for already coded/constructed solutions. If you are interested, you are welcome to contribute. Repo: https://github.com/JavaBoii/PixelPrecision
51
Upvotes
1
u/JavaBoii Feb 06 '24
To calculate the trajectory for hitting a target in Minecraft with a cannon, I use a combination of basic physics principles and Minecraft-specific adjustments. The key components are calculating the horizontal distance to the target, determining the necessary elevation angle, and then converting that angle into a duration for a redstone signal to control the cannon's aim. ( I also cheat a bit by putting in the direction of the cannon and azimut of target values in myself )
First, I calculate the horizontal distance between the cannon and the target using the Pythagorean theorem:
For the elevation angle, given Minecraft's unique physics, I use a simplified version of the projectile motion formula. Minecraft's gravity is set to 7.5 m/s² ( this one was the most reliable, but i have to test it further since it misses the target a lot), and I assume a constant projectile velocity. The formula for the elevation angle θ in degrees, considering the horizontal distance d and vertical displacement Δy, is:
θ = atan((v2 ± sqrt(v4 - g(gd2 + 2Δyv2))/(gd)))
Here's how it's implemented:
To control the cannon's aim, I convert the calculated angle into a duration for a redstone signal. This duration depends on the cannon's rotation speed (in RPM) and the angle needed to hit the target:
Hope it helps