void WordTreeScene::updatePhysics(Word* word) {
    cpShape* shape = word->getShape();
    if( shape!=NULL ) {
        cpSpaceRemoveShape(ChipmunkManager::getInstance()->getSpace(), shape);
        cpShapeDestroy(shape);
        word->setShape(NULL);
    }
    
    cpBody* body = word->getBody();
    if( body!=NULL ) {
        cpSpaceRemoveBody(ChipmunkManager::getInstance()->getSpace(), body);
        cpBodyDestroy(body);
        word->setBody(NULL);
    }
    
    CCLabelTTF* targetLabel = word->getLabel();
    CCPoint pos = targetLabel->getPosition();
    CCSize contentSize = targetLabel->getContentSize();
    
    // init body
    body = cpBodyNew(1, INFINITY);
    body->p = cpv(targetLabel->getPosition().x, targetLabel->getPosition().y);
    cpSpaceAddBody(ChipmunkManager::getInstance()->getSpace(), body);
    
    float size = MAX(targetLabel->getContentSize().width, targetLabel->getContentSize().height);
    
    shape = cpBoxShapeNew(body, size+size*0.3f, size+size*0.3f);
    cpSpaceAddShape(ChipmunkManager::getInstance()->getSpace(), shape);
    
    word->setBody(body);
    word->setShape(shape);
}
示例#2
0
int lc_space_RemoveBody(lua_State *vm){
    //space, body
    lc_space *space = lc_GetSpace(1, vm);
    if (space == NULL){
        printf("chipmunk: Object can't call :RemoveBody\n");
        return 0;
    }
    lc_body *body = lc_GetBody(2, vm);
    if (body == NULL){
        printf("space:RemoveBody(): Can't remove a object that isn't a body.\n");
        return 0;
    }
    
    char pbody[32] = {0};
    snprintf(pbody, 32, "%p", body);
    
    //Check if the body has been added to the space for be removed.
    lua_rawgeti(vm, LUA_REGISTRYINDEX, space->bodies);
    lua_getfield(vm, -1, pbody);
    if (lua_isnil(vm, -1)){
        //The body doesn't exists.
        return 0;
    }
    lua_pop(vm, 1);
    lua_pushnil(vm);
    lua_setfield(vm, -2, pbody);
    //---
    
    cpSpaceRemoveBody(space->space, body->body);
    return 0;
}
void PhysicsBody::setDynamic(bool dynamic)
{
    if (dynamic != _dynamic)
    {
        _dynamic = dynamic;
        if (dynamic)
        {
            if (_world && _cpBody->CP_PRIVATE(space))
            {
                cpSpaceConvertBodyToDynamic(_world->_cpSpace, _cpBody, _mass, _moment);
                cpSpaceAddBody(_world->_cpSpace, _cpBody);
            }
            else
            {
                cpBodySetMass(_cpBody, _mass);
                cpBodySetMoment(_cpBody, _moment);
            }
        }
        else
        {
            if (_world && _cpBody->CP_PRIVATE(space))
            {
                cpSpaceRemoveBody(_world->_cpSpace, _cpBody);
                cpSpaceConvertBodyToStatic(_world->_cpSpace, _cpBody);
            }
            else
            {
                cpBodySetMass(_cpBody, PHYSICS_INFINITY);
                cpBodySetMoment(_cpBody, PHYSICS_INFINITY);
                cpBodySetVel(_cpBody, cpvzero);
                cpBodySetAngVel(_cpBody, 0.0);
            }
        }
    }
}
示例#4
0
文件: physics.c 项目: andi2/cgame
static void _set_type(PhysicsInfo *info, PhysicsBody type)
{
    if (info->type == type)
        return; /* already set */

    info->type = type;
    switch (type)
    {
        case PB_KINEMATIC:
            info->last_pos = cpBodyGetPos(info->body);
            info->last_ang = cpBodyGetAngle(info->body);
            /* fall through */

        case PB_STATIC:
            if (!cpBodyIsStatic(info->body))
            {
                cpSpaceRemoveBody(space, info->body);
                cpSpaceConvertBodyToStatic(space, info->body);
            }
            break;

        case PB_DYNAMIC:
            cpSpaceConvertBodyToDynamic(space, info->body, info->mass, 1.0);
            cpSpaceAddBody(space, info->body);
            _recalculate_moment(info);
            break;
    }
}
示例#5
0
文件: physics.c 项目: dns/CLove
static int l_physics_bodyGC(lua_State* state)
{
    l_physics_Body* body = (l_physics_Body*)lua_touserdata(state, 1);
    cpSpaceRemoveBody(body->physics->space, body->body);

	return 0;
}
示例#6
0
ETERM *space_remove_body(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b = NULL;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    // DEBUGF(("removing body #%d\n", body_id));

    // remove the user data associated with the body
    erlmunk_body_data *data = cpBodyGetUserData(b->body);
    if (data->term != NULL)
        erl_free_compound(data->term);
    free(data);

    cpBodyEachShape(b->body, shapeRemove, NULL);
    cpSpaceRemoveBody(s->space, b->body);

    space_remove_body_hash(s, b);

    cpBodyDestroy(b->body);
    cpBodyFree(b->body);

    return NULL;
}
示例#7
0
ETERM *space_delete(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    // remove all subscribers
    for(erlmunk_subscriber *subscriber = s->subscribers; subscriber != NULL;
        subscriber = subscriber->hh.next) {
        space_remove_subscriber(s, subscriber);
    }
    // remove all bodies
    for(erlmunk_body *b = s->bodies; b != NULL;
        b = b->hh.next) {
        erlmunk_body_data *data = cpBodyGetUserData(b->body);
        if (data->term != NULL)
            erl_free_compound(data->term);
        free(data);

        cpSpaceRemoveBody(s->space, b->body);
        cpBodyDestroy(b->body);
        cpBodyFree(b->body);
        space_remove_body_hash(s, b);
    }

    return NULL;
}
示例#8
0
// poststep functions to add and remove bodies 
void postStepRemoveBody ( cpSpace *space, cpBody *body, void *unused ) {
    
    cpBodyEachShape ( body, (cpBodyShapeIteratorFunc)core_destroy_out_shapes, space );
    
    cpSpaceRemoveBody ( space, body );
    core_destroy_body ( body, NULL );
}
示例#9
0
void landerFree(Lander l, cpSpace* space)
{
    cpSpaceRemoveBody(space, l->body);
    cpSpaceRemoveShape(space, l->shape);
    
    free(l);
}
示例#10
0
	~Phys()
	{
		for (std::vector<Ship*>::iterator it = ship_list.begin(); it != ship_list.end(); ++it)
		{
			cpSpaceRemoveShape(space, (*it)->cpshape);
			cpSpaceRemoveBody(space, (*it)->body);
			delete *it;
		}
		ship_list.clear();
		for (std::vector<Shell*>::iterator it = shell_list.begin(); it != shell_list.end(); ++it)
		{
			cpSpaceRemoveShape(space, (*it)->cpshape);
			cpSpaceRemoveBody(space, (*it)->body);
			delete *it;
		}
		shell_list.clear();
		cpSpaceFree(space);
	}
