Example #1
0
void PhysicsBody::removeAllShapes(bool reduceMassAndMoment/* = true*/)
{
    for (auto& child : _shapes)
    {
        PhysicsShape* shape = dynamic_cast<PhysicsShape*>(child);
        
        // deduce the area, mass and moment
        // area must update before mass, because the density changes depend on it.
        if (reduceMassAndMoment)
        {
            _area -= shape->getArea();
            addMass(-shape->getMass());
            addMoment(-shape->getMoment());
        }
        
        if (_world)
        {
            _world->removeShape(shape);
        }
        
        // set shape->_body = nullptr make the shape->setBody will not trigger the _body->removeShape function call.
        shape->_body = nullptr;
        shape->setBody(nullptr);
    }
    
    _shapes.clear();
}
Example #2
0
void PhysicsBody::removeShape(PhysicsShape* shape, bool reduceMassAndMoment/* = true*/)
{
    if (_shapes.getIndex(shape) != -1)
    {
        // deduce the area, mass and moment
        // area must update before mass, because the density changes depend on it.
        if (reduceMassAndMoment)
        {
            _area -= shape->getArea();
            addMass(-shape->getMass());
            addMoment(-shape->getMoment());
        }
        
        //remove
        if (_world)
        {
            _world->removeShape(shape);
        }
        
        // set shape->_body = nullptr make the shape->setBody will not trigger the _body->removeShape function call.
        shape->_body = nullptr;
        shape->setBody(nullptr);
        _shapes.eraseObject(shape);
    }
}
Example #3
0
PhysicsShape* PhysicsBody::addShape(PhysicsShape* shape, bool addMassAndMoment/* = true*/)
{
    if (shape == nullptr) return nullptr;
    
    // add shape to body
    if (_shapes.getIndex(shape) == -1)
    {
        shape->setBody(this);
        
        // calculate the area, mass, and density
        // area must update before mass, because the density changes depend on it.
        if (addMassAndMoment)
        {
            _area += shape->getArea();
            addMass(shape->getMass());
            addMoment(shape->getMoment());
        }
        
        if (_world && cpBodyGetSpace(_cpBody))
        {
            _world->addShape(shape);
        }
        
        _shapes.pushBack(shape);
    }
    
    return shape;
}
Example #4
0
void PhysicsBody::setScale(float scaleX, float scaleY)
{
    for (auto& shape : _shapes)
    {
        _area -= shape->getArea();
        if (!_massSetByUser)
            addMass(-shape->getMass());
        if (!_momentSetByUser)
            addMoment(-shape->getMoment());
        
        shape->setScale(scaleX, scaleY);
        
        _area += shape->getArea();
        if (!_massSetByUser)
            addMass(shape->getMass());
        if (!_momentSetByUser)
            addMoment(shape->getMoment());
    }
}
Example #5
0
void PhysicsBody::removeShape(PhysicsShape* shape)
{
    auto it = std::find(_shapes.begin(), _shapes.end(), shape);
    
    if (it != _shapes.end())
    {
        // deduce the area, mass and moment
        // area must update before mass, because the density changes depend on it.
        _area -= shape->getArea();
        addMass(-shape->getMass());
        addMoment(-shape->getMoment());
        
        //remove
        if (_world)
        {
            _world->removeShape(shape);
        }
        _shapes.erase(it);
        shape->setBody(nullptr);
        shape->release();
    }
}
Example #6
0
void Game2::createtrap(double x, double y, double z, std::string name){
	
	//create trap
	Trap *_trampa = Trap::create();
	_trampa->setPosition3D(Vec3(x, y, z));
	_trampa->setName(name);
	_trampas->addObject(_trampa);


	//create body
	auto _body = PhysicsBody::createEdgeBox(Size(35, 20), PhysicsMaterial(10, 0, 0.9f), 1.0, Vec2(-5, 10));

	_body->setContactTestBitmask(true);
	_body->setDynamic(false);
	_body->setRotationEnable(false);
	_body->addMass(30.0);
	_body->addMoment(2.0);
	_body->setLinearDamping(0.8f);
	_trampa->setPhysicsBody(_body);
	addChild(_trampa);

}
Example #7
0
void PhysicsBody::addShape(PhysicsShape* shape)
{
    if (shape == nullptr) return;
    
    // add shape to body
    if (std::find(_shapes.begin(), _shapes.end(), shape) == _shapes.end())
    {
        shape->setBody(this);
        _shapes.push_back(shape);
        
        // calculate the area, mass, and desity
        // area must update before mass, because the density changes depend on it.
        _area += shape->getArea();
        addMass(shape->getMass());
        addMoment(shape->getMoment());
        
        if (_world != nullptr)
        {
            _world->addShape(shape);
        }
        
        shape->retain();
    }
}
Example #8
0
void Game2::createplatform(double x, double y, double z, double scale, double bodyscalex, double bodyscaley, double xoffset, double yoffset, std::string name){
	
	//create platform
	Platform *_plataforma = Platform::create();
	_plataforma->setScaleX(scale);
	_plataforma->setScaleY(10);
	_plataforma->setName(name);
	_plataforma->setPosition3D(Vec3(x, y, z));
	_plataformas->addObject(_plataforma);


	//create body
	auto _body = PhysicsBody::createEdgeBox(Size(bodyscalex, bodyscaley), PhysicsMaterial(10, 0, 0.9f), 1.0, Vec2(xoffset, yoffset));

	_body->setContactTestBitmask(true);
	_body->setDynamic(false);
	_body->setRotationEnable(false);
	_body->addMass(30.0);
	_body->addMoment(2.0);
	_body->setLinearDamping(0.8f);
	_plataforma->setPhysicsBody(_body);
	addChild(_plataforma);

}