コード例 #1
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 desity
        // 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 != nullptr)
        {
            _world->addShape(shape);
        }
        
        _shapes.pushBack(shape);
        
        if (_group != CP_NO_GROUP && shape->getGroup() == CP_NO_GROUP)
        {
            shape->setGroup(_group);
        }
    }
    
    return shape;
}
コード例 #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);
    }
}
コード例 #3
0
ファイル: CSubStructure.cpp プロジェクト: ngc92/stargame
	void CSubStructure::onInit(IGameObject& object, IGameWorld& world)
	{
		// init all components
		foreachComponent([](IComponent& component, IGameObject& object){component.init(object);}, std::ref(object));

		// need to write the component subobject loop ourselves here, because we need the shared_ptr
		// build the physic body
		for(auto& cell : mCells)
		{
			auto fixture = physics::Fixture::create(object.getBody(), cell->shape(), 10.f);
			fixture.setRestitution(0.1);
			fixture.setFriction(0.5);
			for(std::size_t i = 0; i < cell->component_count(); ++i)
			{
				addChild(cell->getComponent(i));
				fixture.addMass(cell->getComponent(i)->weight());
			}
		}

		// add the armour segments to the object definition
		for(unsigned i = 0; i < mArmour.size(); ++i)
		{
			auto pob = std::make_shared<property::CPropertyObject>("armour" + to_string(i));
			addChild(pob);
			pob->addChild(mArmour[i].armour->getSharedPropertyObject());
			pob->addProperty( property::CProperty::create("p1", pob.get(), mArmour[i].p1) );
			pob->addProperty( property::CProperty::create("p2", pob.get(), mArmour[i].p2) );
		}
	}
コード例 #4
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 && _cpBody->CP_PRIVATE(space))
        {
            _world->addShape(shape);
        }

        _shapes.pushBack(shape);
    }

    return shape;
}
コード例 #5
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();
}
コード例 #6
0
ファイル: CCPhysicsBody.cpp プロジェクト: bonlai/3kaigame
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());
    }
}
コード例 #7
0
void MassSpring1DRepresentation::init1D(
	const std::vector<Vector3d> nodes,
	std::vector<size_t> nodeBoundaryConditions,
	double totalMass,
	double stiffnessStretching, double dampingStretching,
	double stiffnessBending, double dampingBending)
{
	std::shared_ptr<SurgSim::Math::OdeState> state;
	state = std::make_shared<SurgSim::Math::OdeState>();
	state->setNumDof(getNumDofPerNode(), nodes.size());

	SURGSIM_ASSERT(nodes.size() > 0) << "Number of nodes incorrect: " << nodes.size();

	// Initialize the nodes position, velocity and mass
	// Note: no need to apply the initialPose here, initialize will take care of it !
	for (size_t massId = 0; massId < nodes.size(); massId++)
	{
		addMass(std::make_shared<Mass>(totalMass / static_cast<double>(nodes.size())));

		SurgSim::Math::setSubVector(nodes[massId], massId, 3, &state->getPositions());
	}

	// Initialize the stretching springs
	if (stiffnessStretching || dampingStretching)
	{
		for (size_t massId = 0; massId < nodes.size() - 1; massId++)
		{
			addSpring(createLinearSpring(state, massId, massId + 1, stiffnessStretching, dampingStretching));
		}
	}

	// Initialize the bending springs
	if (stiffnessBending || dampingBending)
	{
		for (size_t massId = 0; massId < nodes.size() - 2; massId++)
		{
			addSpring(createLinearSpring(state, massId, massId + 2, stiffnessBending, dampingBending));
		}
	}

	// Sets the boundary conditions
	for (auto boundaryCondition = std::begin(nodeBoundaryConditions);
		 boundaryCondition != std::end(nodeBoundaryConditions);
		 boundaryCondition++)
	{
		state->addBoundaryCondition(*boundaryCondition);
	}

	// setInitialState: Initialize all the states + apply initialPose if any
	setInitialState(state);
}
コード例 #8
0
ファイル: PlayerFragment.hpp プロジェクト: tyamgin/AiCup
	vector<PlayerFragment> burst(const Virus &virus, int max_fragment_id, int yet_cnt)
	{
		vector<PlayerFragment> fragments;
		double dist = getDistanceTo(virus);
		double dy = y - virus.y, dx = x - virus.x;
		double angle = atan2(y, x);

		if (dist > 0) 
		{
			angle = asin(dy / dist);
			if (dx < 0)
				angle = M_PI - angle;
		}

		mass += BURST_BONUS;

		int new_frags_cnt = int(mass / MIN_BURST_MASS) - 1;

		new_frags_cnt = min(new_frags_cnt, _restFragmentsCount(yet_cnt));

		double new_mass = mass / (new_frags_cnt + 1);

		for (int I = 0; I < new_frags_cnt; I++) 
		{
			int new_fId = max_fragment_id + I + 1;

			PlayerFragment new_fragment;
			new_fragment.x = x;
			new_fragment.y = y;
			new_fragment.addMass(new_mass);
			new_fragment.playerId = playerId;
			new_fragment.fragmentId = new_fId;

			double burst_angle = angle - BURST_ANGLE_SPECTRUM / 2 + I * BURST_ANGLE_SPECTRUM / new_frags_cnt;
			new_fragment.speed = ::Point::byAngle(burst_angle) * BURST_START_SPEED;
			new_fragment.isFast = true;
			new_fragment.ttf = Config::TICKS_TIL_FUSION;

			fragments.push_back(new_fragment);
		}
		speed = ::Point::byAngle(angle + BURST_ANGLE_SPECTRUM / 2) * BURST_START_SPEED;
		isFast = true;

		fragmentId = max_fragment_id + new_frags_cnt + 1;
		addMass(new_mass - mass);
		ttf = Config::TICKS_TIL_FUSION;
		return fragments;
	}
