CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/conversions.js
Views: 789
1
const { Vec3 } = require('vec3')
2
const math = require('./math')
3
const euclideanMod = math.euclideanMod
4
const PI = Math.PI
5
const PI_2 = Math.PI * 2
6
const TO_RAD = PI / 180
7
const TO_DEG = 1 / TO_RAD
8
const FROM_NOTCH_BYTE = 360 / 256
9
// From wiki.vg: Velocity is believed to be in units of 1/8000 of a block per server tick (50ms)
10
const FROM_NOTCH_VEL = 1 / 8000
11
12
exports.toRadians = toRadians
13
exports.toDegrees = toDegrees
14
exports.fromNotchianYaw = fromNotchianYaw
15
exports.fromNotchianPitch = fromNotchianPitch
16
exports.fromNotchVelocity = fromNotchVelocity
17
exports.toNotchianYaw = yaw => toDegrees(PI - yaw)
18
exports.toNotchianPitch = pitch => toDegrees(-pitch)
19
exports.fromNotchianYawByte = yaw => fromNotchianYaw(yaw * FROM_NOTCH_BYTE)
20
exports.fromNotchianPitchByte = pitch => fromNotchianPitch(pitch * FROM_NOTCH_BYTE)
21
22
function toRadians (degrees) {
23
return TO_RAD * degrees
24
}
25
26
function toDegrees (radians) {
27
return TO_DEG * radians
28
}
29
30
function fromNotchianYaw (yaw) {
31
return euclideanMod(PI - toRadians(yaw), PI_2)
32
}
33
34
function fromNotchianPitch (pitch) {
35
return euclideanMod(toRadians(-pitch) + PI, PI_2) - PI
36
}
37
38
function fromNotchVelocity (vel) {
39
return new Vec3(vel.x * FROM_NOTCH_VEL, vel.y * FROM_NOTCH_VEL, vel.z * FROM_NOTCH_VEL)
40
}
41
42