static duk_ret_t PhysicsWorld_SetMaxSubSteps_int(duk_context* ctx)
{
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    int steps = (int)duk_require_number(ctx, 0);
    thisObj->SetMaxSubSteps(steps);
    return 0;
}
Example #2
0
void PhysicsHelper::CreateTetrahedron(PhysicsWorld& world)
{
	Particle* p0 = new Particle(Random::GetF(-kParticleSpawnVariance, kParticleSpawnVariance), Random::GetF(kParticleHeight, kParticleSpawnVariance + kParticleHeight), Random::GetF(-kParticleSpawnVariance, kParticleSpawnVariance), kRadius, kInverseMass);
	Particle* p1 = new Particle(Random::GetF(-kParticleSpawnVariance, kParticleSpawnVariance), Random::GetF(kParticleHeight, kParticleSpawnVariance + kParticleHeight), Random::GetF(-kParticleSpawnVariance, kParticleSpawnVariance), kRadius, kInverseMass);
	Particle* p2 = new Particle(Random::GetF(-kParticleSpawnVariance, kParticleSpawnVariance), Random::GetF(kParticleHeight, kParticleSpawnVariance + kParticleHeight), Random::GetF(-kParticleSpawnVariance, kParticleSpawnVariance), kRadius, kInverseMass);
	Particle* p3 = new Particle(Random::GetF(-kParticleSpawnVariance, kParticleSpawnVariance), Random::GetF(kParticleHeight, kParticleSpawnVariance + kParticleHeight), Random::GetF(-kParticleSpawnVariance, kParticleSpawnVariance), kRadius, kInverseMass);

	p0->SetVelocity(Random::GetF(-kParticleVelocityRange, kParticleVelocityRange)*kTimeStep, Random::GetF(0.0f, kParticleVelocityRange)*kTimeStep, Random::GetF(-kParticleVelocityRange, kParticleVelocityRange)*kTimeStep);
	p1->SetVelocity(Random::GetF(-kParticleVelocityRange, kParticleVelocityRange)*kTimeStep, Random::GetF(0.0f, kParticleVelocityRange)*kTimeStep, Random::GetF(-kParticleVelocityRange, kParticleVelocityRange)*kTimeStep);
	p2->SetVelocity(Random::GetF(-kParticleVelocityRange, kParticleVelocityRange)*kTimeStep, Random::GetF(0.0f, kParticleVelocityRange)*kTimeStep, Random::GetF(-kParticleVelocityRange, kParticleVelocityRange)*kTimeStep);
	p3->SetVelocity(Random::GetF(-kParticleVelocityRange, kParticleVelocityRange)*kTimeStep, Random::GetF(0.0f, kParticleVelocityRange)*kTimeStep, Random::GetF(-kParticleVelocityRange, kParticleVelocityRange)*kTimeStep);

	world.AddParticle(p0);
	world.AddParticle(p1);
	world.AddParticle(p2);
	world.AddParticle(p3);

	Spring* s0 = new Spring(p0, p1, 1.0f);
	Spring* s1 = new Spring(p1, p2, 1.0f);
	Spring* s2 = new Spring(p2, p3, 1.0f);
	Spring* s3 = new Spring(p3, p0, 1.0f);
	Spring* s4 = new Spring(p0, p2, 1.0f);
	Spring* s5 = new Spring(p1, p3, 1.0f);

	world.AddConstraint(s0);
	world.AddConstraint(s1);
	world.AddConstraint(s2);
	world.AddConstraint(s3);
	world.AddConstraint(s4);
	world.AddConstraint(s5);
}
static duk_ret_t PhysicsWorld_MaxSubSteps(duk_context* ctx)
{
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    int ret = thisObj->MaxSubSteps();
    duk_push_number(ctx, ret);
    return 1;
}
static duk_ret_t PhysicsWorld_Gravity(duk_context* ctx)
{
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    float3 ret = thisObj->Gravity();
    PushValueObjectCopy<float3>(ctx, ret, float3_ID, float3_Finalizer);
    return 1;
}
static duk_ret_t PhysicsWorld_SetGravity_float3(duk_context* ctx)
{
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    float3& gravity = *GetCheckedValueObject<float3>(ctx, 0, float3_ID);
    thisObj->SetGravity(gravity);
    return 0;
}
static duk_ret_t PhysicsWorld_IsDebugGeometryEnabled(duk_context* ctx)
{
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    bool ret = thisObj->IsDebugGeometryEnabled();
    duk_push_boolean(ctx, ret);
    return 1;
}
static duk_ret_t PhysicsWorld_SetDebugGeometryEnabled_bool(duk_context* ctx)
{
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    bool enable = duk_require_boolean(ctx, 0);
    thisObj->SetDebugGeometryEnabled(enable);
    return 0;
}
int main()
{
    PhysicsWorld world;

    auto bigSphereShape = std::make_shared<SphereCollisionShape>(5.0f);
    auto smallSphereShape = std::make_shared<SphereCollisionShape>(1.0f);

    auto bigSphere = std::make_shared<RigidBody>(bigSphereShape);
    bigSphere->transform().setPosition({0, -bigSphereShape->radius(), 0});
    bigSphere->setMass(3.0f);

    auto smallSphere = std::make_shared<RigidBody>(smallSphereShape);
    smallSphere->transform().setPosition({0, 10, 0});

    smallSphere->setLinearVelocity({0, -8, 0});

    world.addRigidBody(bigSphere);
    world.addRigidBody(smallSphere);

    for (auto i = 0; i < 2 * 60; i++) {
        world.update(0.016f);
        std::cout << i << ": SmallBox @ " << smallSphere->transform().position() << "; BigBox @ " << bigSphere->transform().position() << std::endl;
    }

    return 0;
}
static duk_ret_t PhysicsWorld_SetRunning_bool(duk_context* ctx)
{
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    bool enable = duk_require_boolean(ctx, 0);
    thisObj->SetRunning(enable);
    return 0;
}
Example #10
0
		void DeSpawned()
		{
			// clear constraints
			for(unsigned int i = 0; i < constraints.size(); ++i)
			{
				ConeTwistConstraint* c = constraints[i];
				physics->RemoveConstraint(c);

				c->Dispose();
				delete c;
			}
			constraints.clear();

			// clear rigid bodies
			for(unsigned int i = 0; i < rigid_bodies.size(); ++i)
			{
				RigidBodyInfo* body = rigid_bodies[i];
				if(physics != NULL)
					physics->RemoveRigidBody(body);

				body->DisposePreservingCollisionShape();
				delete body;
			}
			rigid_bodies.clear();
		}
