[Anet] physics movement setup

Posted By: darkinferno

[Anet] physics movement setup - 08/17/09 14:19

ok, i'm trying to set up multiplayer, i dont want to receive the common, "start simple" chat.. i'll learn as i progress, what i want to do here is, no coding [would be helpful tho ^^] but a guideline of steps to take in setting up basic multiplayer movement.... [using physics ^^]...

my thoughts so far is that:

- i initialize the server
- each client joins and a entity is created on the server and given a global pointer that the client can use
- key inputs are fed to the server and the server moves the entity accordingly..

am i off? any pointers or is there a template, demo similar to this somewhere?
Posted By: darkinferno

Re: [Anet] physics movement setup - 08/17/09 23:20

[update].. it seems so far that almost all multiplayer demos i've seen so far with Anet use client side movement, am wondering if there is a small player script i could see that shows the basics of server side movement.. i'm coninuing to experiment though... currently i do have physics but i learnt the hard way, i cant do that client side .. gets buggy if its to interact with other clients... grin
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/18/09 08:27

Quote:
it seems so far that almost all multiplayer demos i've seen so far with Anet use client side movement


Yes, I already told you that I always do it clientside ^^

Why don't you also make it clientside? It's easier to program and isn't that laggy.
Posted By: flits

Re: [Anet] physics movement setup - 08/18/09 08:34

i am using the free anet together white newton and do it al server side

the project isnt that big but until now it works almost perfect
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/18/09 09:16

Can you explain us in a few words how you are doing that? I think this is interesting for alot people (not only us).
Posted By: flits

Re: [Anet] physics movement setup - 08/18/09 10:30

i wil try to explain it as easy as i can but i am not realy a pro in multieplayer

first i create a server + client (whiteout the client it doesnt seem to being able to control the entities)

then startup the client

then when finshed al the loading stuff, send a var id of the client to the server(this maby needs to send some time after each other until it get reset by the server)

after that the server checks the id and creates a model and send only to that client the var back white NULL(stops looping the client voor sending the request)

the model wil need to know wich client has created him
so take there the skill and give him that idd and update his skill

now you will need some controlling of the entitie by just adding him to the physics enigne

then you will need to be able to control its thie a device but that wont be that hard because your anet runs the function on each model

just wait until the model skill id isnt 0 anymore and then check if its the client idd else dont be able to control it

i hope you get the idee it wont be the best solution but it works almost every time
Posted By: darkinferno

Re: [Anet] physics movement setup - 08/18/09 10:39

ok. i got lost a little when reading that... and @dark_samurai: i can do movemment client side but not phyics movement, it gets buggy
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/18/09 11:01

@flits: This could be done much easier. After you connected the clients with the host and did the loading stuff, every client creates his player using enet_ent_create(). With enet_ent_creator() you can check which client created the entity. So their is no need to store a id in the skill. In the function you can do the following:

if(enet_get_clientid() == enet_ent_creator(enet_ent_globpointer(my)))
{ /*you are allowed to control the player*/}

If you do the movement serverside, how do you transfer the control input from the client to the server? And how do you update the positions on the server?

@darkinferno: To start simple try it this way:
Store the control inputs in ONE!!! skill. Use one bit for every key.
send the skill if it has changed
the server makes the according movement and updates the positions and angels using enet_send_pos/ang().

After that is working you will have to solve 2 problems:
1) If you have a high ping, you will notice that the controls of your own player will get slow.
2) Updating the positions and angles the whole time will cause a lot of traffic. You will have to implement death reckoning or something to reduce the traffic.

Best would be to do a mix of clientside and serverside movement. So that the thing you will see clientside is something like a preview of what will happen. But the real collision detection and movement will be done by the server.
Posted By: lostclimate

Re: [Anet] physics movement setup - 08/18/09 12:52

