Proper way to update transform from other threads.

Post Reply
Tonyx97
Posts: 5
Joined: Sat Sep 10, 2016 5:32 pm

Proper way to update transform from other threads.

Post by Tonyx97 »

Hey guys, I've been reading about motionstates etc, I have bullet physics implemented in my project almost a year ago but I'm improving it since I have annoying problems like this: https://i.gyazo.com/41f1d552fd37959a9c1 ... 23a4ef.mp4

As you can see when there are many objects the character starts flickering (you can't see it because of gyazo but it flickers forwards and backwards). I have a renderer, physics and scripting thread. The scripting thread manages almost everything. When my character is moving it's executing this LUA code:

Code: Select all

local Pitch, Yaw, Roll = getCameraRotation();
local cx, cy, cz = getEntityPosition(ControllerEntity);
local vx, vy, vz = getEntityVelocity(ControllerEntity);
local velRot = Yaw + angle
local fx = math.cos(velRot);
local fy = vy;
local fz = math.sin(velRot);
local movingSpeed = fSync(1.53);
local yRealRot = -Yaw - angle - math.pi / 2
			
--setEntityPosition(ControllerEntity, cx + fx * movingSpeed, cy + fy * movingSpeed, cz + fz * movingSpeed, true);

setEntityRotation(ControllerEntity, toQuaternion(0.0, yRealRot, 0.0));
setEntityVelocity(ControllerEntity, fx * movingSpeed * 100, fy, fz * movingSpeed * 100)
And this happens even with an atomic bool used to stepSimulation when the render scene starts. I don't know if this is a multi-threading problem or bad values in stepSimulation... This is the physics thread.

Code: Select all

while (!g_Renderer->IsClosing())
{
	if (g_PhysicsManager->WaitRendererComplete()) // true when render scene starts
	{
		g_PhysicsManager->SetUpdateState(false); // false to avoid 2 stepSimulations in the same frame.

		g_PhysicsManager->UpdateQueues(); //load/remove pending rigidbodies
		g_PhysicsManager->GetWorld()->stepSimulation(btScalar(1.f / (float)60.f), 3, btScalar(1.f / static_cast<float>(g_Renderer->GetFPS())));
	}
	g_TimeManager->Sleep<microseconds>(1); // also tried ms
}
In this case I'm not changing any transform since I just set the velocity of the entity using its attached rigidbody. The flickering also happens when there are a lot of entities and I change the position using the following code:

Code: Select all

m_vecPosition = vecPos;
if (m_bUsingPhysics)
{
	btTransform matTrans = m_pPhysics->m_pRigidBody->getWorldTransform();
	matTrans.setOrigin(btVector3(m_vecPosition.x, m_vecPosition.y, m_vecPosition.z));

	m_pPhysics->m_pRigidBody->activate(true);
	m_pPhysics->m_pRigidBody->setWorldTransform(matTrans); // I tried using this without the next line
	m_pPhysics->m_pRigidBody->getMotionState()->setWorldTransform(matTrans); // I also tried this one without the previous line
}
I call this function 1 frame after the same frame I changed the position. Is this function ok or am I doing something wrong? I couldn't find an example to replace my function with a better one. Any thoughts about how to fix this problem? I'd appreciate any explained tips to avoid this flickering problem :) Thanks for read.

Edit: At the beginning I thought it was the friction but that couldn't be the problem since it works when there aren't a lot of entities.
Post Reply