cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.11k stars 7.05k forks source link

3.9beta0 2D Physics Wrapper Refactoring Changed Physics Behavior #14266

Open jimrange opened 8 years ago

jimrange commented 8 years ago

Looks like the refactoring of the 2D physics wrapper has resulted in changes to the mass, moment and size of shapes.

I haven't figured out the root cause of this issue, but this discussion shows before and after images from upgrading from 3.8.1 to 3.9beta0. In 3.8.1 the mass and moment of the vehicle were tuned so that the torque applied to the wheels would make the vehicle move responsively and the shapes and sprites were adjusted to properly model the race car. But after upgrading to 3.9beta0 the car barely moves as if the mass or moment drastically increased and the wheel shapes are much larger than the sprites.

No changes were made to the game source code other than upgrading from 3.8.1 to 3.9.

http://discuss.cocos2d-x.org/t/cocos2d-x-3-9-beta0-released/24507/24

Here is some code showing how I create some of the vehicle shapes:

void RaceCar::setupVehicleBody()
{
    _vehicleBody = PhysicsBody::createBox(_config.bodySize);
    _vehicleBody->setMass(_config.mass);

    auto vehicleShape = PhysicsShapeBox::create(_config.bodySize);
    vehicleShape->setMaterial(_config.material);
    _vehicleBody->addShape(vehicleShape);
    _vehicleBody->setMoment(vehicleShape->calculateDefaultMoment() * _config.momentFactor);
    _vehicleBody->setGravityEnable(true);
    _vehicleBody->setLinearDamping(_config.linearDamping);
    _vehicleBody->setAngularDamping(_config.angularDamping);
    _vehicleBody->setAngularVelocityLimit(_config.angularVelocityLimit);

    for (int i=0; i<_config.circleShapeCount; i++)
    {
        auto material = PhysicsMaterial(_config.circleShapes[i].density,
                                             _config.circleShapes[i].restitution,
                                             _config.circleShapes[i].friction);
        auto shape = PhysicsShapeCircle::create(_config.circleShapes[i].radius, material, _config.circleShapes[i].offset);
        _vehicleBody->addShape(shape);
    }

    _vehicleSprite = Sprite::createWithSpriteFrameName(_config.bodySpriteName);
    _vehicleSprite->setScale(_config.vehicleBodySpriteScale.x, _config.vehicleBodySpriteScale.y);
    _vehicleSprite->setPosition(_config.startPosition);
    _vehicleSprite->setPhysicsBody(_vehicleBody);
    CC_SAFE_RETAIN(_vehicleSprite);

    _vehicleBody->setGroup(_config.collisionGroup);

}
TheCodez commented 8 years ago

Maybe it's because update is called twice. As a result damping is applied twice https://github.com/cocos2d/cocos2d-x/issues/14231

jimrange commented 8 years ago

That could potentially be part of the issue I am seeing. But I am also seeing shapes getting sized differently with the same code. e.g. in 3.8.1 the shapes were one size, but now in 3.9beta0 the shapes are a different size with the identical game code.