jrouwe / JoltPhysics

A multi core friendly rigid body physics and collision detection library. Written in C++. Suitable for games and VR applications. Used by Horizon Forbidden West.
MIT License
6.77k stars 452 forks source link

Program crashes when creating body #1250

Closed TheSlugInTub closed 2 months ago

TheSlugInTub commented 2 months ago

Hello, I'm trying to loop over entities with a rigidbody3d component (struct) and the struct has a Body* pointer.

    BodyInterface& bodyInterface = physicsSystem.GetBodyInterface();

    for (EntityID ent : SceneView<Transform, RigidBody3D>(engineState.scene)) // Loops over all entities with transform and rigidbody components
    {
        auto rigid = engineState.scene.Get<RigidBody3D>(ent);
        auto trans = engineState.scene.Get<Transform>(ent);

        if (rigid->colliderType == ColliderType::Box)
        {
            Vec3 transPosition(trans->position.x, trans->position.y, trans->position.z);
            Vec3 bodyScale(rigid->boxSize.x, rigid->boxSize.y, rigid->boxSize.z);
            RVec3 RtransPosition = transPosition;

            BoxShapeSettings floor_shape_settings(bodyScale);
            floor_shape_settings.SetEmbedded(); 

            // Create the shape
            ShapeSettings::ShapeResult floor_shape_result = floor_shape_settings.Create();
            ShapeRefC floor_shape = floor_shape_result.Get(); // We don't expect an error here, but you can check floor_shape_result for HasError() / GetError()

            BodyCreationSettings floor_settings(floor_shape, RtransPosition, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);

            // Create the rigid body
            rigid->body = bodyInterface.CreateBody(floor_settings); 

            // Add it to the world
            if (rigid->body != nullptr)
            {
                bodyInterface.AddBody(rigid->body->GetID(), EActivation::DontActivate);
            }
        }
        else
        {
            std::cerr << "ERROR: Collider type not implemented or is null" << std::endl;
        }
    }

This code is run for each entity with the rigidbody3d component (struct). But it just crashes immediately, it says 'Read access violation', the last two call stacks were BodyInterface::CreateBody and BodyManager::AllocateBody. This worked just fine when I had all the jolt physics stuff in the main function. And if you're wondering, this is the rigidbody struct:

 struct RigidBody3D
{
    ColliderType colliderType;
    glm::vec3 boxSize;

    Body* body;

    RigidBody3D(ColliderType type, glm::vec3 size)
        : colliderType(type), boxSize(size)
    {}
};
jrouwe commented 2 months ago

I'm sorry, but there's not enough information in this description for me to say anything sensible. The AllocateBody function for example is more than 50 lines of code so it could be anything. In general, creating bodies from other threads should work just fine.

TheSlugInTub commented 2 months ago

i wasn't registering the body allocator correctly, well I was but all the initialization of jolt and the allocator was inside a function, and that didn't work for some reason, i switched it to a macro that puts all that init stuff in your main function