swift502 / Sketchbook

3D playground built on three.js and cannon.js.
https://jblaha.art/sketchbook/latest
MIT License
1.47k stars 389 forks source link

Multiplayer #18

Closed q98 closed 4 years ago

q98 commented 4 years ago

Have you considered adding multiplayer/ is it on the list for future features?

swift502 commented 4 years ago

Hi! Unfortunately multiplayer isn't on my todo list. In the next release I'll focus on just polishing features that exist already.

I was curious about a "javascript multiplayer framework" of some kind, and some seemingly competent [1] [2] frameworks always pop up, so I'm sure it would be possible to implement multiplayer, I just don't consider it a priority.

Maybe once there is an actual game mode where you count the score and can win the game, it would make sense to make multiplayer, but now there's barely anthing to do. 😄

q98 commented 4 years ago

While there may not be much to do, I certainly could find the fun in running around the test map with some friends lol. Race some cars, who knows

q98 commented 4 years ago

Could you maybe point me in the right direction with where the game logic is held/what should be authoritative? I wouldn't mind tinkering in a separate fork

swift502 commented 4 years ago

Eeh. I'm not sure what is needed to implement multiplayer.

You can probably start in the world's update loop. Most logic gets called from there. Apologies for the lack of comments in code, like I said, I plan to polish everything in the next release.

Then I imagine you'll need to synchronize positions of players player.position: THREE.Vector3 stored in the world.characters array and vehicles vehicle.collision.position: CANNON.Vec3 stored in the world.vehicles array. Similarly player.quaternion and vehicle.collision.quaternion store rotations.

I guess that's the basics? Should be enough to move entities around?

To get more advanced, characters are controlled by states player.charState, which play animations and manipulate characters as they please. Optionally, characters can be driven by behaviours player.behaviour which is AI code triggering character actions. But you probably don't need to worry about behaviours just yet.

That's what I imagine is relevant. I've never actually inspected the specifics. Maybe it'll be a lot more difficult and require a rewrite of a lot of system. I have no idea. But I'll be happy if you could take a look at it and report what you learn. 👍

q98 commented 4 years ago

I'll start and see where I get, thanks for the info so far ;)

q98 commented 4 years ago

Hey @swift502 would you be willing to share the blend files for the project? I noticed the airplane doesn't have an enter animation and I also have a cool idea for a ramp to jump to the other side of the map 😂

swift502 commented 4 years ago

@FinalEra Just a warning, it's all a mess.

Aiplane entering animation is on my todo list, so yeah, it'd be great to have that. 🙂 As for the world, I update the demo blend file pretty often, so it's already different from what's in the public demo. But you can still play around with it I guess.

Current demo is in demo 0.3/world_v3. It's a Blender 2.8 file. If objects have functions (like the collision objects), they'll have custom properties assigned to them in the object tab.

Character model is in characters/boxman.

Here's export settings for static geometry (the environment). And here's for the character.

q98 commented 4 years ago

@swift502 mess or not, I appreciate you being so open lol. I still use blender 2.79b 😭 2.8 scares me lol

q98 commented 4 years ago

@swift502 when I open the blend file for the world in 2.81a, export according to your settings, no spawn points. (No problem, added one with the proper data: spawn type:player, works.) But the player now just falls through the map 😂

yomotsu commented 4 years ago

I'm not sure what is needed to implement multiplayer.

I have done a similar thing before. https://www.youtube.com/watch?v=W1WdMCg8ufY

To implement the above, I prepared a web socket. Then every player sends delta direction, velocity and etc to the server. Then receives and applies to every player object.

I hope this would be helpful.

swift502 commented 4 years ago

@FinalEra You have to export the first 4 collections or so together. Visuals, collisions, real instances and data points. Spawns should be in the data points collection. Collisions should all be in the collisions collection. It should work.

I knew this would be kinda crazy without any proper in-depth description of my workflow.

q98 commented 4 years ago

@swift502 yeah I'm digging it though! It took me a little longer than I'd like to admit to figure that out. Collections feel weird after using < 2.8 for so long 😂