示例#11
0
文件: Sensors.c 项目: 0w/moai-dev
static void
postStepRemove(cpSpace *space, cpShape *shape, void *unused)
{
	cpSpaceRemoveBody(space, shape->body);
	cpSpaceRemoveShape(space, shape);
	
	cpBodyFree(shape->body);
	cpShapeFree(shape);
}
示例#12
0
void free_body_full(cpBody *body) {
    cpBodyEachShape(body, (cpBodyShapeIteratorFunc) free_shape, NULL);
    
    point_array_free( (Body_data *) cpBodyGetUserData(body) );
	cpSpace *space = cpBodyGetSpace(body);
	cpSpaceRemoveBody(space, body);
	cpBodyDestroy(body);
    cpBodyFree(body);
}
示例#13
0
文件: world.cpp 项目: damir00/zac
void World::removeHuman(Human* h) {
	cpSpaceRemoveShape(space,h->shape);
	cpSpaceRemoveBody(space,h->body);

	for(int i=0;i<humans.size();i++) {
		if(h==humans[i]) {
			humans.erase(humans.begin()+i);
			break;
		}
	}
}
示例#14
0
	void RigidBody2D::UnregisterFromSpace()
	{
		if (m_isRegistered)
		{
			cpSpace* space = m_world->GetHandle();
			for (cpShape* shape : m_shapes)
				cpSpaceRemoveShape(space, shape);

			if (m_handle)
				cpSpaceRemoveBody(space, m_handle);

			m_isRegistered = false;
		}
	}
示例#15
0
void RCPBody::destroy()
{
    // remove all shapes before removing the body
    mShapes.clear();

    if (mType != Static && RCPWorld::instance()) {
        cpSpace * space = RCPWorld::instance()->space();
        if (mBody && space) {
            cpSpaceRemoveBody(space, mBody);
            cpBodyFree(mBody);
        }
    }

    mBody = 0;
}
示例#16
0
/**
 * Removes a player object from the grid.
 * This means moving him from the playing list to the queue.
 */