コード例 #9
0
ファイル: masscalc.cpp プロジェクト: sebhtml/rTANDEM
/*
 * Mass calculator class
 *     Calculates molecular masses based on atomic masses.
 *     Atomic masses come from http://www.unimod.org/unimod_help.html.
 */
masscalc::masscalc(massType _t)
{
	m_massType = _t;
	addMass("H", 1.007825035, 1.00794);
	addMass("O", 15.99491463, 15.9994);
	addMass("N", 14.003074, 14.0067);
	addMass("C", 12.0, 12.0107);
	addMass("S", 31.9720707, 32.065);
	addMass("P", 30.973762, 30.973761);
}
コード例 #10
0
ファイル: PlayerFragment.hpp プロジェクト: tyamgin/AiCup
	Ejection eject()
	{
		auto e = *this + speed.normalized() * (radius + 1);
		
		Ejection new_eject;
		new_eject.x = e.x;
		new_eject.y = e.y;
		new_eject.radius = EJECT_RADIUS;
		new_eject.mass = EJECT_MASS;
		new_eject.ownerPlayerId = playerId;
		new_eject.id = 0;
		new_eject.speed = speed.take(EJECT_START_SPEED);

		addMass(-EJECT_MASS);
		return new_eject;
	}
コード例 #11
0
ファイル: PlayerFragment.hpp プロジェクト: tyamgin/AiCup
	PlayerFragment split(int &max_fragment_id) 
	{
		double new_mass = mass / 2;

		PlayerFragment new_player;
		new_player.x = x;
		new_player.y = y;
		new_player.addMass(new_mass);
		new_player.speed = speed.take(SPLIT_START_SPEED);
		new_player.isFast = true;
		new_player.playerId = playerId;
		new_player.fragmentId = ++max_fragment_id;
		new_player.ttf = Config::TICKS_TIL_FUSION;
		
		fragmentId = ++max_fragment_id;
		ttf = Config::TICKS_TIL_FUSION;
		addMass(-new_mass);

		return new_player;
	}
コード例 #12
0
ファイル: Game2Scene.cpp プロジェクト: drain111/Mr.-Sandmanv2
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);

}
コード例 #13
0
ファイル: CCPhysicsBody.cpp プロジェクト: chling/artemis
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();
    }
}
コード例 #14
0
ファイル: Game2Scene.cpp プロジェクト: drain111/Mr.-Sandmanv2
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);

}
コード例 #15
0
ファイル: CCPhysicsBody.cpp プロジェクト: chling/artemis
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();
    }
}
コード例 #16
0
ファイル: PlayerFragment.hpp プロジェクト: tyamgin/AiCup
	void shrink() 
	{
		addMass(-(mass - MIN_SHRINK_MASS) * SHRINK_FACTOR);
	}
コード例 #17
0
ファイル: ADRAssembler.hpp プロジェクト: erianthus/lifev
 /*!
   This method adds the mass matrix times the coefficient
   (whose default value is 1.0) to the matrix passed in argument.
   @Remark The matrix is NOT finalized, you have to call globalAssemble
   outside this method when the matrix is finished.
  */
 inline void addMass (matrix_ptrType matrix, const Real& coefficient = 1.0)
 {
     addMass (matrix, coefficient, 0, 0);
 }
コード例 #18
0
ファイル: Game2Scene.cpp プロジェクト: drain111/Mr.-Sandmanv2
// on "init" you need to initialize your instance
bool Game2::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
	def = CCUserDefault::sharedUserDefault();



		
	
