FPS and SpiderMonkey

I’ve opened this thread asking all of you if anyone is developing some kind of fps integrated with Spidermonkey client/server connections and want to share with others his own ideas.

So, I start: Is it possible to set up 2 clients and 1 server [which is hosting the main thread] and exchange messages from/to all of them, like player’s and enemy’s positions? Did anyone try it?



c.

Haven’t really tried it yet myself, but sounds similar to the chat engine I’m currently working on for my project. Just different types of message.



Would this Server be stand alone? Cause it might also be part of one of the clients, so the players can host their own server easy.

Yes, as you suggest there’s no need to have a seperated server hosting. It would be easier to have one client hosting the match, indeed. Could you give me some information or link me additional tutorials to set up a client/client[host] connections for sharing iformations like player’s postions, ammo and so on?



thx

r.

What you are asking is possible, however it is not the default out of the box function of spidermonkey. The purpose of the spidermonkey part of jme3 is to give you access to the tcp or udp connections it will create. Doing so allows you to run a server that can handle messages between the client and the server. When you want to add a multiplayer architecture you will have to build on top of the Jmonkey (or any other networking code).



So to put you on the right path for what you are asking, you will want to create 2 new classes which will house a reference to the spider monkey code. In the spider monkey tutorials you will see him always initialize the server and client in the same class. This means that client is hosting the server like you requested. However, as I mentioned in the first paragraph that only gives you access to the connections and not the game information you want to store. So back to the idea of the two new classes, you will create a class for the client and one for the server.

Client PSUEDO CODE

[java]

import spidermonkeyclasses;

public class mygameclient

{

private Client clientconnection; //reference to spidermonkey



/you can approach this a couple different ways, you can work this client class into your main game class, or you can have this class hold your game application class from J monkey. But from here you would have your jmonkey game setup and do stuff. For example if you want this client to become a server you can have a button that says host game and it will launch the mygameserver class/

}

[/java]





Server pseduo code



[java]

import spidermonkeycclasses;



public class mygameserver

{

private Server server; //spidermonkey server reference



private ClientState myclientstate;// this is a new class you will have to write to hold the information about your client



ConcurrentHashMap < int, ClientState> myclientlist; //this is how you would link a client ID and a state together. You will need to write another function which adds the clients to this list upon connection to the server. You can do this by looking at the spider monkey tutorials that deal with messagereceived and clientconnected



/From here you would have functions that support the maintanence of your server including new player connections and disconnects as well as all current players and there states. You will need to send the states of all the players to each connected player so they can graphically add the new connected players to their scene/



}

[/java]

I hope this helps give you a better idea of what needs to be done to get to that point. I recently just got a workable scene that allows multipeople to connect as Oto the robot and run around. When I feel its a little more polished I’ll try to get a video up and answer any questions I can

2 Likes

You would need some way for the clients to know of one another though. I’m working on OGF, which should be able to do just that. (which is why it’s called Open Game Fidner in the first place.)



But you would indeed have to have your game clients act both as SpiderMonkey Clients and as SpiderMonkey Servers.

ractoc said:
You would need some way for the clients to know of one another though. I'm working on OGF, which should be able to do just that. (which is why it's called Open Game Fidner in the first place.)

But you would indeed have to have your game clients act both as SpiderMonkey Clients and as SpiderMonkey Servers.


I'm not exactly sure what your OGF is going to do. You mean like if you ever had played a First Person Shooter Multiplayer like Counter Strike or COD, there is a list of available servers that are hosting games of that type. Are you writing something that if implemented into the games code will post information to your tool and it will list information about anytype of game running at that time?

As far as the client needing to know about other clients, that is true but its something you can handle yourself. The client gets the information about the other connected clients from the server. So for example on the gameserver code you have the clientconnected even setup (or it could be a message received) and then the server broadcasts to all other connected clients the details of the client that just connected. Upon getting these details, the first client connected builds a spot for that new client in the scene.

