jrouwe/JoltPhysics

btMotionState equivalent?

Closed this issue · 4 comments

Bullet physics had a 'btMotionState', which was essentially a callback that the physics system would call at the end of a simulation step for each object, where the display position information could be updated. Does Jolt have an equivalent, or do you need to iterate your objects after the simulation step and apply the changes with a ton of locking like so?

Also, is there a way to just obtain a 4x4 matrix of the object's position, rotation, etc. rather than grabbing and converting the position and rotation quaternion separately?

`

mPhysicsSystem->Update(Timer::GetDeltaTime(), 1, mTempAllocator, mJobSystem);

WObj *obj = Player::playerHead->obj; // Ugly dirty for example
// Get character position, rotation and velocity
JPH::RVec3 char_pos;
JPH::Quat char_rot;
JPH::Vec3 char_vel;
{
	JPH::BodyLockRead lock(sGetBodyLockInterface(mPhysicsSystem, true/*inLockBodies*/), obj->collisionObjectID);
	if (!lock.Succeeded())
		return;
	const JPH::Body &body = lock.GetBody();
	char_pos = body.GetPosition();
	char_rot = body.GetRotation();
	char_vel = body.GetLinearVelocity();

	obj->GetPos()->x = char_pos.GetX();
	obj->GetPos()->y = char_pos.GetY();
	obj->GetPos()->z = char_pos.GetZ();
	obj->mom.x = char_vel.GetX();
	obj->mom.y = char_vel.GetY();
	obj->mom.z = char_vel.GetZ();
}

`

Also, is there a way to just obtain a 4x4 matrix of the object's position, rotation, etc. rather than grabbing and converting the position and rotation quaternion separately?

Have you looked at Body::GetWorldTransform() and similiar for center of mass?

https://jrouwe.github.io/JoltPhysics/class_body.html#a4bab5d07f84d5a9d38119a1923298b5c
https://jrouwe.github.io/JoltPhysics/class_body.html#a9431bfa66a018714074fe149c7a65ac5

Also, is there a way to just obtain a 4x4 matrix of the object's position, rotation, etc. rather than grabbing and converting the position and rotation quaternion separately?

Have you looked at Body::GetWorldTransform() and similiar for center of mass?

https://jrouwe.github.io/JoltPhysics/class_body.html#a4bab5d07f84d5a9d38119a1923298b5c https://jrouwe.github.io/JoltPhysics/class_body.html#a9431bfa66a018714074fe149c7a65ac5

Aha! Thank you, I overlooked that.

Is it also faster to somehow lock 'all' objects when fetching the simulation changes post-update, or is locking each individual object the way-to-go?

Does Jolt have an equivalent, or do you need to iterate your objects after the simulation step and apply the changes with a ton of locking like so?

Jolt doesn't have a system built in for this. You can get PhysicsSystem::GetActiveBodies() before and after the step and take the union (or install a BodyActivationListener and add bodies that got activated during the step to your pre-step active bodies).

Next you would use BodyLockMultiRead to lock all bodies in one go (which is cheaper than one by one). If you know you're not modifying any state while this is happening you can pass PhysicsSystem::GetBodyLockInterfaceNoLock to this to avoid doing any actual locking.

Thanks! I'll check out the samples that call these functions, as it sounds like exactly what I need. Also, apologies for cluttering the Issue board -- I didn't realize that Discussions were enabled for this GitHub project and I'll use it going forward.