Page 1 of 1

Basic Multiplayer Collision

PostPosted: Sat Nov 15, 2014 12:16 am
by FWadmin
Parallel to my other thread, there's a problem I've been fighting with for a very long time now... something that should be a simple feature, and which I probably should've just asked about, but despite my greatest efforts I can't find any documentation, references, examples or any kind of existing online help about it. It's probably something obvious for most experienced javascript coders or those who have handled the engine for a while, but I just can't figure it out:

How to avoid players from walking trough walls in multiplayer.

I wager it's got something to do with box2d, and example 13.6 perfectly shows both a player and several AI characters walking around without being able to go trough dirt layer tiles, but it's clientsided-only and I can't figure out how to get the same effect in multiplayer. I've successfully integrated the 13.6 tiled map (i think) into multiplayer in such a way that the map is streamed from the server rather than created in the client, and i think I've managed to give the dirt layer there box2d physics, though I can't quite test it without any collision. The player entity is created in ServerNetworkEvents.js, but I can't seem to add box2d-related stuff there... I also can't find a copy of ServerNetworkEvents.js with any mention of "box2d" in it anywhere on the internet. But that's where the player entity is created in the 24.x examples (although unfortunately without physics), so I'm a little confused. All the examples and references I found throughout the documentation, youtube videos and this forum also didn't hint me towards what I'm doing wrong.

All I want is to make wall tiles have a purpose in the 24.x multiplayer examples, I already got players connecting and seeing each other, on the 13.6 map, with chat and everything working... it's just that everyone has noclip

I'm sure there's supposed to be an easy solution to this, as pretty much every multiplayer IGE game I've seen has this feature, but after months of being unable to figure this out I've finally admitted defeat, any help would be greatly appreciated... thanks in advance

Re: Basic Multiplayer Collision

PostPosted: Thu Nov 20, 2014 2:50 am
by cdiddy2
Creating the player within the ServerNetowrkEvents is fine. Its tough to know for sure what you are doing you may have to post some code but ill try.

Ok, so within the player class in the init function add this code. This should create a box2d entity that is dynamic ON THE SERVER. Do not set a texture or anything on the server that goes in the client code.

Code: Select all
       if(ige.box2d){

            var fixDefs = [];

            fixDefs.push({
                density: 0.0,
                friction: 0.5,
                restitution: 0.2,
                shape: {
                    type: 'circle'
                }
            })
                self.box2dBody({
                    type: 'dynamic',
                        linearDamping: 0.0,
                        angularDamping: 0.1,
                        allowSleep: true,
                        bullet: false,
                        gravitic: true,
                        fixedRotation: false,
                        fixtures: fixDefs
                    });
        }


Now this may be invisible for you and will have gravity on just fyi. To test and see that it is working you can try and add
Code: Select all
        this.networkDebugMode = true;
to the init of your client and that will show the box2d fixtures that are being streamed to the client.

Hope this helps and if it doesn't let me know some more data or code.

Re: Basic Multiplayer Collision

PostPosted: Wed Nov 26, 2014 12:19 pm
by robaldred
This is the magic that creates the walls in the 13.6 example
In the callback for loading the map you have:
Code: Select all
ige.box2d.staticsFromMap(layersById.CollisionLayer);

This can be used serverside aswell.
It automatically creates static box2d bodies in the physics world, basic on the data in the map layer you pass to it.
The staticsFromMap method also accepts a 2nd argument that can be used to ignore certain tiles in the layer passed.

In this example I'll show how you can have a ground layer which might have multiple tile types.
1 = Grass
2 = Water

Let assume we don't want the player to walk on water.

Code: Select all
var tileCheck = function(value,x,y) {
   if(value == 2) {
       return false;
   }
   return true;
};

ige.box2d.staticsFromMap(layersById.Ground,tileCheck);


You can do this server side for multiplayer in the same way, make sure if it's a shared class that you wrap any server logic in
Code: Select all
if(ige.isServer) { }


All you need to do is ensure that the layer you pass to the staticsFromMap method is an instance of an IgeTileMap2d or a subclass of it such as IgeTextureMap.

A Texture layer from Tiled is automatically created in the engine as an IgeTextureMap and can be used exactly as in the 13.6 example.

Re: Basic Multiplayer Collision

PostPosted: Sun Dec 07, 2014 7:19 pm
by FWadmin
Thanks guys, I've spent the past several days trying to implement what you suggested, but my player entity still has noclip...