I do everything server side. That fixes things like speed hackers, item hacking etc. My process is like this


  • create an array of connections that contain things like a pointer to an entity, the particular connection id to the client that it refers to, etc..
  • when a client connects i choose an empty array slot, fill it with there information (which i get from a local database that contains player information, such as name, type, health, mp etc.)
  • I then send the info to all clients including the one just created, so that they can update there connection arrays. I also send all previous connected clients information to the new one so it stays up to date.
  • the server than goes through a loop with a variable that repeatedly counts to a specific number than goes back to zero. every time it hits different numbers it does different updates, so lets say it goes to 10, it will go from 0 to 10 thne restart, at 5 it will request key presses, at 10 it will update all connections positions etc. All of the while the server is doing the moving and interacting of all the objects.
  • create an array of connections that contain things like a pointer to an entity, the particular connection id to the client that it refers to, etc..
  • when a client disconnects, the server sends a message to every client that the client with that id has disconnected, and the clients and the server itself all remove that players connection list entry.
  • on a side note while all this is going on the client is also looping through the connections and moving entities to the estimated positions that they should be, this helps eliminate some of the choppyness, and i know samauri and flit know this but inferno, thats called dead-reckoning. If you want to get some decent theory, locoweed a long time ago made a tutorial about mp games with a6.22 its on the aum resource site. It is usesless code wise, but may give you a lot of idead mp theory wise.



That about sums it up. its not too complicated. I do some dead reckoning, and it is not a great idea to have the 1 in 10 frame updates for twitch games, but Im making an orpg and it works fine for that.
Posted By: darkinferno

Re: [Anet] physics movement setup - 08/18/09 14:20

i'm hearing about cheating on client side, bit am wondering, if i'm doing movement clientside, what can a cheater do to get an advantage?

also.. i dont know if dark_samuri would like to do this but it would be cool to see a server-side template or demo similar to the 3d chat program on your website... its easier for me and am sure for everyone else when they have a base to study on wink
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/18/09 14:39

Cheaters normaly manipulate the client application for their cheats. And if they are able to get access to the enet_send_skill() function for example and the health of a player is stored in a skill, they can easily cheat wink
Because of this, such important values should be stored on the server. The client only gets a "copy" of those values. But the value that is used for checking if the client is dead or not must be on the server and the client shouldn't be able to change it directly.

I currently have a lot of work, let's see if I have some time do do something like that in the future. But I really can't promise that.


The best would be to try it like I explained above. Shouldn't be too hard to code wink
Posted By: lostclimate

Re: [Anet] physics movement setup - 08/18/09 15:27

Originally Posted By: darkinferno
i'm hearing about cheating on client side, bit am wondering, if i'm doing movement clientside, what can a cheater do to get an advantage?

also.. i dont know if dark_samuri would like to do this but it would be cool to see a server-side template or demo similar to the 3d chat program on your website... its easier for me and am sure for everyone else when they have a base to study on wink


put it this way, the only things that should be defined on the client (for absolute security, of course not always needed) the only thing that should be made on the client is calls to the server, and possible actions like setting rotation, that dont matter to anyone elses gameplay. for example, lets say you have it so that 60/second your character moves on your machine, one in 6 times your position is updated to everyone else, if it is done on the client, the client could hack your program so that you move 600 times a second so now your character can move 10x as fast as everyone and when it updates to the server, the server doesnt know the difference. of course you could make checks on the server on distances moved, but thats like putting bandage on a gash, better to just be more careful instead.
Posted By: darkinferno

Re: [Anet] physics movement setup - 08/18/09 23:56

ok, pretty lame question but somehow am not seeing it, how do i create an entity oh the server? creating them on clients is ok how do i say, create 2 entities on the server, am getting a little mixed up as to how anet handles server, server+client

[edit] or maybe am just not thinking this through.. maybe been infront of tha pc for too long ^^
Posted By: lostclimate

Re: [Anet] physics movement setup - 08/19/09 01:09

sorry, you'll have to get your answer from samauri, i dont use his entity management system.
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/19/09 09:22

If you call enet_ent_create() it creates the entity everywhere never mind on which host the function is called.

EDIT: Btw. I would advice you to read the Simple 3D chat tutorial and the HowTos. You will have a much smarter start wink
Posted By: darkinferno

Re: [Anet] physics movement setup - 08/19/09 11:52

yes, i have been through that.. i did make some progress on creating physics entities on the server, my next objective will be to attach a unique pointer to them so each client can have control
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/19/09 12:38

Why a unique pointer? The glob. pointer is a unique pointer and can be used to identify the entity. With enet_ent_creator you get the clientid of the creator of the entity. if CreatorID equals myClientID allow controls.

On the server you set up the physicparameters and on the client that controls the entity you fill a skill with the input. Everytime the skill has changed, send it to the server with enet_send_skill(). The server always updates the positions and angles with enet_send_pos/ang.