#pragma region Creaciondevariables

	_chara = Character::create();
	addChild(_chara);
	Point center = Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y);
	i = 1;
	j = 1;
	k = 1;
	free = true;

	moverderecha = false;
	moverizq = false;
	arriba = false;
	rotar = false;
	bool abrir = false;
	puntuacion = 0;
	_chara->vidas = def->getIntegerForKey("vidas");

#pragma endregion
	muerto = false;
	camera = Camera::createPerspective(60,visibleSize.width / visibleSize.height,1, 1000);
	camera->setPosition3D(Vec3(0, 0, 500));
	_plataformas = Array::create();
	_plataformas->retain();
	_trampas = Array::create();
	_trampas->retain();
	_chara->setPosition3D(Vec3(90.0, 90.0, 0.0));
	_chara->setScale(1.0);
	camera->lookAt(Vec3(0, 0, 0), Vec3(0, 1, 0));
	addChild(camera);
	camera->setScale(3);

	createplatform(200.0, -300.0, 0.0, 70.0, 1400, 1, 350, 65, "plataforma");
	createtrap(300,-230,0,"trampa");
	createtrap(335,-230,0,"trampa");
	createtrap(370,-230,0,"trampa");
	createplatform(600, -50, 0.0, 8.0, 160, 1, 32, 65, "plataforma");
	createtrap(700,-230,0,"trampa");
	createtrap(735,-230,0,"trampa");
	createtrap(770,-230,0,"trampa");
	createtrap(805,-230,0,"trampa");
	createtrap(840,-230,0,"trampa");
	createtrap(875,-230,0,"trampa");
	createtrap(910,-230,0,"trampa");
	createtrap(945,-230,0,"trampa");
	createtrap(980,-230,0,"trampa");
	createplatform(1000, -50, 0.0, 8.0, 160, 1, 32, 65, "plataforma");
	createplatform(1500, -50, 0.0, 8.0, 160, 1, 32, 65, "plataforma");
	createtrap(1600,15,0,"trampa");
	createplatform(2000, -50, 0.0, 8.0, 160, 1, 32, 65, "plataforma");
	createplatform(2500, -300.0, 0.0, 85.0, 1700, 1, 350, 65, "plataforma");
	createtrap(2700,-230,0,"trampa");
	createtrap(2735,-230,0,"trampa");
	createtrap(2770,-230,0,"trampa");
	createtrap(2805,-230,0,"trampa");
	createtrap(2840,-230,0,"trampa");
	createtrap(2875,-230,0,"trampa");
	createtrap(2910,-230,0,"trampa");
	createtrap(2945,-230,0,"trampa");
	createtrap(2980,-230,0,"trampa");
	createtrap(3200,-230,0,"trampa");
	createtrap(3310,-230,0,"trampa");
	createtrap(3520,-230,0,"trampa");
	createtrap(3700,-230,0,"trampa");


	createplatform(4200.0, -300.0, 0.0, 8.0, 160, 1, 32, 65, "plataforma");

	esfera = Sprite3D::create("char/esfera.c3t");
	esfera->setPosition3D(Vec3(4300.0, 200.0, 0));
	auto _bodyesf = PhysicsBody::createCircle(128, PHYSICSBODY_MATERIAL_DEFAULT, Vec2(-80, -80));
	esfera->setScale(4);
	_bodyesf->setContactTestBitmask(true);
	_bodyesf->setDynamic(false);
	_bodyesf->setRotationEnable(false);
	_bodyesf->addMass(30.0);
	
	
	esfera->setName("esfera");
	esfera->setPhysicsBody(_bodyesf);
	addChild(esfera);

	auto keyboardListener = EventListenerKeyboard::create();
	keyboardListener->onKeyPressed = CC_CALLBACK_2(Game2::onKeyPresed, this);
	keyboardListener->onKeyReleased = CC_CALLBACK_2(Game2::onKeyReleased, this);
	this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(keyboardListener, this);
	this->scheduleUpdate();



	//Physics

	auto contactListener = EventListenerPhysicsContact::create();
	contactListener->onContactBegin = CC_CALLBACK_1(Game2::onContactBegin, this);
	this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);

	hud = new HUD(_chara->vidas, true,800);
	this->addChild(hud);
	
	

	
	//xinput


	auto console = Director::getInstance()->getConsole();
	//console->listenOnTCP(6113);
	
	struct Console::Command changeforce = {
		"changeforce",
		"Cambia la fuerza del salto",
		[this](int fd, const std::string& args) {
			if (args.length() == 0)
			{
				
				
			}
			else
			{			
				createplatform((int)args[0], (int)args[1], (int)args[2], (int)args[3], (int)args[4], (int)args[5], (int)args[6], (int)args[7], args);
			}
		} };
	

	console->addCommand(changeforce);
    return true;
	
}