After I finally understood what you were talking about, I managed to implement the code without any errors... but also no results :? I added the suggested code, and nothing happened! Turns out, the StaticsFromMap thing was already there, adding the suggested code just made it more specific, but even so my player entity just ignored it. I added the box2d entity into the player file init function and nothing happened. No errors or anything, no changes, nothing... :shock:

So after days of wrestling with the code, I decided to rebuild the whole thing from the start. Now I've managed to essentially get example 13.7 to work multiplayer, but it had no serverside player entity, just a bunch of AI walking around. I managed to get the player to appear by copying across some code from the previous build within another few days, but it's still got noclip. The AI bump into the walls without walking trough, but the player just goes right trough! So I messed around with StaticsFromMap and discovered that even if I completely removed it from the server.js, the AI still behaved the same, they still won't walk trough walls :? so something else is telling them where not to walk, and I can't for the life of me figure out what so I can desperately try to apply the same code to the player entity.

Eventually I managed to get some interesting results with the player class init box2d thing, I noticed the same code is already in PlayerComponent.js, but when applied to Character.js it stops character entities from walking trough each other, the player can bump into AI's instead, but still walks trough walls. It's something, and it's cool, but it still doesn't solve the issue.

I enabled debugging somehow and can see a purple 3D-box-like outline around both all the AI characters and the player, and using the code suggested by cdiddy2 I got the two to bump into each other (although I'd prefer that not to happen), so I'm pretty sure that all the characters have box2d physics/collision now. But the player still walks trough walls as if StaticsFromMap doesn't create statics that the box2d characters care about... :?

Thanks for your help guys, I really appreciate it and you probably helped me escape a lot of frustration and wild goose chasing, I'm just out of ideas where to go from now...

All I'm trying to achieve is pretty much just a multiplayer version of example 13.6, like a basic multiplayer frame to build on, and I can add everything else afterwards

Re: Basic Multiplayer Collision

PostPosted: Mon Dec 08, 2014 9:49 am
by TheBenji
Can you share your code? I'm not really sure what you did now and why it isn't working.

However in example 13.7 the Ai knows where walls is thanks to this piece of code:

Code: Select all
      if (!this.collisionMap.map._mapData[destTileY] || !tileChecker(this.collisionMap.map._mapData[destTileY][destTileX])) {
         self.pathNextTick = true;
         return;
      }


Basically it just checks manually (this.collisionMap.map._mapData[destTileY][destTileX]) if it can go through.

(I just looked at it shortly, could be wrong...)

Re: Basic Multiplayer Collision

PostPosted: Mon Dec 08, 2014 11:52 am
by robaldred
I'll setup a working version of the example for you this morning.

Re: Basic Multiplayer Collision

PostPosted: Tue Dec 09, 2014 3:57 am
by cdiddy2
personally I have never used staticsFromMap. I just manually place entities around with collision boxes that I define myself. Never had any problems doing it that way. May be worth a shot

Re: Basic Multiplayer Collision

PostPosted: Thu Dec 11, 2014 7:44 pm
by FWadmin
TheBenji wrote:However in example 13.7 the Ai knows where walls is thanks to this piece of code:
Code: Select all
      if (!this.collisionMap.map._mapData[destTileY] || !tileChecker(this.collisionMap.map._mapData[destTileY][destTileX])) {
         self.pathNextTick = true;
         return;
      }

Basically it just checks manually (this.collisionMap.map._mapData[destTileY][destTileX]) if it can go through.

Oh, crud... isn't that part of the AI pathfinder? Would that mean it's impossible to implement the same code to the player entity since it doesn't use a pathfinder? :( The player entity's movement doesn't use any destTileY/destTileX, right..? If so, then... uhm, then what? There must be some other way to make the player entity have similar results, even if it's not trough the same kind of pathfinding :? hmm...

TheBenji wrote:Can you share your code? I'm not really sure what you did now and why it isn't working.

I would repack and upload my game files as examples but there's really not much point, as it's essentially just a mix of the ige examples 13.7 and 13.6 :oops: I found a commented-out character entity creation in example 13.7 but it seems to be client-sided only, so I'm trying to integrate that from 13.6 but I probably broke more than I created...

robaldred wrote:I'll setup a working version of the example for you this morning.

Wow, that would be incredibly kind of you!! :D Something like that should be added to the ige examples as 13.8, as it would surely help a lot of game developers get started on making a multiplayer game

cdiddy2 wrote:personally I have never used staticsFromMap. I just manually place entities around with collision boxes that I define myself. Never had any problems doing it that way. May be worth a shot

Sounds good, though I probably need some more experience with both javascript and ige to know what I'm doing before I'll be able to do that xD