Jump to content

  • Log In with Google      Sign In   
  • Create Account

Elspin

Member Since 19 Sep 2007
Offline Last Active Aug 20 2013 06:37 PM

Posts I've Made

In Topic: Network Interpolation Help

08 March 2013 - 08:37 PM

Here's some C# sample code which I wrote just yesterday for the very simple client-side interpolation that I mentioned above:

 

public class CharacterMover
{
    private Vector3 actualPosition; // where we render

    private Vector3 latestPosition; // last position reported by server
    private float latestPositionTime; // the time we want to arrive at the last position reported by server
    private Vector3 previousPosition; // the previous point we're interpolating from
    bool isMoving = false;
    
    // Must be longer than the period between movement messages, plus a little extra to account
    // for network jitter. 0.3f is ok for MMOs and strategic RPGs, probably too high for RTSs,
    // MOBAs, and action RPGs. Far too high for FPSs which need a more complex system than this.
    public float InterpolationPeriod = 0.3f; 
   
    // Update is called once per frame
    void Update ()
    {
        if (isMoving)
        {
            float t = 1.0f - ((latestPositionTime - Time.NOW) / InterpolationPeriod);
            actualPosition = Vector3.Lerp(previousPosition, latestPosition, t);
        }
        else
        {
            actualPosition = latestPosition;
        }

        // TODO: move sprite or 3D model to actualPosition and render it there
    }
    
    // Call this whenever you get a message from the server notifying this client of the character's new
    // position and it's quite close to the current position
    public void SetPosition(Vector3 newPosition)
    {
        isMoving = true;
        
        // Set this as a future position to arrive at, and interpolate between the
        // current position and future position in Update()
        latestPosition = newPosition;
        latestPositionTime = Time.NOW + InterpolationPeriod;
        previousPosition = actualPosition;
    }
    
    // Call this whenever you get a message from the server notifying this client of the character's new position and
    // it's NOT close to the current position, OR you need to perform an instant correction, OR you just want to stop the
    // character still in a certain spot.
    public void SnapToPosition(Vector3 newPosition)
    {
        isMoving = false;
        // move directly there, do not pass go, do not collect $200
        previousPosition = latestPosition = newPosition;
        latestPositionTime = Time.NOW;
        actualPosition = latestPosition;
    }

}
 

It's not intended to be AAA quality tested code, and there are some bugs unhandled edge-cases in there, but it does a decent enough job of keeping movement of networked characters smooth at fairly low movement speeds and infrequent changes in velocity/direction.

 

Thanks, I'll definitely try this out!


In Topic: Network Interpolation Help

07 March 2013 - 11:57 PM

I wondered this same thing in high school when I was working on the PSP homebrew games. You should look into BSD Sockets --they're native to UNIX/Linux, but also used in Windows (using the Winsock API that comes with the Windows SDK). Every platform I've ever come across supports the socket API.

 

As far as I'm concerned, this is an awesome guide on how socket programming works:

http://beej.us/guide/bgnet/output/print/bgnet_A4.pdf

 

It gives a great introduction to the TCP and UDP protocols, how data is transferred over a network and gives you an idea on how network programming works at the low-level. Lots of error checking is involved, but learning this will make you a networking digital Jedi.

 

There's this other library I've seen in a few projects, notably Cube, called enet. It's built on top of the socket API, so I would look into that as well once you're familiar with Beej's guide above.

 

My OP states pretty clearly multiple times that I already know the transport layer, and ENet is in fact a library I use. The topic is directed towards the interpolation and display of sent data to make things like positions, firing times, animation states, etc look smooth on a client that is receiving a message stating an event has happened a very significant amount of time after the event has happened. 


In Topic: Network Interpolation Help

01 March 2013 - 01:56 PM

Thanks for the responses guys - they do help, but I'm looking more for solid example code. A while ago I tried, but it didn't end up working very well. I've already read a lot of articles on the subject so I understand what I should be doing, but getting it to work well is another matter. EPIC is something I've been looking at for a while as it does an ok job at doing what I want to do, but it's rather cryptic and comes with no documentation other than a few comments.


In Topic: ENet packets not sending properly?

02 July 2011 - 07:02 PM

One thing to try is not to destroy the packet after sending. The ENet library takes ownership of the packet after you call send. The only time you should call destroy is after you are done processing a packet after a RECEIVE event occurs.

Score! That seems to have fixed the problem. I missed the part in the tutorial where it said that ENet only needs you to clean up packets after receive, thanks for informing me.



In Topic: ENet packets not sending properly?

02 July 2011 - 05:03 PM

What's the value you receive versus the value you send? Is it consistent, or random?


It appears to be totally random - though it sometimes stays the same for multiple runs. As of right now I'm sending a unsigned short of 13 and recieving an unsigned short of 30888.

PARTNERS