Mystery offset collision box in raycasts. #1313
-
I recently switched from Bullet to Jolt in my game. I'm only using raycasts on collision shapes. Unfortunately, my raycasts are hitting a 'shadow' or mystery mirror collision box which is offset. I don't think it's the CenterOfMass issue that is mentioned in the documentation. https://www.youtube.com/watch?v=r4bN1SKhmg0 (video example) I use EnTT for entity component system and have a transform component that holds the newly created BodyID for when the body needs updating. I also set the Entity ID on the BodyID as user data so I can correlate the 2. This is the setup for when I spawn an entity: // I have debugged these half extents and they are always good
JPH::BoxShapeSettings box_settings(halfExtents, 0.f); // Have experimented with different values here as my boxes are somtimes under the 0.005f convex limit
JPH::Shape::ShapeResult result = box_settings.Create();
if (result.IsValid())
{
JPH::ShapeRefC collisionShape = result.Get();
// Create body creation settings
JPH::BodyCreationSettings body_settings(
collisionShape,
newPosition, // A JPH::Vec3 where the entity will be spawned in WorldSpace
JPH::Quat::sIdentity(),
mesh.isStatic ? JPH::EMotionType::Static : JPH::EMotionType::Kinematic,
mesh.isStatic ? Layers::NON_MOVING : Layers::MOVING);
// Use EnTT entity index to connect with BodyID
body_settings.mUserData = static_cast<uint64_t>(static_cast<uint32_t>(e.ID()));
e.MutableTransform().bodyID = m_physics->CreateBody(body_settings);
}
In my PhysicsManager instance, this is the CreateBody BodyID PhysicsManager::CreateBody(const BodyCreationSettings &inSettings)
{
BodyInterface &bodyInterface = m_pimpl->physicsSystem->GetBodyInterface();
BodyID newID = bodyInterface.CreateAndAddBody(inSettings, EActivation::Activate);
return newID;
} And every physics cycle: {
auto transformsView = m_registry.view<nda::TransformComponent>();
auto joltPhysics = m_physics->System();
BodyInterface &body_interface = joltPhysics->GetBodyInterface();
std::for_each(
transformsView.begin(),
transformsView.end(),
[this, &body_interface, &transformsView](auto e) {
const auto &transform = transformsView.get<nda::TransformComponent>(e);
const glm::vec3 &pos = transform.Position();
const glm::quat &q = transform.Orientation();
body_interface.SetPositionAndRotation(
transform.bodyID,
Vec3(pos.x, pos.y, pos.z),
Quat(q.x, q.y, q.z, q.w),
EActivation::DontActivate);
});
} And lastly my RayCast function std::pair<entt::entity, JPH::BodyID> PhysicsManager::RayCastClosest(const glm::vec3 &from, const glm::vec3 &to)
{
// Perform a raycast
// JPH::RayCast ray;
JPH::RRayCast ray(nda::ToJolt(from), nda::ToJolt(to));
JPH::RayCastResult rayCastResult;
if (m_pimpl->physicsSystem->GetNarrowPhaseQuery().CastRay(ray, rayCastResult))
{
const auto bodyID = rayCastResult.mBodyID;
// Now get the body itself in order to retrieve the user data
const auto userData = m_pimpl->physicsSystem->GetBodyInterface().GetUserData(bodyID);
if (userData)
{
const auto id = static_cast<entt::entity>(static_cast<uint32_t>(userData));
return { id, bodyID };
}
}
return { entt::null, JPH::BodyID() };
} I've hit a bit of a brick wall here and despite stepping in through the debugger, verifying that the raycast TO/FROM is correct, I just can't seem to figure out why this mystery collision is happening. Grateful for any help at this point. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Try changing:
to
|
Beta Was this translation helpful? Give feedback.
Try changing:
to