This would be the entity function:
Code:
function player_function()
{
   if(enet_ent_creator(enet_ent_globpointer(my)) == enet_get_clientid())
   { //Every client controls his own player
      while(1)
      {
         //store the keys as bits in a skill1
         if(my.skill1 != oldskill1) enet_send_skill(enet_ent_globpointer(my),1,1,SERVER);
      }
   }
   if(enet_get_connection() == SERVER_MODE)
   {
      //set up the physic stuff
      while(1)
      {
           //use skill1 for the controls of the entity
           enet_send_pos(enet_ent_globpointer(my),BROADCAST);
           enet_send_ang(enet_ent_globpointer(my),BROADCAST);
      }
   }
}



That's all wink Simple? Yes it is laugh

All you have to notice is that every client must create his player using enet_ent_create().
Posted By: darkinferno

Re: [Anet] physics movement setup - 08/19/09 13:31

i tried the above, well.. i tried this to be exact:

Code:
function move_player()
{
	while(enet_ent_globpointer(my) == -1) {wait(1);} //wait until the entity gets a global pointer
	
   if(enet_ent_creator(enet_ent_globpointer(my)) == enet_get_clientid())
   { //Every client controls his own player
      while(1)
      {
         //store the keys as bits in a skill1
        // if(my.skill1 != oldskill1) enet_send_skill(enet_ent_globpointer(my),1,1,SERVER);
        wait(1);
      }
   }
   if(enet_get_connection() == 2)
   {
      //set up the physic stuff
		phent_settype(my, PH_RIGID, PH_BOX);//physik entity definieren & kollision = sphere
		phent_setmass(my, 75, PH_SPHERE);//masse definieren & rotation = sphere
      
      while(1)
      {
           //use skill1 for the controls of the entity
           //enet_send_pos(enet_ent_globpointer(my),BROADCAST);
           //enet_send_ang(enet_ent_globpointer(my),BROADCAST);
           wait(1);
      }
   }

}



i'm working with the way you set up the chat program, so the entities are created when the client joins via:

Code:
function create_player()
{
	VECTOR creating_pos; //position were the entity will be created
	
	//generates the starting position:
	if(enet_get_clientid() == 0) {creating_pos.x = 0;creating_pos.y = 0;}
	if(enet_get_clientid() == 1) {creating_pos.x = -200;creating_pos.y = 0;}
	if(enet_get_clientid() == 2) {creating_pos.x = 200;creating_pos.y = -200;}
	if(enet_get_clientid() == 3) {creating_pos.x = 200;creating_pos.y = 200;}
	creating_pos.z = 0;
	
	//creates the coresponding character:
	if(character == 1) {player =enet_ent_create(_str("char1.mdl"),creating_pos,_str("move_player"));}
	if(character == 2) {player =enet_ent_create(_str("char1.mdl"),creating_pos,_str("move_player"));}
	if(character == 3) {player =enet_ent_create(_str("char1.mdl"),creating_pos,_str("move_player"));}

}



what this does however is, when i run as server/client, it creates the entity but physics isnt applied, when i join as a client, the character i created has physics applied, now if am correct, somethings wrong here? i shouldnt be seeing any movement on the client unless i sent positions?

[edit] i think i may be overlooking something simple however, am looking into it, i made this post maybe a little early
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/19/09 14:35

if(enet_get_connection() == 2) means on every client, but you want the movement on the server correct?

=> correct would be if(enet_get_connection() == SERVER_MODE || enet_get_connection() == CLIENT_SERVER_MODE)
Posted By: darkinferno

Re: [Anet] physics movement setup - 08/19/09 17:11

ok.. well the above worked, i now have movement on the clients by sending positions from the server but, the server/client doesnt get updated, only the other clients that join...
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/19/09 17:15

Can you post your code? (the entity function)
Posted By: darkinferno

Re: [Anet] physics movement setup - 08/19/09 18:32