int player_remove(struct player *p, struct game *g) {
	if (p->list != g->p_p) {
		//The player isn't in the playing list
		return -2;
	}
	if (linkedlist_add_last(g->p_q, p)) {
		ERR_TRACE();
		return -1;
	}
	linkedlist_remove_node(g->p_p, p->node);
	cpSpaceRemoveShape(g->cp, (struct cpShape *)&p->shape);
	cpSpaceRemoveBody(g->cp, &p->body);
	p->node = g->p_q->last;
	return 0;
}
void TestColliderDetector::destroyCPBody(cpBody *body)
{
	cpShape *shape = body->shapeList_private;
	while(shape)
	{
		cpShape *temp = shape->next_private;
        
		cpSpaceRemoveShape(space, shape);
		cpShapeFree(shape);
        
		shape = temp;
	}
    
	cpSpaceRemoveBody(space, body);
	cpBodyFree(body);
}
示例#18
0
void PhysicsWorld::doRemoveBody(PhysicsBody* body)
{
    CCASSERT(body != nullptr, "the body can not be nullptr");
    
    // remove shapes
    for (auto& shape : body->getShapes())
    {
        removeShape(shape);
    }
    
    // remove body
    if (cpSpaceContainsBody(_cpSpace, body->_cpBody))
    {
        cpSpaceRemoveBody(_cpSpace, body->_cpBody);
    }
}
 CDynamics2DSingleBodyObjectModel::~CDynamics2DSingleBodyObjectModel() {
    bool bIsStatic = cpBodyIsStatic(m_ptBody);
    /* Dispose of shapes */
    for(cpShape* pt_shape = m_ptBody->shapeList;
        pt_shape != NULL;
        pt_shape = pt_shape->next) {
       cpSpaceRemoveShape(GetDynamics2DEngine().GetPhysicsSpace(), pt_shape);
       cpShapeFree(pt_shape);
    }
    /* Dispose of body */
    if(! bIsStatic)
       cpSpaceRemoveBody(GetDynamics2DEngine().GetPhysicsSpace(), m_ptBody);
    cpBodyFree(m_ptBody);
    /* Reindex space */
    if(bIsStatic) cpSpaceReindexStatic(GetDynamics2DEngine().GetPhysicsSpace());
 }
示例#20
0
CDynamics2DCylinderEntity::~CDynamics2DCylinderEntity() {
    if(m_ptBody != NULL) {
        cpSpaceRemoveConstraint(m_cEngine.GetPhysicsSpace(), m_ptLinearFriction);
        cpSpaceRemoveConstraint(m_cEngine.GetPhysicsSpace(), m_ptAngularFriction);
        cpConstraintFree(m_ptLinearFriction);
        cpConstraintFree(m_ptAngularFriction);
        cpSpaceRemoveBody(m_cEngine.GetPhysicsSpace(), m_ptBody);
        cpBodyFree(m_ptBody);
        cpSpaceRemoveShape(m_cEngine.GetPhysicsSpace(), m_ptShape);
    }
    else {
        cpSpaceRemoveStaticShape(m_cEngine.GetPhysicsSpace(), m_ptShape);
        cpSpaceRehashStatic(m_cEngine.GetPhysicsSpace());
    }
    cpShapeFree(m_ptShape);
}
示例#21
0
	void RigidBody2D::Destroy()
	{
		cpSpace* space = m_world->GetHandle();
		for (cpShape* shape : m_shapes)
		{
			cpSpaceRemoveShape(space, shape);
			cpShapeFree(shape);
		}

		if (m_handle)
		{
			cpSpaceRemoveBody(space, m_handle);
			cpBodyFree(m_handle);
		}
		m_shapes.clear();
	}
