ozRocker Posted May 17, 2016 Share Posted May 17, 2016 I've been asking a lot of questions lately. Well here's why: www.punkoffice.com/webiverse I'm working on a virtual world. This runs on mobile devices and its networked, so yeh, you can open multiple browser windows to see for yourself. Its just a start. There's much to be done such as flying, building, asset management, region management, basic physics, etc... I'm aiming to make it a collaborative workspace where people can build stuff together. I have a lot of experience playing Second Life, developing for it and also running the OpenSim server so I've used some of my knowledge from that. For those who care and want the details, here we go: Client I'm using typescript for the first time...and I love it! I can now use Visual Studio for EVERYTHING. I just felt stupid writing a big project in javascript so I decided to grow up and learn typescript. Server I'm using C#. Its one of my fave languages. So quick and easy and portable so I can build in Visual Studio then just copy the file to my Linux server and it works. I was tossing up between nodejs and C#. I've seen the benchmarks and for the bigger stuff C# comes out faster. Networking Just using websockets for this one. No libraries required on client side, just straight javascript. Using Fleck on the server. Communication The client sends keypresses to the server NOT position / rotation information. This is to prevent client from cheating and changing position in browser debugging tools. Keypresses are sent every 50 milli-seconds when the user is holding down the key. It has to be done this way. Its not possible to send just a DOWN and UP key event because network lag will give a variable time difference between DOWN and UP and calculate different movements to the client. This will make "client correction" go crazy and will cause the avatar to snap back every time they release a key. Also, you can't send timestamps with these keypresses 'cos the client can change them and make their character basically teleport. I know 'cos I worked on this problem for weeks. Client prediction / correction The client will "predict" the movement when user presses the keys so the avatar moves instantly. The server will calculate movement using the same formula and send the new position / rotation the client. If the client's current position / rotation is different (due to rounding errors or cheating) it will be corrected to mirror the server. The process is complicated and I had to read a lot of different tutorials on how to do this properly. This is the technique that John Carmack invented when he made Quake for WAN. This technique is never perfect though. You either have speed or accuracy, but you can't have both (which is why in multiplayer games you can still get hit by a bullet even if you managed to quickly hide behind a wall). Logging The server is logging to a MySQL database. I'm logging players entering and leaving, their user agent, their FPS every minute (minimum, median, maximum). I'm also logging the amount of outgoing and incoming data between each player. I really need stats so I can see if this is feasible for mobile users. I remember Second Life really drained the bandwidth. That's ok for a home connection, but not for someone using their mobile phone with a data limit. Authentication All my client code is visible. Sockets can also be connected from anywhere and bad data sent to screw up the server. I need to stop this (or at least make it harder to do). I'm using a session key from PHP then sending that session key through the socket on initial connection to verify that its the same client. That session key is deleted as soon as they've been verified. Physics The only physics so far is simple moving forward and rotating. I'd like to add gravity as well. The only problem is the algorithm must be the same on client and server. If I can't find a library that exists in Javascript and also C# I'll have to write my own. I'm prepared to do that though. However, frame rate has priority especially with mobile devices. I will sacrifice physics to keep a smooth frame rate if I have to. iiceman, satguru, JohnK and 3 others 6 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted May 17, 2016 Share Posted May 17, 2016 Lovely!! Quote Link to comment Share on other sites More sharing options...
jerome Posted May 17, 2016 Share Posted May 17, 2016 im-pres-sive !!! Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 17, 2016 Share Posted May 17, 2016 5 hours ago, ozRocker said: You either have speed or accuracy, but you can't have both Here you have Heisenberg's uncertainty principle so you may not have Oimo physics working on a server but you do have quantum physics.? jerome 1 Quote Link to comment Share on other sites More sharing options...
ozRocker Posted May 18, 2016 Author Share Posted May 18, 2016 Right now I'm going for accuracy so there's lag. What you see are player positions a few seconds ago (or whatever the network lag is). Someone asked about adding weapons. I'd need to add player prediction for that so what you see is where a player will most likely be at that point in time Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 18, 2016 Share Posted May 18, 2016 Out of interest what are you thinking of applying gravity to? People jumping up; leaping forward; flying and falling down? Objects being thrown; knocked off tables and dropped? Quote Link to comment Share on other sites More sharing options...
ozRocker Posted May 18, 2016 Author Share Posted May 18, 2016 12 minutes ago, JohnK said: Out of interest what are you thinking of applying gravity to? People jumping up; leaping forward; flying and falling down? Objects being thrown; knocked off tables and dropped? objects being pushed around and knocked off and players walking on stuff and falling off Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 18, 2016 Share Posted May 18, 2016 OK so relatively simple linear motion as opposed to projectile motion. Though perhaps some rotation during falling. Writing your own 'gravity algorithm' should be quite straight forward. Quote Link to comment Share on other sites More sharing options...
ozRocker Posted May 18, 2016 Author Share Posted May 18, 2016 I'll also need them to react if they hit something, so I need to incorporate bounce Quote Link to comment Share on other sites More sharing options...
iiceman Posted May 18, 2016 Share Posted May 18, 2016 Looks great so far, I didn't see any noticeable lag when trying things out. Good job! Well, with node.js you could probably use the cannon module https://www.npmjs.com/package/cannon to get a server side equivalent to the babylon physics implementation. Writing your own physics seems like a lot of work. Quote Link to comment Share on other sites More sharing options...
ozRocker Posted May 18, 2016 Author Share Posted May 18, 2016 16 minutes ago, iiceman said: Well, with node.js you could probably use the cannon module https://www.npmjs.com/package/cannon to get a server side equivalent to the babylon physics implementation. Writing your own physics seems like a lot of work. Cannon would be good. Unfortunately I'm using C# on the server-side. I might not even have physics. It depends how much it'll drop the FPS, or how much data would be going back and forth between clients and server. I've spent years in Second Life without using any physics whatsoever Quote Link to comment Share on other sites More sharing options...
satguru Posted May 19, 2016 Share Posted May 19, 2016 Nice! Best of luck with this project Quote Link to comment Share on other sites More sharing options...
ozRocker Posted May 19, 2016 Author Share Posted May 19, 2016 5 hours ago, satguru said: Nice! Best of luck with this project Thanx mate! Hopefully I can turn it into something useful Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 19, 2016 Share Posted May 19, 2016 Still thinking about gravity and it is obviously not as easy as I first thought. Some of my thoughts below, just ignore if not useful. Considering a person moving forward and then falling of an edge and for the moment ignoring how the lack of ground below is detected and that at some point (s)he will hit the ground. Key presses for the forward motion are being sent every 50ms at the start of the fall key presses are now ignored and a 'falling key' is sent every 50ms (this is how synchronisation is maintained, I think?). A count of the number of falling keys sent needs to be kept fkc, a time period t, for each falling key is needed (this could match the time period for key sending or could be any number that gives a realistic fall), the acceleration due to gravity g, would be needed (9.8 m/sec squared in real world or again any number that works). Given that the vertical position of the person at the start of the fall is y then the vertical position of the person after fkc falling keys is given by y - 0.5 * g * (fkc * t)^2 OK still a long way to go but hope this may be a useful start. Quote Link to comment Share on other sites More sharing options...
ozRocker Posted May 19, 2016 Author Share Posted May 19, 2016 52 minutes ago, JohnK said: Still thinking about gravity and it is obviously not as easy as I first thought. Some of my thoughts below, just ignore if not useful. Considering a person moving forward and then falling of an edge and for the moment ignoring how the lack of ground below is detected and that at some point (s)he will hit the ground. Key presses for the forward motion are being sent every 50ms at the start of the fall key presses are now ignored and a 'falling key' is sent every 50ms (this is how synchronisation is maintained, I think?). A count of the number of falling keys sent needs to be kept fkc, a time period t, for each falling key is needed (this could match the time period for key sending or could be any number that gives a realistic fall), the acceleration due to gravity g, would be needed (9.8 m/sec squared in real world or again any number that works). Given that the vertical position of the person at the start of the fall is y then the vertical position of the person after fkc falling keys is given by y - 0.5 * g * (fkc * t)^2 OK still a long way to go but hope this may be a useful start. Awesome! I appreciate your help with this mate The server should already know a player is falling since it has records of player and object positions. It needs to calculate this without requiring notification from the client so we can be sure if its correct. If its relying on the client they could tamper with the code or messages and you could have someone floating. However, if I want "client prediction" I need to somehow synchronise the client results with server results. Maybe I can have a running index on both client and server from the time the player starts falling. In this case only the server would be sending its positions to the client with the index to match. Another option is making it easy by bypassing client prediction and synchronising and just waiting on server results, but then the player would see physics taking effect half a second later (or whatever the lag is). Quote Link to comment Share on other sites More sharing options...
JohnK Posted May 19, 2016 Share Posted May 19, 2016 Not entirely sure we are looking at identical pictures, your picture and mine are very close but some details may be different. Could be my lack of understanding or my lack of clarity. As I understand it the client has to send data to the server or there is no project. In your case this is in the form of codes representing key presses and there is then some chance of the 'player' interfering with this code. So I believe the current situation looks like this. every 50 ms on client read key pressed send key code to server use key code to move avatar to new predicted position receive position from server correct position of avatar on server read key code use key code to calculate avatar position send position to client Now when I talked about a 'falling key' I was meaning a local computer generated code number to be interpreted by the server in order to calculate the new position of the avatar. What I was trying to do was to send the same sort of data sequence all the time to the server. So instead of sending an 89, say, for move forward taken from the key press you send a 500 for fall because the avatar is falling. In my mind there is the same chance of the player hacking the fall code as there is of hacking the key code. every 50 ms on client read key pressed if (avatar over space) { if(not falling) { falling = true; startY = avatar.position.y; fkc = 0; } send falling code to server fkc++; avatar.position.y = startY -0.5 * g * (fkc *t)^2; receive position from server correct position of avatar } // need to deal with avatar hits ground falling set to false else { send key code to server use key code to move avatar to new predicted position receive position from server correct position of avatar } on server read key code use key code to calculate avatar position send position to client Please bear in mind that the pseudo-code above is just to indicate my thinking and would need plenty more though to get the logic right. Also bear in mind what I know about multi-player games could be written on a pinhead and still leave room for the fairies. I suppose the point I am trying to make is that sending a code to indicate falling and how the server reacts to that code by carrying out a position calculation and sending it back is really no different than sending a key press code and the server doing and sending back a position calculation except in the level of complexity of the calculation. Also that the scope for cheating must be of the same level. Hope I haven't missed the point entirely. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.