Code:
function move_player()
{
	var save_pan = 0; //stores the entity.pan (needed to find out if the pan has changed)
	VECTOR save_pos; //stores the entity.pos (needed to find out if the position has changed)
	
	while(enet_ent_globpointer(my) == -1) {wait(1);} //wait until the entity gets a global pointer
	
	if(enet_ent_creator(enet_ent_globpointer(my)) == enet_get_clientid())
	{ //Every client controls his own player
		while(1)
		{
			vec_set(camera.x,my.x);
			//store the keys as bits in a skill1
			// if(my.skill1 != oldskill1) enet_send_skill(enet_ent_globpointer(my),1,1,SERVER);
			wait(1);
		}
	}
	if(enet_get_connection() == SERVER_MODE || enet_get_connection() == CLIENT_SERVER_MODE)
	{
		//set up the physic stuff
		phent_settype(my, PH_RIGID, PH_BOX);//physik entity definieren & kollision = sphere
		phent_setmass(my, 75, PH_SPHERE);//masse definieren & rotation = sphere
		
		while(1)
		{
			//use skill1 for the controls of the entity
			
			
			//sends the position if changed
			if(save_pos.x != my.x || save_pos.y != my.y || save_pos.z != my.z) {enet_send_pos(enet_ent_globpointer(my),-1);vec_set(save_pos,my.x);}
			//sends the angles if they changed
			if(save_pan != my.pan) {enet_send_angle(enet_ent_globpointer(my),-1);save_pan = my.pan;}


			wait(1);
		}
	}

}



thats it so far
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/19/09 19:10

Yeah so what's the problem? Now you have to save every key that is used for the controls as a seperate bit in skill1.

And on the server you evaluate the input and do the movement.

Then the snippet is ready.
Posted By: darkinferno

Re: [Anet] physics movement setup - 08/19/09 19:19

yes, am not worried about that, what am saying is, with the above code, when i run as server/client, the client that comes with that option isnt receiving any position updates... so when i start a game with server/client:

the character floats there, when i join as another client, i fall to the floor, as i should, all clients see the server/client floating in the air, position isnt being updated, dont know if i'm being clear
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/19/09 19:32

Oh you meand that. Yes currently the code is designed that only normal clients can control players.

The problem is the if construction. When the client server runs this function and it created the entity the execution of the function get's stuck in the first while loop.

This could should also make it able for the client server to control players:

Code:
function move_player()
{
	var save_pan = 0; //stores the entity.pan (needed to find out if the pan has changed)
	VECTOR save_pos; //stores the entity.pos (needed to find out if the position has changed)
	
	while(enet_ent_globpointer(my) == -1) {wait(1);} //wait until the entity gets a global pointer
	
	if(enet_ent_creator(enet_ent_globpointer(my)) == enet_get_clientid())
	{ //Every client controls his own player
		control_player();
	}
	if(enet_get_connection() == SERVER_MODE || enet_get_connection() == CLIENT_SERVER_MODE)
	{
		//set up the physic stuff
		phent_settype(my, PH_RIGID, PH_BOX);//physik entity definieren & kollision = sphere
		phent_setmass(my, 75, PH_SPHERE);//masse definieren & rotation = sphere
		
		while(1)
		{
			//use skill1 for the controls of the entity
			
			
			//sends the position if changed
			if(save_pos.x != my.x || save_pos.y != my.y || save_pos.z != my.z) {enet_send_pos(enet_ent_globpointer(my),-1);vec_set(save_pos,my.x);}
			//sends the angles if they changed
			if(save_pan != my.pan) {enet_send_angle(enet_ent_globpointer(my),-1);save_pan = my.pan;}


			wait(1);
		}
	}

}

function control_player()
{
   if(enet_get_connection() == CLIENT_SERVER_MODE) proc_mode = PROC_LATE;
   while(1)
   {
	vec_set(camera.x,my.x);
	//store the keys as bits in a skill1
	// if(my.skill1 != oldskill1 && enet_get_connection() != CLIENT_SERVER_MODE) enet_send_skill(enet_ent_globpointer(my),1,1,SERVER);
	wait(1);
   }
}