To do this you need to find the right method of sending information back and forth from your server. I mean there are a few different methods and you need to pick the most suitable one for your game. You can send the information eventbased (this would suit a turn based game) or you can send at certain frequency (for real time and FPS games). You would need to make a new thread on both the client and the server that is responsible for sending the information.

There’s an OGF topic right here on this fprum explaining everything in detail… If you have any questions after reading it, post them there and I’ll try to answer them.



OGF-topic



Mark

Hey Scrubalub, your test case where you have “a workable scene that allows multipeople to connect as Oto the robot and run around” is exactly what I have been trying to get together and struggling with. It also seems to be a common question that people have as to how to get something like that going. Is there any chance you could post the source code for that? I could totally see it becoming the basis for a tutorial on how to use/implement Spidermonkey. Actually, come to think of it, if you can provide the source code, I’d totally write up a tutorial on this explaining each of the parts.

I have no problem helping out with questions. The problem with posting source code is that I have certain things abstracted to fit the criteria of my game so it wouldn’t be clear to most people and I want to keep some source private. However, I can post the necessary classes / ideas you would need to write to implement into your own architecture. I guess what I am getting at is the examples provided from the Jmonkey team are meant to be easy and shoved into 1 class which isn’t good programming practices (but is practical to show how to use the engine).





EDIT: I also didn’t use spidermonkey for this, but the concepts are the same when you are using spidermonkey. I won’t be available till tomorrow to post a video and code snippets. So if you want start lining up some questions thats cool with me.



http://www.youtube.com/watch?v=jwgfg7gRg-U



I downloaded camtasia software it did it all for me within 10 minutes, even uploaded it to youtube. The second player connects about 30 seconds in, I didnt spend any time editing or pampering the video.

1 Like

Yeah, I think the fact that they lump everything into 1 class is partly what’s causing some difficulty for me. I’m pretty new to java and jME3, so I don’t really know what’s good coding practice and have been just assuming they’ve been doing things properly in the examples/tutorials. It tripped me up in the NiftyGUI stuff because for that you have to make a new screen controller class for each screen, etc. I figured it was something similar for SpiderMonkey. Other than that I’m not exactly sure when/how to start loading my actual game in the actual code. I’ve got a decent understanding of the variables/states I want to send, but not sure how to actually implement it in the code.



I.e. clients send position/rotation to server and gets back position/rotation of the other players from the server.



Basically I’m confused with:

what needs to be in its own class?

am I missing a key line of code that’s messing everything up? (this has happened numerous times to me, lol)

when do the connections need to be made? (say I have a NiftyGUI starting up the game)



Also, I thought your test case was basically just the town scene from the HelloTutorials w/ multiple Oto’s running around in it, so I just assumed there wasn’t anything worth keeping private. Sorry if I asked for too much.



EDIT: Also, nice vid! That’s basically what I want to get working, but with SpiderMonkey. (mutliple Oto’s running around on a terrain, not just a scene model).

There is no way that we recommend as there are many ways to implement a software/game. Your questions are mostly java or object oriented programming beginner questions so there should be lots of tutorials and primers about this topic when you search a bit with these terms.

@scrubalub:

I don’t need the source code, but I’m very interested in your thoughts and problems.

What is a good update/time balance? What do you put in the message, to move the character? Sending positions is my biggest problem. Do you send directions? What do you do, if the camera (assuming its a FPS) turn left? Do you send a new direction? Who decides, if a player is hit or not? How can I prevent people from cheating → should the server check inputs?

Do you use a “central” host or do clients communicate with each other?





I think it’s great, that each effect has it’s own class.

Otherwise, like normen said, jME3 would recommend/command you to do it this way.

Not to mention it’s far easier to read a short code, than a code with many different classes!

I don’t need the source code, but I’m very interested in your thoughts and problems.

Me too! Please, write something here. It will be usefull for many people, sure!

Thanks!

r.