Example #11
0
    void PhysicsBodyObject::SetActive( bool active )
    {
        if( active == Active() )
            return;

        rsk::ActiveObject::SetActive( active );
    //
    //  Encapsulate RSKMAP_ITERATE; duplicate them so only what's needed is run
        PhysicsWorld* world = gGame->GetWorld();
        if( active )
        {
            RSKMAP_ITERATE_VALUE_NAMED( m_Detectors, slot )
            {
                if( slot->active )
                {
                    world->AddBody( slot->area.Self() );
                    CommitDetectorActive( *slot, active );
                }
            }
            return;
        }

        if( world )
        {
            RSKMAP_ITERATE_VALUE_NAMED( m_Detectors, slot )
            {
                CommitDetectorActive( *slot, active );
                world->RemoveBody( slot->area->GetID() );
            }
            return;
        }
static duk_ret_t PhysicsWorld_IsClient(duk_context* ctx)
{
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    bool ret = thisObj->IsClient();
    duk_push_boolean(ctx, ret);
    return 1;
}
static duk_ret_t PhysicsWorld_PhysicsUpdatePeriod(duk_context* ctx)
{
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    float ret = thisObj->PhysicsUpdatePeriod();
    duk_push_number(ctx, ret);
    return 1;
}
static duk_ret_t PhysicsWorld_SetPhysicsUpdatePeriod_float(duk_context* ctx)
{
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    float updatePeriod = (float)duk_require_number(ctx, 0);
    thisObj->SetPhysicsUpdatePeriod(updatePeriod);
    return 0;
}
Example #15
0
void PhysicsWorld::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data)
{
    PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
    PhysicsContact* contact = static_cast<PhysicsContact*>(arb->data);
    
    world->collisionSeparateCallback(*contact);
    
    delete contact;
}
Example #16
0
 MasterEntity::~MasterEntity()
 {
     if( gGame.Exists() )
     {
         PhysicsWorld* world = gGame->GetWorld();
         if( world )
             world->RemoveEntity( GetEntityID() );
     }
 }
