This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Client physics integration #202
Closed
sapphire-arches
wants to merge
15
commits into
ddevault:master
from
sapphire-arches:client_physics_integration
Closed
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2754dbb
Initial implementation, probably doesn't work
sapphire-arches 53b7259
Add project reference for TestClient
sapphire-arches 91135e4
implement copy constructors
sapphire-arches f12d882
Add OffsetBy to BoundingBox
sapphire-arches 5fc96f1
Hook C.N.Client up to C.N.Physics so client code gets some dumb stuff…
sapphire-arches 47370b1
Remove locking velocity for full physics update as it had a tendancy …
sapphire-arches e2151e4
Prevent negative timeouts
sapphire-arches d45dd39
syntax
sapphire-arches 0b92f65
Fix boog in player physics
sapphire-arches 9f3197c
Style
sapphire-arches 558c8b8
More style
sapphire-arches 1084f1c
spleling
sapphire-arches 0cf9c51
make MillisBetweenUpdates static so things work
sapphire-arches 47843bf
stylin
sapphire-arches d06136e
Make MS/Update tuneable
sapphire-arches File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Threading; | ||
using Craft.Net.Common; | ||
using Craft.Net.Physics; | ||
|
||
namespace Craft.Net.Client | ||
{ | ||
public partial class MinecraftClient : IAABBEntity | ||
{ | ||
#region Constants | ||
public float AccelerationDueToGravity | ||
{ | ||
// 32 blocks per second squared / ( ticks per second) = x blocks per tick squared | ||
get { return 0.08f;} | ||
} | ||
|
||
public float Drag | ||
{ | ||
get { return 1 - 0.02f;} | ||
} | ||
|
||
private static Size _size = new Size(0.8, 1.6, 0.8); | ||
|
||
public Size Size | ||
{ | ||
get | ||
{ | ||
return new Size(_size); | ||
} | ||
} | ||
#endregion | ||
|
||
private object _velocityLock = new object(); | ||
private Vector3 _velocity = new Vector3(); | ||
public Vector3 Velocity | ||
{ | ||
get | ||
{ | ||
Vector3 ret; | ||
lock (_velocityLock) | ||
{ | ||
ret = _velocity; | ||
} | ||
return ret; | ||
} | ||
|
||
set | ||
{ | ||
lock (_velocityLock) | ||
{ | ||
_velocity = value; | ||
} | ||
} | ||
} | ||
|
||
private static BoundingBox _boundingBox = new BoundingBox(new Vector3(0), new Vector3(_size.Width, _size.Height, _size.Depth)); | ||
// Make the magic in PhysicsEngine work | ||
private static Vector3 _positionOffset = new Vector3(_size.Width / 2, _size.Height / 2 , _size.Depth / 2); | ||
|
||
public BoundingBox BoundingBox | ||
{ | ||
get | ||
{ | ||
BoundingBox box; | ||
lock (_positionLock) | ||
{ | ||
box = _boundingBox.OffsetBy(_position - _positionOffset); | ||
} | ||
return box; | ||
} | ||
} | ||
|
||
public bool BeginUpdate() | ||
{ | ||
return true; | ||
} | ||
|
||
public void EndUpdate(Vector3 newPosition) | ||
{ | ||
Position = newPosition; | ||
} | ||
|
||
public void TerrainCollision(PhysicsEngine engine, Vector3 collisionPoint, Vector3 collisionDirection) | ||
{ | ||
if (collisionDirection == Vector3.Down) | ||
{ | ||
_onGround = true; | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
using System; | ||
using Craft.Net.Networking; | ||
using Craft.Net.Physics; | ||
using Craft.Net.Logic; | ||
using System.Collections.Concurrent; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
|
@@ -61,12 +63,15 @@ public void Connect(IPEndPoint endPoint) | |
NetworkManager = new NetworkManager(NetworkStream); | ||
NetworkingReset = new ManualResetEvent(true); | ||
NetworkWorkerThread = new Thread(NetworkWorker); | ||
PhysicsWorkerThread = new Thread(PhysicsWorker); | ||
|
||
NetworkWorkerThread.Start(); | ||
var handshake = new HandshakePacket(NetworkManager.ProtocolVersion, | ||
EndPoint.Address.ToString(), (ushort)EndPoint.Port, NetworkMode.Login); | ||
SendPacket(handshake); | ||
var login = new LoginStartPacket(Session.SelectedProfile.Name); | ||
SendPacket(login); | ||
PhysicsWorkerThread.Start(); | ||
} | ||
|
||
public void Disconnect(string reason) | ||
|
@@ -100,6 +105,41 @@ public void SendChat(string message) | |
SendPacket(new ChatMessagePacket(message)); | ||
} | ||
|
||
private DateTime nextPhysicsUpdate = DateTime.MinValue; | ||
private Thread PhysicsWorkerThread; | ||
private PhysicsEngine engine; | ||
private void PhysicsWorker() | ||
{ | ||
while (NetworkWorkerThread.IsAlive) | ||
{ | ||
if (nextPhysicsUpdate < DateTime.Now) | ||
{ | ||
//We need to wait for a login packet to initialize the physics subsystem | ||
if (World != null && engine == null) | ||
{ | ||
engine = new PhysicsEngine(World.World, LogicManager.PhysicsProvider); | ||
engine.AddEntity(this); | ||
} | ||
nextPhysicsUpdate = DateTime.Now.AddMilliseconds(50); | ||
try | ||
{ | ||
engine.Update(); | ||
} catch (Exception) | ||
{ | ||
// Sometimes the world hasn't loaded yet, so the Phyics update can't properly read blocks and | ||
// throws an exception. | ||
} | ||
} else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. Give this another one-over for style. |
||
{ | ||
var sleepTime = (nextPhysicsUpdate - DateTime.Now).Milliseconds; | ||
if (sleepTime > 0) | ||
{ | ||
Thread.Sleep(sleepTime); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private DateTime nextPlayerUpdate = DateTime.MinValue; | ||
private void NetworkWorker() | ||
{ | ||
|
@@ -108,7 +148,20 @@ private void NetworkWorker() | |
if (IsSpawned && nextPlayerUpdate < DateTime.Now) | ||
{ | ||
nextPlayerUpdate = DateTime.Now.AddMilliseconds(100); | ||
SendPacket(new PlayerPacket(OnGround)); | ||
lock (_positionLock) | ||
{ | ||
SendPacket(new PlayerPacket(OnGround)); | ||
if (_positionChanged) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use K&R style braces here. |
||
SendPacket(new PlayerPositionPacket( | ||
Position.X, | ||
Position.Y, | ||
Position.Z, | ||
Position.Y - 1.62, | ||
OnGround | ||
)); | ||
_positionChanged = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indent is off here. |
||
} | ||
} | ||
} | ||
// Send queued packets | ||
while (PacketQueue.Count != 0) | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting error here.