Browse files

The server now only sends updates of players that are not behind you.

  • Loading branch information...
1 parent b36d101 commit c42eea08b6f4136b891fa3479a7637dd9c340060 @Overv committed Dec 31, 2011
Showing with 27 additions and 8 deletions.
  1. +27 −8 js/network.js
View
35 js/network.js
@@ -576,6 +576,13 @@ Server.prototype.onChatMessage = function( socket, data )
//
// Called when a client sends a position/orientation update.
+function normaliseAngle( ang )
+{
+ ang = ang % (Math.PI*2);
+ if ( ang < 0 ) ang = Math.PI*2 + ang;
+ return ang;
+}
+
Server.prototype.onPlayerUpdate = function( socket, data )
{
if ( typeof( data.x ) != "number" || typeof( data.y ) != "number" || typeof( data.z ) != "number" ) return false;
@@ -595,14 +602,26 @@ Server.prototype.onPlayerUpdate = function( socket, data )
pl.yaw = data.yaw;
// Forward update to other players
- socket.volatile.broadcast.emit( "player", {
- nick: name,
- x: pl.x,
- y: pl.y,
- z: pl.z,
- pitch: pl.pitch,
- yaw: pl.yaw
- } );
+ for ( var p in s.world.players ) {
+ var tpl = s.world.players[p];
+ if ( tpl.socket == socket ) continue;
+
+ var ang = Math.PI + Math.atan2( tpl.y - pl.y, tpl.x - pl.x );
+ var nyaw = Math.PI - tpl.yaw - Math.PI/2;
+ var inFrustrum = Math.abs( normaliseAngle( nyaw ) - normaliseAngle( ang ) ) < Math.PI/2;
+
+ if ( inFrustrum )
+ {
+ tpl.socket.volatile.emit( "player", {
+ nick: name,
+ x: pl.x,
+ y: pl.y,
+ z: pl.z,
+ pitch: pl.pitch,
+ yaw: pl.yaw
+ } );
+ }
+ }
}
} );
}

1 comment on commit c42eea0

@WarenGonzaga

I want to make some contribution with this kind of HTML5 game.

Please sign in to comment.