void PhysicsDrawer::drawWorld(const PhysicsWorld& world)
{
	auto lock = world.lockBtWorld();

	btDynamicsWorld& btWorld = *const_cast<btDynamicsWorld*>(world.getBtWorld());

	btWorld.setDebugDrawer(&m_debugDraw);
	btWorld.debugDrawWorld();
}
Example #18
0
PhysicsWorld* PhysicsWorld::construct(Scene& scene)
{
    PhysicsWorld * world = new PhysicsWorld();
    if(world && world->init(scene))
    {
        return world;
    }
    
    CC_SAFE_DELETE(world);
    return nullptr;
}
Scene* HelloGravity::createScene() {
    auto scene = Scene::createWithPhysics();
    PhysicsWorld* world = scene->getPhysicsWorld();
    world->setGravity(Vec2(0, -500));
    world->setSpeed(1.0f);
    world->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
    auto layer = HelloGravity::create();
    scene->addChild(layer);

    return scene;
}
Example #20
0
bool TMenu::init()
{
	Layer::init();

	Menu * menu = Menu::create();
	addChild(menu);
	
	for (int i = 0; i < sizeof(title) / sizeof(*title); ++i)
	{
		MenuItemFont * item = MenuItemFont::create(title[i], [](Ref * sender){
			MenuItem * item = (MenuItem *)sender;
			int i = item->getTag() - 1000;
			Layer * l = NULL;
			if (title[i] == "T01CPP11") l = T01CPP11::create();
			else if (title[i] == "T02Vector") l = T02Vector::create();
			else if (title[i] == "T03Map") l = T03Map::create();
			else if (title[i] == "T04Label") l = T04Label::create();
			else if (title[i] == "T05Touch") l = T05Touch::create();
			else if (title[i] == "T06Box2D") l = T06Box2D::create();
			else if (title[i] == "T07PhysicsWorld") l = T07PhysicsWorld::create();
			else if (title[i] == "T08RayCast") l = T08RayCast::create();
			else if (title[i] == "T09Joint") l = T09Joint::create();
			else if (title[i] == "T10MultiShapes") l = T10MultiShapes::create();
			else if (title[i] == "T11FlyppyBird") l = T11FlyppyBird::create();
			if (l != NULL)
			{
				TBack * b = TBack::create();
				//Scene * s = Scene::create();
				Scene * s = Scene::createWithPhysics(); // 3.x ÎïÀíÊÀ½ç
				PhysicsWorld * world = s->getPhysicsWorld();
				world->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
				s->addChild(b);
				s->addChild(l);
				Director::getInstance()->pushScene(s);

			}
		});
		menu->addChild(item);
		item->setTag(1000 + i);
	}

	menu->alignItemsVertically();

	// ´¥Ãþ
	auto ev = EventListenerTouchOneByOne::create();
	//ev->onTouchBegan = std::bind(&TMenu::touchBegan, this, std::placeholders::_1, std::placeholders::_2);
	ev->onTouchBegan = CC_CALLBACK_2(TMenu::touchBegan, this);
	ev->onTouchMoved = [&](Touch * touch, Event *){
		setPositionY(getPositionY() + touch->getDelta().y);
	};
	_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

	return true;
}
Example #21
0
Scene*InGameLayer::scene()
{
	auto sc=Scene::createWithPhysics();
	PhysicsWorld* phyWorld = sc->getPhysicsWorld();
	//phyWorld->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
	//0,0不受到重力的影响
	phyWorld->setGravity(Vect(0,0));
	auto lay=InGameLayer::create();
	sc->addChild(lay);
	return sc;
}
Example #22
0
 PhysicsBodyObject::~PhysicsBodyObject()
 {
     if( gGame.Exists() )
     {
         PhysicsWorld* world = gGame->GetWorld();
         if( world )
         {
             RSKMAP_ITERATE( m_Detectors )
                 world->RemoveBody( iter->second.area->GetID() );
         }
     }
 }
