Archive 17/01/2023.

Character Movement/Physics flickering

dev4fun

Hey guys, Im doing character movement on my server, and I see that is flickering some times, dno why. Im using 60fps update on client and 30fps on server (physicsworld).

Video showing the problem:
https://puu.sh/zBdGu/2826b16857.mp4

Node Settings:

Map Floor settings:

	Node * pMapNode = pScene->CreateChild( pMap->GetName(), LOCAL );
	pMapNode->SetPosition( Vector3( 0, 0, 0 ) );
	pMapNode->SetScale( Vector3( 1.0f, 1.0f, 1.0f ) );
	
	auto * pBody = pMapNode->CreateComponent<RigidBody>();
	pBody->SetFriction( 1.0f );

	auto * pShape = pMapNode->CreateComponent<CollisionShape>();
	pShape->SetTriangleMesh( GetSubsystem<ResourceCache>()->GetResource<Model>( 	pMap->GetBaseMap()->GetFileName() ) );

Character Mover on Server:

	Node * pCharacterNode = GetNode();
	auto * pCharacterBody = pCharacterNode->GetComponent<RigidBody>();
	
	const Controls & cControls = pConnection->GetControls();
	const Vector3 & cVelocity = pCharacterBody->GetLinearVelocity();
	
	Quaternion cRotation( 0.0f, cControls.yaw_, 0.0f );
	Vector3 cMoveDirection = Vector3::ZERO;

	Vector3 cPlaneVelocity( cVelocity.x_, 0.0f, cVelocity.z_ );

	if( cControls.buttons_ & CHARACTERCONTROL_Forward )
	{
		cMoveDirection += Vector3::FORWARD;
		pCharacterNode->SetRotation( cRotation );
	}
	
	if( cMoveDirection.LengthSquared() > 0.0f )
		cMoveDirection.Normalize();

	pCharacterBody->ApplyImpulse( cRotation * cMoveDirection * 1.0f );

	Vector3 cBrakeForce = -cPlaneVelocity * 0.1f;
	pCharacterBody->ApplyImpulse( cBrakeForce );

Thanks!

Eugene

Does it work smoothly if simulated locally?

dev4fun

Server is running locally, so, shouldnt happen this. Dont know if can be some setting on physics… I tried get the physics settings from 18_CharacterDemo sample.

1vanK
node->GetOrCreateComponent<SmoothedTransform>();
George1

I think the below code affecting that motion.

Vector3 cBrakeForce = -cPlaneVelocity * 0.1f;
pCharacterBody->ApplyImpulse( cBrakeForce );

Can you comment it out to see if it is ok?

1vanK

Do you work with physics in function FixedUpdate() ?

dev4fun

Yes, Im using a Logic Component. ll try about SmoothedTransform.

dev4fun

Didnt work.

@ Could be something on yaw? Dno. Nothing works until now.

//Manipulating Camera Yaw
if( INPUT->GetKeyDown( KEY_LEFT ) )
	fCameraYaw += 0.5f;
else if( INPUT->GetKeyDown( KEY_RIGHT ) )
	fCameraYaw -= 0.5f;

//Getting Mouse Yaw
fMouseYaw = Atan2( INPUT->GetMousePosition().y_ - (GRAPHICS->GetSize().y_ >> 1), INPUT->GetMousePosition().x_ - (GRAPHICS->GetSize().x_ >> 1) ) + 90.0f;

And to send to server side:

cControls.yaw_ = CAMERAMANAGER->GetCameraYaw() + CAMERAMANAGER->GetMouseYaw();

@@ 've tried remove brake force, change the value that Im multiplying the impulse, but nothing is changed. Same problem on all changes.

@@@ Same making the physics on PhysicsPreStep…

1vanK

pCharacterNode->SetRotation( cRotation );

actually you can not set rotations and positions for dynamics bodies, you can use only forces and impulses

1vanK

Honestly, you do not need to rotate the capsule, just push it in the direction you want. Just rotate camera around object in Update (in Update you can use standart input->GetMouseMove() for yaw)

1vanK

You can calculate impulses which will move the body.
For example I use this code for control movement speed:

        Vector3 desiredVel = rotation * dir * MOVE_SPEED;
        Vector3 impulse = (desiredVel - playerBody->GetLinearVelocity()) * playerBody->GetMass();// * 0.1f;
        playerBody->ApplyImpulse(impulse);

Same for rotations

EDIT: based on http://www.iforce2d.net/b2dtut/constant-speed

dev4fun

Hmm ic. Same after all changes quoted on this topic, the problem always appears, dno why.

If I use ur code out this condition if( cControls.buttons_ & CHARACTERCONTROL_Forward ), this happens (beyond flickr)

Its strange, in every way the flicker happens lol.

Don

Could you provide a repository that we could look at? This seems like an issue that could encompass many parts of the code, and providing the full scope could help. Also I want to say, your graphical style is really slick.

Eugene

+1 for @Don
I could investigate the problem if there is something run-able.

Eugene

Haa, I have an idea.

