UnderwaterApps / overlap2d-runtime-libgdx

Overlap2D - UI and Level Editor libgdx runtime
http://overlap2d.com
Other
170 stars 96 forks source link

PhysicsBodyComponent.body is null #83

Open kschoos opened 8 years ago

kschoos commented 8 years ago

When an entity is created and added to the system, PhysicsBodyComponent.body will not be instantiated. It only gets instantiated after a null check in PhysicsSystem::processBody(). This means that we would have to wait for 1 engine tick in order to be sure all physics objects are properly instantiated.

A fix would be to call PhysicsBodyLoader::createBody() from ComponentFactory::createPhysicsComponents(). At that point we have all the information we need to build the physicsbody if I am not mistaken.


SpineComponentFactory::createComponents(...)and CompositeComponentFactory::createComponents(...)both call

ComponentFactory::createCommonComponents(entity, vo, ...);
ComponentFactory::createPhysicsComponents(entity, vo)

practically creating the same object twice. In case we call PhysicsBodyLoader::createBody() from ComponentFactory::createPhysicsComponents() it even creates two bodies at the same spot.


Maybe also implementing float PhysicsBodyComponent.initialAngle and Vector2 PhysicsBodyComponent.initialPosition could make it easier to instantiate an object at the right position and rotation ?

BionicFelps commented 7 years ago

Probably related, I'm attempting to add physics bodies at runtime, which I think would be fairly common for making bullets, etc. But in this case I'm making a more complex physics object with a circle and a box to represent bipedal enemies, they'll turn their circle to go left or right, I've tried initiating this in Create() as well as in the second run of processEntity in my custom system, in all of my code the physics body exists, even after the functions leave scope, but at some point Overlap sets the physics body to null, and then later tries to use it, causing a catastrophic crash.

I've also tried doing this on the five thousandth iteration just to be sure any and all setup should be done.