Hi All,
I have using JBox2D 2.0.1 for some time now and noticed that the more recent release of the C++ Box2D has now incorporated kinematic bodies which seem to be very useful. I have looked through just about every ounce of the source code and I cannot spot anything terribly special about the kinematic body in the way it's handled. The only places in the code I ever see "kinematic" are in the b2Body class only to set the mass and invmass to zero. The hint I was getting from this was that the kinematic body was, effectively, just a static body that wasn't being called a static body. So to the code I went to test the theory and it seems like I'm not terribly far off, but some things don't work as they would with the kinematic body. For instance, the kinematic body is supposed to be moved by setting its velocity, but this won't work with "my" kinematic body. I can set the velocity and it holds course for exactly one time step before it stops (and changes its velocity back to 0,0). Also, it will "float" like a static body until a dynamic body comes into contact with it, then it will fall under gravity. When the contact is removed, the body will stop falling.
I was wondering in what ways does the engine treat the kinematic body differently from a static (or dynamic) body? In other words, what bits of code will I have to modify to make this new type work like the kinematic body?
Thanks,
Nathan
Edit: I have spotted the bit of code to fix the problem with the body falling when in contact with a dynamic body. Inside the Island.java class, there is a line in the block for integrating the velocities that says
I changed this to
I have also just found some code that needed to be added to ContactManager.java. Kinematic bodies should not collide with each other or with static bodies, so the following needs to be added to the pairAdded(...) function.
I am still trying to figure out what is causing the body to stop after a single time step when setting the linear velocity. I DO know that I can set the linear velocity if the mass is nonzero, but this will make it susceptible to being "pushed around" by dynamic bodies rather than keeping its static-like fortitude.
Edit Again:
I just figured out what was causing my newly created kinematic bodies to stop. They were falling asleep it seems. In Island.java, I changed a condition from
to
This block is under the higher condition of
Problem solved, it seems. I hope this can be useful for other people. 🙂
I have using JBox2D 2.0.1 for some time now and noticed that the more recent release of the C++ Box2D has now incorporated kinematic bodies which seem to be very useful. I have looked through just about every ounce of the source code and I cannot spot anything terribly special about the kinematic body in the way it's handled. The only places in the code I ever see "kinematic" are in the b2Body class only to set the mass and invmass to zero. The hint I was getting from this was that the kinematic body was, effectively, just a static body that wasn't being called a static body. So to the code I went to test the theory and it seems like I'm not terribly far off, but some things don't work as they would with the kinematic body. For instance, the kinematic body is supposed to be moved by setting its velocity, but this won't work with "my" kinematic body. I can set the velocity and it holds course for exactly one time step before it stops (and changes its velocity back to 0,0). Also, it will "float" like a static body until a dynamic body comes into contact with it, then it will fall under gravity. When the contact is removed, the body will stop falling.
I was wondering in what ways does the engine treat the kinematic body differently from a static (or dynamic) body? In other words, what bits of code will I have to modify to make this new type work like the kinematic body?
Thanks,
Nathan
Edit: I have spotted the bit of code to fix the problem with the body falling when in contact with a dynamic body. Inside the Island.java class, there is a line in the block for integrating the velocities that says
Code:
if( b.isStatic() ) continue;
I changed this to
Code:
if( !b.isDynamic() ) continue;
I have also just found some code that needed to be added to ContactManager.java. Kinematic bodies should not collide with each other or with static bodies, so the following needs to be added to the pairAdded(...) function.
Code:
if( body1.isStatic() && body2.isKinematic() ) return m_nullContact;
if( body2.isStatic() && body1.isKinematic() ) return m_nullContact;
if( body1.isKinematic() && body2.isKinematic() ) return m_nullContact;
if( body2.isStatic() && body1.isKinematic() ) return m_nullContact;
if( body1.isKinematic() && body2.isKinematic() ) return m_nullContact;
I am still trying to figure out what is causing the body to stop after a single time step when setting the linear velocity. I DO know that I can set the linear velocity if the mass is nonzero, but this will make it susceptible to being "pushed around" by dynamic bodies rather than keeping its static-like fortitude.
Edit Again:
I just figured out what was causing my newly created kinematic bodies to stop. They were falling asleep it seems. In Island.java, I changed a condition from
Code:
if (b.m_invMass == 0.0f) continue;
to
Code:
if( b.isStatic() ) continue;
This block is under the higher condition of
Code:
if (allowSleep){...}
Problem solved, it seems. I hope this can be useful for other people. 🙂