Ensure that:

  1. You have turned off server physics interpolation.
  2. You have RigidBody of your character only on server side. The code may work even if you have bodies both on client and server side. I’d like not to tempt fate.

Position and rotation are Node attributes, while linear and angular velocities are RigidBody attributes. To cut down on the needed network bandwidth the physics components can be created as local on the server: in this case the client will not see them at all, and will only interpolate motion based on the node’s transform changes. Replicating the actual physics components allows the client to extrapolate using its own physics simulation, and to also perform collision detection, though always non-authoritatively.

By default the physics simulation also performs interpolation to enable smooth motion when the rendering framerate is higher than the physics FPS. This should be disabled on the server scene to ensure that the clients do not receive interpolated and therefore possibly non-physical positions and rotations. See SetInterpolation().

dev4fun

Hmm ye I have character only on server side… I should create in client replicated or local? I will not have 2 characters at same time?

Thanks for ur help, I believe that u solved the problem, ll try this later.

Eugene

If you have character on the server side only, it should be fine.
Then, just turn off physics interpolation on the server side.
If it doen’t help… well, make a sample for ppl to try it. I’ve never seen such bugs with sample network app.

dev4fun

Interpolation was already turned off lol.

SVN Repo:
client: https://subversion.assembla.com/svn/dev-client/trunk
server: https://subversion.assembla.com/svn/dev-server/trunk
shared: https://subversion.assembla.com/svn/dev-shared/trunk

If someone want to see this in-game: https://puu.sh/zCy4x/f004273bf4.rar
(should run login server, after game server)(can type any login and pw, and dont need a character nick)(to walk let mouse button left pressing or double click with mouse button left)

void CMapHandler::CreateScene() - create scene to server and maps collision
CCharacter * CCharacterHandler::LoadCharacter( Connection * pConnection, String strCharacterName ) - instantie XML responsible for character node and create the logic component CCharacter
void CCharacter::FixedUpdate( float fTime ) - physics of each character at scene

I really dno what more can be, Im “newbie” on Urho3D, and until now this is the unique problem I dont solved.
Thanks.

Eugene

Archive seems broken. Or maybe some very new version of winRAR.
Tried to download&open it two times.

And… I have no idea how to download SVN repos w/o any installed SVN client.

Eugene

I’m looking at your code and I’m not sure that the Client doesn’t have RigidBody component: in the file Server Data\Objects\Character.xml there is replicated RigidBody component that is probably replicated on the client side.

dev4fun

Oh ya, its replicated… so maybe I understood wrong your question in previous post.

dev4fun

lol its really strange, when I believe that is solved, ll check and the problem appears again and again. Sometimes I got less flickering, sometimes more…

Eugene

So even if you 100% don’t have any RigidBody for characters on the client side, you still get artifacts?

Try this test plan:

Do you have artifacts with standard SceneReplication sample?
If yes… that’s strange, I don’t want to even think about it.
If no, do you have artifacts if use your character controller in SceneReplication sample?
If yes… that’s strange, I don’t want to even think about it.
If no, artifacts occur somewhere in your code that is different from SceneReplication sample…

dev4fun

Hmm ya probably its something on my source. 've implemented the movement code and camera code on scene replication sample and all works good…

So it isnt:

  • Camera (yaw)
  • Object (xml) from character
  • Movement Physics Code (ApplyImpulse etc)
  • Collision (used same collision as used on SceneReplication)
  • Logic Component (CCharacter)

Jesus, 'll need to review whole source code haha.

Eugene

Well, bisection isn’t so hard, much better than “nothing works”.
I suggest you to compare scenes in sample an in your project.
E.g. just dump them in XML.
IMO it’s quite hard to break inbuilt interpolation from code unless you intentionally do something outside your server instance of the Character.

dev4fun

Hmm ya, 'll try this haha.

Just to know, scene on server, needs to be identical on game? Bcoz on my server 'll use just to coliision, this way I put all maps (models) on same scene. But on game, I load just one map, and when necessary I load other map. This way scene game != scene server.

Eugene

Replicated nodes and components must be identical. They will be, regardless of your will. Local nodes and components… you know. They don’t have to.

dev4fun

Things I’ve tested:

  • Camera (yaw)
  • Object (xml) from character
  • Movement Physics Code (ApplyImpulse etc)
  • Collision (used same collision as used on SceneReplication)
  • Logic Component (CCharacter)
  • Multiple connections to server (client connect to login server and game server)
  • Scene Settings

Comparing dumped scenes…

My alternatives are practically exhausted, I really could pay someone to help me
I dont have the slightest idea of what might be happening :frowning:

dev4fun

Sure, I made some tests and I came to the conclusion that the problem is the game… :roll_eyes: Why?

My Game with Server of Scene Replication Sample = flickering
Scene Replication Game with My Server = all works good

Now I need to check game code to see what could be happening. Any idea?

dev4fun

Problem was bcoz camera was updating by HandleUpdate and dont HandlePostUpdate… :tired_face:
I have to made many tests to discover this and think on this haha, thanks and sorry to all who helped me :smiley: