@zhangxm
The first thing that I noticed is that you changed the “logic”.
The previous algorithm was:
- Scene -> updatePhysicsWorld()
- PhysicsWorld -> Iterate all over the bodies and update it
Your current algorithm is:
- Scene -> physicsManager();
- For every component: Physics2d::afterSimulation(): you get transform, scale and rotation in world coordinates and updates the physics code
- Simulate physics code
- For every component: Physics2d::beforeSimulation(): get scale, rotation and transform and update from physics and update the nodes
My questions are:
- Why do you need both
afterSimulation
and beforeSimulation
?
- Why are you trying to get the world coordinates of the sprites ? Does the old PhysicsBody/PhysicsWorld do that ?
- In any case, you are using a expensive 3d code to obtain the scale, rotation, transform. (eg: `Mat4::decompose() ). I would avoid using that function if possible… perhaps 2d version of it.
The fastest physics code is the one that is implemented in PhysicsSprite
, in particular this function:
void PhysicsSprite::syncPhysicsTransform() const
{
// Although scale is not used by physics engines, it is calculated just in case
// the sprite is animated (scaled up/down) using actions.
// For more info see: http://www.cocos2d-iphone.org/forum/topic/68990
cpVect rot = (_ignoreBodyRotation ? cpvforangle(-CC_DEGREES_TO_RADIANS(_rotationX)) : _CPBody->rot);
float x = _CPBody->p.x + rot.x * -_anchorPointInPoints.x * _scaleX - rot.y * -_anchorPointInPoints.y * _scaleY;
float y = _CPBody->p.y + rot.y * -_anchorPointInPoints.x * _scaleX + rot.x * -_anchorPointInPoints.y * _scaleY;
if (_ignoreAnchorPointForPosition)
{
x += _anchorPointInPoints.x;
y += _anchorPointInPoints.y;
}
float mat[] = { (float)rot.x * _scaleX, (float)rot.y * _scaleX, 0, 0,
(float)-rot.y * _scaleY, (float)rot.x * _scaleY, 0, 0,
0, 0, 1, 0,
x, y, 0, 1};
_transform.set(mat);
}
As you can see, the code is not decomposing the matrix and trying to get the world coordinates. This could be seen as a bug… or feature.
But in any case, we should be backwards compatible. I haven’t reviewed the v3.0 Physics code in depth, but if you are trying to replace that code, then you must be 100% backwards compatible with it… so I would use the algorithms that are being used by it.
UPDATE:
So, I believe that the old “PhysicsWorld” code actually uses the world coordinates for the nodes, but I’m not 100% sure.
The physicsworld code is very difficult to understand, and we must be 100% backwards compatible with it.
So, I guess that the 1st step is to understand it: what is the its design ? How does it work ?
And after that implement that design in the component.