Please notice the change in the if(my.skill1 != oldskill ... line. If you are controling your player as client server there is no need to send the skill. Also the PROC_LATE is important, otherwise the movement will lag a bit (see manual for PROC_LATE).
Posted By: darkinferno

Re: [Anet] physics movement setup - 08/19/09 20:51

yes it works perfectly, i was looking into how to store the key pressed into a skill, was looking at key_lastpressed but i dont think it'll accomplish what i'm trying to do, i take it i have to be sending the scan code of the keys but basically am on my way now ^^
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/20/09 08:46

I meant it this way:

Every key can be pressed or not pressed => 1 or 0
A skill consists of 4 Byte => 32 bit

Now you say, bit 1 is the status of the move forward key. If bit1 is 1 this key is pressed, if it is 0 it isn't.

This is how you store the keys:
Code:
my.skill1 = 0;
my.skill1 |= key_pressed(20); //forward key in bit1
my.skill1 |= key_pressed(21)<<1; //backward key in bit2
my.skill1 |= key_pressed(22)<<2; //left key in bit3
my.skill1 |= key_pressed(23)<<3; //right key in bit4
//and so on



On the server you can get them this way:
Code:
forward_key = my.skill1&0x00000001; //get bit1
backward_key = my.skill1&0x00000002; //bet bit2
left_key = my.skill1&0x00000004;
right_key = my.skill1&0x00000006;
//and so on


Posted By: darkinferno

Re: [Anet] physics movement setup - 08/20/09 20:38

it seems ok, but i'm using it in the same way you gave:

but on the server i can only get the forward key, everything else is non-responsive, am i missing something, also, is there anything like this anywhere in the manual
Posted By: Gumby22don

Re: [Anet] physics movement setup - 08/20/09 21:58

I'm no expert, but should:

right_key = my.skill1&0x00000006;

be

right_key = my.skill1&0x00000008;

Just for someone trying to grab all this code, as a beginner it may be confusing. (Reason, powers of two are used to store these numbers on top of each other like this)

-Don
have a great day
Posted By: darkinferno

Re: [Anet] physics movement setup - 08/20/09 22:50

yes i see, this isn't the problem however, the problem is, it doesn't seem to be storing the values correctly, i store the values in the exact same way as dark_samurai stated but only the first one seems to work, which is:

my.skill1 = 0;
my.skill1 |= key_pressed(20); //forward key in bit1

i then get that value and store it in a variable called forward_key
forward_key = my.skill1&0x00000001; //get bit1

this works and i am able to get forward movement on the server, i get no other movement however so am wondering if the values are being stored correctly..
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/21/09 09:31

Gumby22don is right it must be 0x00000008 because 0x00000006 is not a power of two (I make this mistake quite often ^^). Btw. the scancodes I used are NOT the scancodes of w-a-s-d!

Best would be to work with the debugger or print the variable.

Test it this way:
- press forward key => see what value skill1 has on the client (1)
- press backward key => check the value (2)
- press left key => check the value (4)
- press right key => ckeck the value (8)

The correct values are in the brackets.
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/21/09 16:39

Ok I think I found the problem:

Code:
forward_key = my.skill1&0x00000001; //get bit1
backward_key = (my.skill1&0x00000002)>>1; //bet bit2
left_key = (my.skill1&0x00000004)>>2;
right_key = (my.skill1&0x00000008)>>3;



This should work now.
Posted By: darkinferno

Re: [Anet] physics movement setup - 09/01/09 13:31

yes it does work, i've been away for a while but in my absence i found several problems with the code setup, this is how i receive the controls on the server:

Code:
if(enet_get_connection() == SERVER_MODE || enet_get_connection() == CLIENT_SERVER_MODE)
	{
		//set up the physic stuff

		
		var forward_key;
		var backward_key;
		var left_key;
		var right_key;
		var space_key;
		
		my.can_jump=0;
		while(1)
		{
			forward_key = my.skill1&0x00000001; //get bit1
			backward_key = (my.skill1&0x00000002)>>1; //get bit2
			left_key = (my.skill1&0x00000004)>>2; //get bit3
			right_key = (my.skill1&0x00000008)>>3; //get bit4
			space_key = (my.skill1&0x000000016)>>4; //get bit5



with the above code setup, i can only move on the server/client not on the normal clients, in order to move on the regular clients i have to add:

" || enet_get_connection() == CLIENT_MODE "

Code:
if(enet_get_connection() == SERVER_MODE || enet_get_connection() == CLIENT_SERVER_MODE || enet_get_connection() == CLIENT_MODE )
	{
		//set up the physic stuff

		
		var forward_key;
		var backward_key;
		var left_key;
		var right_key;
		var space_key;
		
		my.can_jump=0;
		while(1)
		{
			forward_key = my.skill1&0x00000001; //get bit1
			backward_key = (my.skill1&0x00000002)>>1; //get bit2
			left_key = (my.skill1&0x00000004)>>2; //get bit3
			right_key = (my.skill1&0x00000008)>>3; //get bit4
			space_key = (my.skill1&0x000000016)>>4; //get bit5



this allows movement on the client but it also moves the entity directly, meaning that, even if i remove the "enet_send_pos" the model still moves... i really wanna get this basic setup running
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 09/01/09 14:36

Can you post the whole function and tell me what it exactly should do for you. Then I can tell you where the problem is.
Posted By: darkinferno

Re: [Anet] physics movement setup - 09/01/09 15:19

Code:
var updateRate;


function control_player()
{
	var oldskill1;
	var save_pan = 0; //stores the entity.pan (needed to find out if the pan has changed)
	
	if(enet_get_connection() == CLIENT_SERVER_MODE) proc_mode = PROC_LATE;
	
	trigger_ctrl();
	
	while(1)
	{
		player_camsys(); //this is cam system of the player, found in player_system.c
		
		//store the keys as bits in a skill1
		my.skill1 = 0;
		my.skill1 |= key_pressed(key_for_str("f")); //forward key in bit0
		my.skill1 |= key_pressed(key_for_str("v"))<<1; //backward key in bit1
		my.skill1 |= key_pressed(key_for_str("c"))<<2; //left key in bit2
		my.skill1 |= key_pressed(key_for_str("b"))<<3; //right key in bit3
		my.skill1 |= key_pressed(key_for_str("Space"))<<4; //right key in bit4

		enet_send_skills(enet_ent_globpointer(my),6,6,SERVER);

		
		if(my.skill1 != oldskill1 && enet_get_connection() != CLIENT_SERVER_MODE) 
		{enet_send_skills(enet_ent_globpointer(my),1,1,SERVER);	oldskill1=my.skill1;} //oldskill1=my.skill1;

		camera.pan -= 2*mouse_force.x; //rotates the entity with the mouse
		camera.tilt += 2*mouse_force.y; //rotates the entity with the mouse
		
		my.pan=camera.pan;
		
		my.skill[2] = camera.pan; //damages the player 
		my.skill[10] = camera.tilt; //damages the player 
		if(save_pan != my.pan) {enet_send_skills(enet_ent_globpointer(my),2,2,-1); save_pan = my.pan;}
		

		enet_send_skills(enet_ent_globpointer(my),10,10,SERVER);

		wait(1);
	}
}

function move_player()
{
	//[UNUSED]var save_pan = 0; //stores the entity.pan (needed to find out if the pan has changed)
	VECTOR save_pos; //stores the entity.pos (needed to find out if the position has changed)
	var timePassed = 0;
	
	while(enet_ent_globpointer(my) == -1) {wait(1);} //wait until the entity gets a global pointer
	
	//pl_animate(1); // this contains the blended animation states
	key_ctrlr(); //controls key inputs
	
	if(enet_ent_creator(enet_ent_globpointer(my)) == enet_get_clientid())
	{ 
		//Every client controls his own player
		control_player();
		control_anim_st();	// this control local animations	
	}
	else
	{
		control_other_anim_st();
	}
	if(enet_get_connection() == SERVER_MODE || enet_get_connection() == CLIENT_SERVER_MODE || enet_get_connection() == CLIENT_MODE )
	{
		//set up the physic stuff
		phent_settype(my, PH_RIGID, PH_BOX);//physik entity definieren & kollision = sphere
		phent_setmass(my, 75, PH_SPHERE);//masse definieren & rotation = sphere
		phent_setdamping(my,100.0, 100.0);//Reibung definieren & drehungsdämpfung nach kollision
		//	phent_setgroup(player, 2);
		
		var forward_key;
		var backward_key;
		var left_key;
		var right_key;
		var space_key;
		
		my.can_jump=0;
		while(1)
		{
			forward_key = my.skill1&0x00000001; //get bit1
			backward_key = (my.skill1&0x00000002)>>1; //get bit2
			left_key = (my.skill1&0x00000004)>>2; //get bit3
			right_key = (my.skill1&0x00000008)>>3; //get bit4
			space_key = (my.skill1&0x000000016)>>4; //get bit5

			phys_ctrl(forward_key,backward_key,left_key,right_key);
			//jump_ctrl(space_key);
			
			if (my.weap_trigger==1)client_shoot();
			
			//sends the position if changed
			if(timePassed >= updateRate)
			{
				vec_set(my.skill[8],my.x);
				enet_send_skills(enet_ent_globpointer(my),8,10,-1); //sends the new skill value
			}
			if(save_pos.x != my.x || save_pos.y != my.y || save_pos.z != my.z) {enet_send_pos(enet_ent_globpointer(my),-1);vec_set(save_pos,my.x);}
			
			//[UNUSED]sends the angles if they changed		
			//[UNUSED]if(save_pan != my.pan) {enet_send_angle(enet_ent_globpointer(my),-1);save_pan = my.pan;}

			timePassed += time_frame;

			wait(1);
		}
	}
	else
	{
		//		my.pan=camera.pan;
		//		my.skill2=my.pan;
		//		enet_send_skills(enet_ent_globpointer(my),2,2,SERVER);
	}

}



basically what i want is for each player to be created as a physics entity on the server[works so far] now i want each client to send the keys pressed to the server, the server then moves that clients' entity and send the positions bac to each client
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 09/02/09 09:31

Don't do to many steps in one. This is the code how I told you this could be done, right? This should be working this way: The clients send the control input for there players to the server/clientserver. The server/clientserver moves the entity using the physic engine and sends the positions and angles every frame to all the other participants.

Code:
function move_player()
{
	var save_pan = 0; //stores the entity.pan (needed to find out if the pan has changed)
	VECTOR save_pos; //stores the entity.pos (needed to find out if the position has changed)
	
	while(enet_ent_globpointer(my) == -1) {wait(1);} //wait until the entity gets a global pointer
	
	if(enet_ent_creator(enet_ent_globpointer(my)) == enet_get_clientid())
	{ //Every client controls his own player
		control_player();
	}
	if(enet_get_connection() == SERVER_MODE || enet_get_connection() == CLIENT_SERVER_MODE)
	{
		phent_settype(my, PH_RIGID, PH_BOX);//physik entity definieren & kollision = sphere
		phent_setmass(my, 75, PH_SPHERE);//masse definieren & rotation = sphere
		phent_setdamping(my,100.0, 100.0);//Reibung definieren & drehungsdämpfung nach kollision
		//	phent_setgroup(player, 2);
		
		while(1)
		{
			var forward_key = my.skill1&0x00000001; //get bit1
			var backward_key = (my.skill1&0x00000002)>>1; //get bit2
			var left_key = (my.skill1&0x00000004)>>2; //get bit3
			var right_key = (my.skill1&0x00000008)>>3; //get bit4
			var space_key = (my.skill1&0x000000016)>>4; //get bit5

                        phys_ctrl(forward_key,backward_key,left_key,right_key);
			
			
			//sends the position if changed
			if(save_pos.x != my.x || save_pos.y != my.y || save_pos.z != my.z) {enet_send_pos(enet_ent_globpointer(my),-1);vec_set(save_pos,my.x);}
			//sends the angles if they changed
			if(save_pan != my.pan) {enet_send_angle(enet_ent_globpointer(my),-1);save_pan = my.pan;}


			wait(1);
		}
	}

}

function control_player()
{
   if(enet_get_connection() == CLIENT_SERVER_MODE) proc_mode = PROC_LATE;
   while(1)
   {
	vec_set(camera.x,my.x);
	my.skill1 = 0;
	my.skill1 |= key_pressed(key_for_str("f")); //forward key in bit0
	my.skill1 |= key_pressed(key_for_str("v"))<<1; //backward key in bit1
	my.skill1 |= key_pressed(key_for_str("c"))<<2; //left key in bit2
	my.skill1 |= key_pressed(key_for_str("b"))<<3; //right key in bit3
	my.skill1 |= key_pressed(key_for_str("Space"))<<4; //right key in bit4
	if(my.skill1 != oldskill1 && enet_get_connection() != CLIENT_SERVER_MODE) enet_send_skill(enet_ent_globpointer(my),1,1,SERVER);
	wait(1);
   }
}



I will show you how you correctly make a updaterate and interpolation when the code above is working for you and you understood the code.
Posted By: darkinferno

Re: [Anet] physics movement setup - 09/02/09 10:48

i do have movement but thats just the physics engine, i can walk around using the server/client but the regular clients dont move, i take it they key presses arent being received properly?
Posted By: darkinferno

Re: [Anet] physics movement setup - 09/05/09 22:27

i take it this problem is a bit hard to manage?
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 09/06/09 09:45

Quote:
i take it this problem is a bit hard to manage?


No, this is a simple snippet. Just add me to ICQ (see profil for ICQ number) and I will help you there (this will go much faster).
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 09/06/09 19:16

The problem is solved now. I posted final snippet with update_rate and interpolation in the Lite-C Contributions forum.
Posted By: Zwiebli

Re: [Anet] physics movement setup - 08/19/10 21:08

Hi,
I have a question concerning the saving of the keys in bits of a skill. It worked till I wanted to read the 7th bit. The var check3 showed 0. This is how I wrote it:

my.skill1 = 0;
my.skill1 |= 1;
my.skill1 |= 1<<1;
my.skill1 |= 1<<2;
my.skill1 |= 1<<3;
my.skill1 |= 1<<4;
my.skill1 |= 1<<5;
my.skill1 |= 1<<6;
my.skill1 |= 1<<7;
check2 = (my.skill1&0x00000001);
check2 = (my.skill1&0x00000002)>>1;
check2 = (my.skill1&0x00000004)>>2;
check2 = (my.skill1&0x00000008)>>3;
check2 = (my.skill1&0x000000016)>>4;
check2 = (my.skill1&0x000000032)>>5;
check2 = (my.skill1&0x000000064)>>6;
check3 = (my.skill1&0x0000000128)>>7;
Thanks for any help
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/20/10 18:24

You are mixing hexadecimal with decimal.

Hex:
0x01
0x02
...
0x09
0x0A
...
0x0F
0x10

So you could write it this way (decimal):
(my.skill1 & 1)
(my.skill1 & 2) >> 1
...
(my.skill1 & 128) >> 7
Posted By: Zwiebli

Re: [Anet] physics movement setup - 08/20/10 19:19

Is there a site where this is explained? I just pasted it from your Anet-manuel in the tipps & tricks section and thought it works analog till the 32nd bit. I didn`t even understand what the "0x000..." actually stands for.
thanks for the help
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 08/21/10 08:34

Hexadecimal is explained here: http://en.wikipedia.org/wiki/Hexadecimal

Maybe this helps you understand how using flags (= using single bits of a variable) works: http://www.rohitab.com/discuss/index.php?showtopic=25162
Posted By: Zwiebli

Re: [Anet] physics movement setup - 02/17/11 18:09

Hm sorry could you show me an example how to write/read the last bit of a skill? I still quite understand how it works...
thanks a lot
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 02/17/11 18:29

A skill is a var (= float). Which means it consists of 4 byte. Each byte has 8 bit.

--> 4 Byte = 32 bit

If you want to set the last bit of a skill you have to do a OR operation with
1000 0000 0000 0000 0000 0000 0000 0000 (binary!).
--> As you see the 32. bit is 1
Because we can't write binary numbers in lite-c, we have to calculate the according hex or decimal number:
0x80000000 (hex)
2147483648 (Dec)

my.skill[1] = my.skill[1] | 0x80000000; //sets the 32. bit

For clearing a bit, you have to make an AND operation with 0111 1111 1111 1111 1111 1111 1111 1111 (binary) = 0x7FFFFFFF (hex)

my.skill[1] = my.skill[1] & 0x7FFFFFFF; //resets the 32. bit


This may sound complicated but if you look into binary operations (AND/OR) and how binary numbers and hexadecimal works it is pretty easy wink
Posted By: Zwiebli

Re: [Anet] physics movement setup - 02/17/11 19:34

but if i set the last bit of skill1 shouldn`t it have the value 2147483648 afterwards if i set skill1 to zero before? Because only the value zero is displayed instead of 2147483648.

But thanks for the explanation it helped a lot laugh
If i want to read out the value of the last bit should it look something like this? :

right_key = (my.skill1 & 0x80000000)>>31

and thanks for the quick answer
Posted By: Dark_samurai

Re: [Anet] physics movement setup - 02/18/11 08:36

Exactly!

The problem is, that skills are floats => 10000...(binary) != 2147483648

If you will do this with an int instead of a float, you will see that it is 2147483648 after the OR operation.
© 2024 lite-C Forums