Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] trying to make look work, but the convertion is not the right one #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 32 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var timeStarted = Math.floor(new Date() / 1000).toString();
var playersConnected = [];
var uuidToPlayer = {};
var vec3 = require("vec3");
var conv=require("./lib/convertions");


function toFixedPosition(p)
Expand Down Expand Up @@ -195,25 +196,43 @@ server.on('login', function(client) {
sendRelativePositionChange(client,toFixedPosition(position),onGround);
});

client.on('position_look', function(packet) {
var position = new vec3(packet.x,packet.y,packet.z);
var onGround=packet.onGround;
sendRelativePositionChange(client,toFixedPosition(position),onGround);

function writeOthers(packetName,packetFields)
{
getOtherClients().forEach(function (otherClient) {
otherClient.write(packetName, packetFields);
});
}

// float (degrees) --> byte (1/256 "degrees")
client.on('look', function(packet) {
var c={
entityId:client.id,
yaw:Math.floor(packet.yaw*256 /360 ),
pitch:Math.floor(packet.pitch*256 /360 ),
onGround:packet.onGround
};
console.log(packet.yaw);
console.log(packet.pitch);
console.log(c);
writeOthers("entity_look",c);
});


function sendRelativePositionChange(client,newPosition,onGround) {
if (uuidToPlayer[client.uuid].position) {
var diff = newPosition.minus(uuidToPlayer[client.uuid].position);
if(diff.distanceTo(new vec3(0,0,0))!=0)
getOtherClients().forEach(function (otherClient) {
otherClient.write('rel_entity_move', {
entityId: uuidToPlayer[client.uuid].id,
dX: diff.x,
dY: diff.y,
dZ: diff.z,
onGround: onGround
});
if (diff.distanceTo(new vec3(0, 0, 0)) != 0) {

writeOthers('rel_entity_move', {
entityId: client.id,
dX: diff.x,
dY: diff.y,
dZ: diff.z,
onGround: onGround
});
// console.log(diff);
}
}
uuidToPlayer[client.uuid].position = newPosition;
uuidToPlayer[client.uuid].onGround=onGround;
Expand Down
49 changes: 49 additions & 0 deletions lib/convertions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var math = require('./math');
var euclideanMod = math.euclideanMod;
var PI = Math.PI;
var PI_2 = Math.PI * 2;
var TO_RAD = PI / 180;
var TO_DEG = 1 / TO_RAD;
var FROM_NOTCH_BYTE = 360 / 256;
var FROM_NOTCH_VEL = 5 / 32000;

exports.toRadians = toRadians;
exports.toDegrees = toDegrees;
exports.fromNotchianYaw = fromNotchianYaw;
exports.fromNotchianPitch = fromNotchianPitch;

exports.toNotchianYaw = function(yaw) {
return toDegrees(PI - yaw);
};

exports.toNotchianPitch = function(pitch) {
return toDegrees(-pitch);
};

exports.fromNotchianYawByte = function(yaw) {
return fromNotchianYaw(yaw * FROM_NOTCH_BYTE);
};

exports.fromNotchianPitchByte = function(pitch) {
return fromNotchianPitch(pitch * FROM_NOTCH_BYTE);
};

exports.fromNotchVelocity = function(vel) {
return vel.scaled(FROM_NOTCH_VEL);
};

function toRadians(degrees) {
return TO_RAD * degrees;
}

function toDegrees(radians) {
return TO_DEG * radians;
}

function fromNotchianYaw(yaw) {
return euclideanMod(PI - toRadians(yaw), PI_2);
}

function fromNotchianPitch(pitch) {
return euclideanMod(toRadians(-pitch) + PI, PI_2) - PI;
}
12 changes: 12 additions & 0 deletions lib/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.clamp = function clamp(min, x, max) {
return x < min ? min : x > max ? max : x;
};

exports.sign = function sign(n) {
return n > 0 ? 1 : n < 0 ? -1 : 0;
};

exports.euclideanMod = function euclideanMod(numerator, denominator) {
var result = numerator % denominator;
return result < 0 ? result + denominator : result;
};