I’m going to start this tutorial by speaking about my background. I have a Computer Science degree an I work as a software engineer for a company that builds simulators and trainers for the government. I encounter all different kinds of problems including architecture, networking, graphics (2d and 3d) and do it in multiple programming languages (java, c,c++ and c#). As Normen and I have mentioned before, there are multiple different ways to design your game. It is up to you to do a lot of reading on the type of game you want to develop. Once you have a good idea of what you want your game to be, you are going to think of a structure to get this idea accomplished. For example if you build your entire game out in single player mode first, you are going to run into countless problems if you try to adapt it to a multiplayer game. From the start you need plan for a multiplayer architecture.



The youtube example I posted may look like one of the simple demos posted on the site but its not. Its part of the game framework I have been building on top of jme. Jmonkey is a great engine but if you are building a larger scale game eventually you are going to have to build a layer on top of jme that is your game API. So behind the scenes in that video I had already written a “WorldState” which houses information about the game as its running. From there I built a level loader, so If I need to I can swap all that terrain with 1 function call at runtime of the game. Some other reasons I won’t give my source code is for one, its incomplete. There are tons of commented out lines that might mean something but it took a lot of debugging to get things in place. Also my architecture might not fit your game design so copying and pasting my code would make it harder on you. Its like the old saying “Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.”



DarkPhoenixX: I’ll address your questions first then I will give a more detailed write up for the other guys.



What is a good update/time balance?

It is up to you here. My setup has been running about 41 miliseconds. I may have to reduce this later because that is a pretty high number. Most games are fine running 24-30.

What do you put in the message, to move the character?

[java]

private float[] translation = new float[3];

private float[] modelDirection = new float[3];

private float[] rotation = new float[4];

[/java]

Sending positions is my biggest problem. Do you send directions?

See above.

What do you do, if the camera (assuming its a FPS) turn left?

For movement I am using everything out of the tutorial TestWalkingChar. So the controls you see there, which are Booleans, left right up down, that’s what I am using. Is there a better system? Maybe? I haven’t gotten there yet.

Do you send a new direction?

Not sure what you are asking here.

Who decides, if a player is hit or not?

Right now I just have a Worldstate which is clientside based which adds the players as they connect to physics space. So technically the collision happens on both clients. Depending your game type you may have to handle it another way,

How can I prevent people from cheating –> should the server check inputs?

You should have checks on both sides, but the more popular way is having the server be authoritative. See the link I provided below written by valve software.

Do you use a “central” host or do clients communicate with each other?

This depends on the type of game, read the documentation below. But the general rule of thumb is have a “central” host and the clients talk to the server and the server tells the clients what to do. For the video I posted I was running the server on my machine, I connected to the server first. Waited about 30 seconds for my tester to connect.



Alright, now for the guys who are new to programming concepts. As I have mentioned throughout this, read, read, read as much as you can on the subjects. You will come across conflicting articles sometimes, which is normal because there is NOT a 100% correct way of building your game. It solely depends on your requirements and how your end product needs to look.



The main concept I am using is a client/server setup. These classes are already written for you if you are using spidermonkey. If you decide not to use spidermonkey you will have to write your own Networking piece, which is a lot of extra overhead if you are a beginner. I didn’t use it because 2 months ago it still had some issues and I figured it would be fun to write my own anyways. As I understand it Lars has done a good job fixing it up.

So you are going to need 2 separate main classes. 1 for the server and 1 for the client.

The client class can be done many different ways but for this example I used this approach. Where you connect the client to the server depends on how you want to do it in your game. I just connect the 2 right away which of course means if the server isn’t up the client crashes. But I am far from done with my game 

[java]

public class Main extends SimpleApplication

{

Private Client client;//instance of spidermonkeyclient

//your normal JME3 game functions

@Override

public void simpleInitApp()

{

setupKeys(); //setup some default keys, for an actual game I would abstract this further

buildWorldState(); //World state is what is holding everything relevant to the game. I will elaborate further on this below.

setupChaseCamera();//for an actual game I would abstract this further

createLight();//for an actual game I would abstract this further, probably on per level basis

}

@Override

public void simpleUpdate(float tpf)

{

rootNode.updateLogicalState(tpf);

rootNode.updateGeometricState();

}

}

[/java]

I am using the worldstate as one of the appstates the jmeengine uses. Read up more on that. As I said before it holds important information about the world. Like it manages the physics, It manages the Levels and players that are being added into the world. I can’t post the whole thing here but below is an example of the update function in the world state.

[java]

public class WorldState extends AbstractAppState

{

@Override

public void update(final float tpf)

{

player.update(tpf); //the code behind this is exactly whats taken from the Testwalkingchar example

for (final Player gc : others.values())

{

/* I’ve tried getting the other players connected to move by using walkdirections and I was having a lot of problems so I took the approach of just setting their translation. In that case you need to enqueue it so the scenegraph doesn’t freak out. */

Future fut = getGame().enqueue(new Callable()

{

public Object call() throws Exception

{

gc.updatedistributedPlayer(tpf); //custom code I had to write to mimic the testwalkingchar demo, the difference is the code behind here doesn’t use a walk direction but positions (translations)

return null;

}

});

}

}

[/java]

Warning: My client server architecture is only using TCP right now. This approach works for TCP but maybe not UDP. Please read the links below for more information.

The next part you might be wondering is how does the client send information to the server such as positions etc. For now I just have a separate thread that spawns on the client after everything has loaded up. It runs at 41 milliseconds just like the server (more on that next). It has a reference to the simpleapplication which means it also has references to the worldstate and Client connection. This way you can grab all current positions put them into a message and send it through the client to the connected server.



Server setup

In my setup I made another class that is standalone. This class should hold the server reference from Spidermonkey, Server. From here it uses the events the server reference fires such as message received, client connected and disconnected. These events allow you to keep track of what clients are connected to the server. So I mentioned the client sends its information at 41 milliseconds in the form of a message. The server class needs to handle that type of message and broadcast it to all other connected clients. I also have a separate thread on the server running at the 41 times a second broadcasting updated client stats.



Well I have written a lot and there are a bunch of references here for everyone to read. Hopefully I touched on the points everyone wanted to hear.



Links and references

http://developer.valvesoftware.com/wiki/Source_Multiplayer_Networking

http://www.gamedev.net/community/forums/showfaq.asp?forum_id=15



EPIC - Entity Position Interpolation Code



http://stackoverflow.com/questions/90423/what-kind-of-issues-are-there-in-implementing-realtime-multiplayer-games

5 Likes

Great, thx!

Now I have a idea what I can do and what i cannot! xD

Yeah, I decided to see how things work single player first just to make sure I understood how things are implemented (animations, physics, terrains, etc.) in first place. Good thing I just kept things basic I guess, now I’ll have less code to rewrite! I started rewriting some things in their own classes when I tried to make the testWalkingChar from scratch yesterday (I couldn’t find the file because I was being dumb, and only found it after the fact that I made most of it) after going through some java tutorials and I gotta say I’m really liking it. I can already see how it’s going to be useful in keeping the code clean but also implementing things in a powerful way.



Also thanks a ton for the tutorial on multiplayer networking and how to set it up! Got lots of reading and learning to keep me busy now. I’ll let you know if I have any more questions. :smiley:



Are there any specific concepts or commonly used things in java that you would recommend going over?

(something sort of like calling functions from another class, which I discovered recently)

I’ve read spidermonkey wiki and opened jme3test.networks example, but I didn’t find any real multiplayer game tutorial.

In the mean time, I made my very simple single player app and setup a working TCP/UDP connection for server/client communication.

Now, I would to merge these to build a multiplayer game and setup a stand alone server without graphic and a couple of client.

Which classes I must create?


public class WorldState extends AbstractAppState
, on jME3?
Anyway as I've read from your post, worldstate class is clientside based, so on each client side there is a simpleUpdate loop which controls player's movements, check for collision and add to the screen other player's models. Isn't it?
So the server has only to setup sockets and run a loop wich implements reading various client's state, sending to each client the state of the others players without looking for collision?

Server:
while (gameisrunning==true) {
read from sockets each client state
send to each client the other client state, but not his own.
}

Client:
while (listeningFromServer==true) {
1. read from stdin player movement
2. read from the serverSocket other player's movements
3. update the rootNode
4. check for collision [if client_ 1 shoot to B3 and client_2 is on B3] and eventually remove some characterCollisionShape
5. render to the display the stituation
6. send back to the server only it own state.
7. stay tuned for next news [next cycle]
}

For the Update/time balance. How do you set it up?

I don't want the cake done! The receipe and ingredients suggestions would be enough!
Or if you prefer go fishing, bring me to the lake and teach me to fishing for the first time!

PS: I'm having issues with spidermonkey so as I said before I'm working with a multicasting UDP connection. As you may know the overhead, it will be better to fix spidermonkey and use it because it is simpler?


Thanks!
r.
cardoppler said:
I've read spidermonkey wiki and opened jme3test.networks example, but I didn't find any real multiplayer game tutorial.
In the mean time, I made my very simple single player app and setup a working TCP/UDP connection for server/client communication.
Now, I would to merge these to build a multiplayer game and setup a stand alone server without graphic and a couple of client.
Which classes I must create?

, on jME3?
Anyway as I've read from your post, worldstate class is clientside based, so on each client side there is a simpleUpdate loop which controls player's movements, check for collision and add to the screen other player's models. Isn't it?

Yes for the most part, but remember you have to decide what is acceptable. For example checking for collisions on the client only may not work for the game you are making :)

So the server has only to setup sockets and run a loop wich implements reading various client's state, sending to each client the state of the others players without looking for collision?

For the most part, yes.

Server:
while (gameisrunning==true) {
read from sockets each client state
send to each client the other client state, but not his own.
}

Client:
while (listeningFromServer==true) {
1. read from stdin player movement
2. read from the serverSocket other player's movements
3. update the rootNode
4. check for collision [if client_ 1 shoot to B3 and client_2 is on B3] and eventually remove some characterCollisionShape
5. render to the display the stituation
6. send back to the server only it own state.
7. stay tuned for next news [next cycle]
}

This sounds like a pretty good setup to start with

For the Update/time balance. How do you set it up?
I have a separate thread for the sending and receiving of information for each (client and server). I setup a default time that each iteration of the loop in that thread should take (41 ms, just recently i bumped it down to 18ms) . Meaning I have start time at the beginning of the loop, then I call the "update" method which sends everything. At the end of that loop i grab the post time and subtract the 2. I sleep the thread if it does it quicker than my base (18ms). If there is a better way or more optimal please let me know :)

I don't want the cake done! The receipe and ingredients suggestions would be enough!
Or if you prefer go fishing, bring me to the lake and teach me to fishing for the first time!

PS: I'm having issues with spidermonkey so as I said before I'm working with a multicasting UDP connection. As you may know the overhead, it will be better to fix spidermonkey and use it because it is simpler?
I don't recommend adding code to the spidermonkey API. I suggest you contact Lars (creator of spidermonkey) and get any errors hashed out so other users don't run into the same problem. If you want to write your own networking piece like I did, be prepared to do a lot of reading and get sidetracked from your main goal for a few weeks up to a few months.

Thanks!
r.


I answered the questions in the quotes

Question for scrubalub, Lars and anyone who knows about fps networking:

I suppose the player on client side would have the possibility to play without be interrupted by the receiving/sending of things for update his own world status.

So maybe I should implement 2 thread for each client? One for play [with inputManager and simpleupdate] and the other one for communication with the server[UDP sockets]? These two thread could share informations, possibly the worldstatus?

And for the server-side create a thread for each new connected client?

All of these thread running in parallel way?



Thanks

c.

The network thread and the render thread are already decoupled the same way you mentioned, so there’s no need to create new threads.