示例#22
0
Body::~Body(){
	if(body){
		//clean up all constraints
		constraintsToRemove.clear();
		cpBodyEachConstraint(body, &constraintRemoveQuery, (void*)this);
		for(auto c: constraintsToRemove){
			((Constraint*)cpConstraintGetUserData(c))->constraint = nullptr;
			cpSpaceRemoveConstraint(cpConstraintGetSpace(c), c);
			cpConstraintDestroy(c);
		}

		//remove body
		cpSpaceRemoveBody(cpBodyGetSpace(body), body);
		cpBodyFree(body);
		body = nullptr;
	}
}
GeometrySpringDynamic::~GeometrySpringDynamic()
{
	clearJoints();

	if( 0 != m_Shape && true == cpSpaceContainsShape( m_Space, m_Shape ) )
	{
		cpSpaceRemoveShape( m_Space, m_Shape );
		cpShapeFree( m_Shape );
		m_Shape = 0;
	}
	if( 0 != m_Body && true == cpSpaceContainsBody( m_Space, m_Body ) )
	{
		cpSpaceRemoveBody( m_Space, m_Body );
		cpBodyFree( m_Body );
		m_Body = 0;
	}
}
示例#24
0
void Slice::SliceShapePostStep(cpSpace *space, cpShape *shape, struct SliceContext *context)
{
	cpVect a = context->a;
	cpVect b = context->b;
	
	// Clipping plane normal and distance.
	cpVect n = cpvnormalize(cpvperp(cpvsub(b, a)));
	cpFloat dist = cpvdot(a, n);
	
	ClipPoly(space, shape, n, dist);
	ClipPoly(space, shape, cpvneg(n), -dist);
	
	cpBody *body = cpShapeGetBody(shape);
	cpSpaceRemoveShape(space, shape);
	cpSpaceRemoveBody(space, body);
	cpShapeFree(shape);
	cpBodyFree(body);
}
示例#25
0
void PhysicsBody::setDynamic(bool dynamic)
{
    if (dynamic != _dynamic)
    {
        _dynamic = dynamic;
        if (_world != nullptr)
        {
            if (dynamic)
            {
                cpSpaceAddBody(_world->_info->space, _info->body);
            }else
            {
                cpSpaceRemoveBody(_world->_info->space, _info->body);
            }
        }
        
    }
}
示例#26
0
文件: Unit.cpp 项目: uzername/Yukkuri
Unit::~Unit()
{
	ActionTimer* timer = NULL;
	while( actionTimers != NULL ){
		timer = actionTimers;
		Timer::DeleteTimerEventById( timer->timerId );
		actionTimers = timer->next;
		delete timer, timer = NULL;
	}
	if( physShape ){
		cpSpaceRemoveShape( Phys::space, physShape );
		cpShapeFree( physShape );
	}
	if( physBody ){
		if( !cpBodyIsStatic(physBody) )
			cpSpaceRemoveBody( Phys::space, physBody );
		cpBodyFree( physBody );
	}
}
示例#27
0
void Bomb::destroy(cpSpace *space) {
  if (!destroyed) {
    destroyed = true;
    if (simulation_type == "DYNAMIC")
      cpSpaceRemoveShape(space, bombShape);
    else
      cpSpaceRemoveStaticShape(space, bombShape);
    
    if (simulation_type == "DYNAMIC") cpSpaceRemoveBody(space, body);
    cpShapeFree(bombShape);
    cpBodyFree(body);
  
    for (int j=0; j < PARTICLE_TOTAL; j++) {
      delete particles[j];
      particles[j] = NULL;
    }
  
    delete [] particles;
  }
}
示例#28
0
文件: Plink.c 项目: Adefy/AdefyiOS
static void
update(cpSpace *space, double dt)
{
	if(ChipmunkDemoRightDown){
		cpShape *nearest = cpSpaceNearestPointQueryNearest(space, ChipmunkDemoMouse, 0.0, GRABABLE_MASK_BIT, CP_NO_GROUP, NULL);
		if(nearest){
			cpBody *body = cpShapeGetBody(nearest);
			if(cpBodyIsStatic(body)){
				cpSpaceConvertBodyToDynamic(space, body, pentagon_mass, pentagon_moment);
				cpSpaceAddBody(space, body);
			} else {
				cpSpaceRemoveBody(space, body);
				cpSpaceConvertBodyToStatic(space, body);
			}
		}
	}
	
	cpSpaceStep(space, dt);
	cpSpaceEachBody(space, &eachBody, NULL);
}
示例#29
0
文件: Player.cpp 项目: ofx/dr
Player::~Player(void)
{
    delete this->m_ParticleSystem;
	delete this->m_Sprite;
	delete this->m_ParticleSprite;

    // Delete weapon in weapon slots
    for (int i = 0 ; i < NUM_WEAPON_SLOTS ; ++i)
    {
        delete this->m_Weaponslots[i];
    }
    delete this->m_Weaponslots;

    // Delete physics stuff
    cpSpace *space = this->m_Engine->GetWorld()->GetSpace();
    cpSpaceRemoveShape(space, this->m_Shape);
    cpSpaceRemoveBody(space, this->m_Body);
    cpShapeFree(this->m_Shape);
    cpBodyFree(this->m_Body);
}
 CDynamics2DMultiBodyObjectModel::~CDynamics2DMultiBodyObjectModel() {
    /* Dispose of shapes and bodies */
    while(!m_vecBodies.empty()) {
       /* Get pointer to body */
       cpBody* ptBody = m_vecBodies.back().Body;
       /* Remove all of its shapes */
       cpShape* ptCurShape = ptBody->shapeList;
       cpShape* ptNextShape;
       while(ptCurShape) {
          ptNextShape = ptCurShape->next;
          cpSpaceRemoveShape(GetDynamics2DEngine().GetPhysicsSpace(), ptCurShape);
          cpShapeFree(ptCurShape);
          ptCurShape = ptNextShape;
       }
       /* Dispose of body */
       cpSpaceRemoveBody(GetDynamics2DEngine().GetPhysicsSpace(), ptBody);
       cpBodyFree(ptBody);
       /* Dispose of struct */
       m_vecBodies.pop_back();
    }
 }