Example #23
0
PhysicsWorld* PhysicsWorld::create()
{
    PhysicsWorld* instance = new PhysicsWorld();

    if(instance && instance->init())
    {
        return instance;
    }

    delete instance;
    return NULL;
}
void BtPlugin::destroyWorld( const String &worldName )
{
   Map<StringNoCase, PhysicsWorld*>::Iterator iter = mPhysicsWorldLookup.find( worldName );
   if ( iter == mPhysicsWorldLookup.end() )
      return;

   PhysicsWorld *world = (*iter).value;
   world->destroyWorld();
   delete world;
   
   mPhysicsWorldLookup.erase( iter );
}
PhysicsWorld* PhysicsWorld::construct(Scene* scene)
{
    PhysicsWorld * world = new (std::nothrow) PhysicsWorld();
    if (world && world->init())
    {
        world->_scene = scene;
        world->_eventDispatcher = scene->getEventDispatcher();
        return world;
    }

    CC_SAFE_DELETE(world);
    return nullptr;
}
static duk_ret_t PhysicsWorld_Raycast_float3_float3_float_int_int(duk_context* ctx)
{
    int numArgs = duk_get_top(ctx);
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    float3& origin = *GetCheckedValueObject<float3>(ctx, 0, float3_ID);
    float3& direction = *GetCheckedValueObject<float3>(ctx, 1, float3_ID);
    float maxDistance = (float)duk_require_number(ctx, 2);
    int collisionGroup = numArgs > 3 ? (int)duk_require_number(ctx, 3) : -1;
    int collisionMask = numArgs > 4 ? (int)duk_require_number(ctx, 4) : -1;
    PhysicsRaycastResult ret = thisObj->Raycast(origin, direction, maxDistance, collisionGroup, collisionMask);
    PushValueObjectCopy<PhysicsRaycastResult>(ctx, ret, PhysicsRaycastResult_ID, PhysicsRaycastResult_Finalizer);
    return 1;
}
void PhysicsPlugin::_debugDraw( SceneManager *graph, const SceneRenderState *state )
{
   // We only debug draw in the diffuse pass if we have a physics object.
   if ( !PHYSICSMGR || !state->isDiffusePass() )
      return;

   // Render the server by default... else the client.
   PhysicsWorld *world = PHYSICSMGR->getWorld( smServerWorldName );
   if ( !world )
      world = PHYSICSMGR->getWorld( smClientWorldName );

   if ( world )
      world->onDebugDraw( state );
}
Example #28
0
void PlygonDrawer::playBlastAnim(Point2i pos, enNodeColor color) 
{
	auto convt = m_graph->m_coordCvt;
	Vec2 coord = convt.getCoord(pos);

	auto scene = Director::getInstance()->getRunningScene();
	PhysicsWorld* phyWld = scene->getPhysicsWorld();

	for (int i = 0; i < 5; ++i)
	{
		Sprite* spStar = BeadFactory::createBlastStar(color, 0.2 + 0.1 * i);
		float radius = spStar->getContentSize().width/2 * spStar->getScaleX();
		spStar->retain();
		spStar->setPosition(coord);
		/*spStar->runAction( Sequence::createWithTwoActions(MoveBy::create(2.0f, dir),
				CallFunc::create([=]() {
				spStar->removeFromParent();
				spStar->release();
		})));*/
		spStar->runAction( Sequence::createWithTwoActions(DelayTime::create(15.0f),
			CallFunc::create([=]() {
				spStar->removeFromParent();
				spStar->release();
		})));
#define GravityAmpl 50
		PhysicsMaterial phyMat = PHYSICSBODY_MATERIAL_DEFAULT; phyMat.density = 200.0f;
		auto body = PhysicsBody::createCircle(radius, phyMat);
		Vec2 dir = ccp(rand()%100 - 50, rand()%100 - 50);
		dir.normalize(); dir = dir * body->getMass() * 8.0f * GravityAmpl;
		Vect force(dir);
		body->applyImpulse(force);
		body->setDynamic(true);
		//body->setCategoryBitmask(0x04);
		//body->setCollisionBitmask(0x08);
		body->setGroup(-1);
		body->setGravityEnable(true);

		spStar->setPhysicsBody(body);
		m_tileMap->addChild(spStar, 100);
	}

	static bool isPhyInit = false;
	if (! isPhyInit) {
		isPhyInit = true;
		phyWld->setGravity(Vect(0.0f, -9.8f * GravityAmpl));
		//phyWld->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
		phyWld->setUpdateRate(2.5f);
	}
}
Example #29
0
void Node::setPhysicsBody(PhysicsBody* body)
{
    if (body != nullptr)
    {
        body->_node = this;
        body->retain();
        
        // physics rotation based on body position, but node rotation based on node anthor point
        // it cann't support both of them, so I clear the anthor point to default.
        if (!getAnchorPoint().equals(Vec2::ANCHOR_MIDDLE))
        {
            CCLOG("Node warning: setPhysicsBody sets anchor point to Vec2::ANCHOR_MIDDLE.");
            setAnchorPoint(Vec2::ANCHOR_MIDDLE);
        }
    }
    
    if (_physicsBody != nullptr)
    {
        PhysicsWorld* world = _physicsBody->getWorld();
        _physicsBody->removeFromWorld();
        _physicsBody->_node = nullptr;
        _physicsBody->release();
        
        if (world != nullptr && body != nullptr)
        {
            world->addBody(body);
        }
    }
    
    _physicsBody = body;
    
    if (body != nullptr)
    {
        Node* node;
        Scene* scene = nullptr;
        for (node = this->getParent(); node != nullptr; node = node->getParent())
        {
            Scene* tmpScene = dynamic_cast<Scene*>(node);
            if (tmpScene != nullptr && tmpScene->getPhysicsWorld() != nullptr)
            {
                scene = tmpScene;
                break;
            }
        }
        
        updatePhysicsBodyPosition(scene);
        updatePhysicsBodyRotation(scene);
    }
}
Example #30
0
	Character::Character(PhysicsWorld& physicsWorld)
		: Entity(physicsWorld.createCircleBody(GData::r_width/2, GData::r_height/2, GData::t_size/2)),
		  walkingSpeed(4.0f),
		  runningSpeed(8.0f)
	{
		setAnimation(GData::assets.getAnimation("standing"));
	}