Spawn points being built into the map is a great idea - something that hadn't crossed my mind as being possible 🤔

swift502 commented 4 years ago

@FinalEra Maybe in 2020 it's time to move on to that real-time PBR in 2.8. 🙂 There's some oversights with collections regarding visibility of objects, but overall, being able to name your layers feels very natural and is definitely an improvement over the old system.

Normally games have custom editors to simplify and unify creating game entities. But all you really need to define a game world is to assign data to your objects, which is possible in Blender, so why not make use of it.

q98 commented 4 years ago

@swift502 I totally see where you're coming from. I think you're right. 🤗 have you had a look at Babylon.js? I know you made some changes to cannon, specifically for use with this, and three. converting Sketchbook to babylon would be treacherous, and probably not recommended, but Babylon is awesome :D. I think I may be biased due to knowing more there than with three though.

q98 commented 4 years ago

@swift502 something there like you've done here with sketchbook would be game-changing though ;) you can run the same codebase(almost) on server and client to do some easy authoritative networking. https://doc.babylonjs.com/features/nullengine

swift502 commented 4 years ago

@FinalEra It looks interesting. If I ever had problems with three.js I'll look into it more, but I'm happy with three.js.

And I'm sure I'll be able to figure out networking even with three.js if I ever need it, get inspiration by @yomotsu. 🙂

raoneel commented 4 years ago

@swift502 question on this topic, I'm going down this path of synchronizing and was wondering what to send from the character. My basic approach is to send the character data from one player to another, along with the keyboard inputs (up/down/left/right etc.)

On the first frame, it will synchronize the movement data (position, velocity, etc.), and then for every frame after that, it will just send the keyboard data and replay them on the other clients. I was wondering, what are the list of things I would need to synchronize in order to replicate the physical state of the character? So far I have:

Physics

Character (I'm not sure about these)

So for example, let's say there are two players. Let's say player A is in the middle of a jump, and is holding "up". When player B joins, on the first frame they will copy all of the traits listed above into a new character, which should hopefully create a character in the middle of a jump (maybe not animation wise, but at least physically). And then, you would also know that playerA is holding up, so when the character lands, it will move forward.

I know this isn't perfect but I think it might be a good workaround vs having an authoritative server that is also running Sketchbook. It can also lead to the characters slowly desyncing/drifting if there is lag, or the simulation isn't deterministic. Anyway, my question is, is my list of things to synchronize above correct, or should I add/remove something?

swift502 commented 4 years ago

The list is probably correct, except for acceleration, which is never used, only calculated.

Me and a friend of mine are in the process of remaking Sketchbook into a more polished multiplayer game, and we're planning to take the server route, turning off rendering on the server side, which should be trivial.

But I'm really not the person to ask how to do these things. I know nothing about networking. 😄

raoneel commented 4 years ago

Thanks! I'll give it a shot and report back with results. Are you planning on releasing the multiplayer server open source?

swift502 commented 4 years ago

I guess not. The goal is to have a monetizable io game. Having it open source wouldn't make much sense as far as I can see.

I'd offer you possible collaboration but we seem to be doing ok so far, and you have your Babylon project right?

raoneel commented 4 years ago

I got multiplayer working using the approach I described above. Would be cool to have Sketchbook run as a server component but looks like it's not necessary in my use case haha. Thanks for all of your help with this! You can check out the final product here https://dj3d.io/

swift502 commented 4 years ago

@raoneel That's awesome. I already saw your project and I love it. The face swapping on the character is awesome.

Would be cool to have Sketchbook run as a server component

@soylomass somehow made it work. He really plugged few of my classes into his "engine". I wouldn't consider it Sketchbook anymore, but it's Sketchbook cars running server-side multiplayer. Characters might be more difficult with all the animation and state handling.

img

raoneel commented 4 years ago

@swift502 Thank you!

The car demo looks pretty fun, I could imagine a fun driving/racing game built with that engine. It's definitely a lot easier when you take animations out of the picture :D