CDynamics2DStretchableObjectModel::~CDynamics2DStretchableObjectModel() {
    if(m_pcGrippable != NULL) delete m_pcGrippable;
    if(m_ptLinearFriction != NULL) {
       cpSpaceRemoveConstraint(GetDynamics2DEngine().GetPhysicsSpace(), m_ptLinearFriction);
       cpConstraintFree(m_ptLinearFriction);
    }
    if(m_ptAngularFriction != NULL) {
       cpSpaceRemoveConstraint(GetDynamics2DEngine().GetPhysicsSpace(), m_ptAngularFriction);
       cpConstraintFree(m_ptAngularFriction);
    }
 }
 void CDynamics2DVelocityControl::Detach() {
    if(m_ptControlledBody != NULL) {
       /* Remove constraints */
       cpSpaceRemoveConstraint(m_cDyn2DEngine.GetPhysicsSpace(), m_ptLinearConstraint);
       cpSpaceRemoveConstraint(m_cDyn2DEngine.GetPhysicsSpace(), m_ptAngularConstraint);
       /* Free memory up */
       cpConstraintFree(m_ptLinearConstraint);
       cpConstraintFree(m_ptAngularConstraint);
       /* Erase pointer to controlled body */
       m_ptControlledBody = NULL;
    }
 }
Beispiel #3
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);
}
Beispiel #4
0
void worldConstr_invalidate(WorldConstraint_t *aConstraint)
{
    if(!aConstraint->isValid)
        return;
    aConstraint->isValid = false;
    cpSpaceRemoveConstraint(aConstraint->world->cpSpace, aConstraint->cpConstraint);
}
void NaiveRotationAlgorithm::stepSystem(SystemInfo *individual)
{
    
    MachineSystem *oldSystem = individual->system;
    individual->system = new MachineSystem(*oldSystem); // copy it to stop all the damn bouncing about
    delete oldSystem;
    
    cpBody *inputBody = individual->system->partAtPosition(individual->system->inputMachinePosition)->body;
    cpBody *outputBody = individual->system->partAtPosition(individual->system->outputMachinePosition)->body;
    cpSpace *systemSpace = individual->system->getSpace();

    
    cpConstraint *motor = cpSimpleMotorNew(cpSpaceGetStaticBody(systemSpace), inputBody, M_PI);
    cpSpaceAddConstraint(systemSpace, motor);
    cpConstraintSetMaxForce(motor, 50000);

    
    for (int i=0; i<simSteps; i++) {
        individual->inputValues[i] = (cpBodyGetAngle(inputBody));
        
        cpSpaceStep(systemSpace, 0.1);
        individual->outputValues[i] = (cpBodyGetAngle(outputBody));
    }
    cpSpaceRemoveConstraint(systemSpace, motor);
    cpBodySetAngVel(outputBody, 0);
    cpBodySetAngle(inputBody, 0);
}
Beispiel #6
0
 void SDynamics2DEngineGripperData::ClearConstraints() {
    if(GripConstraint != NULL) {
       cpSpaceRemoveConstraint(Space, GripConstraint);
       cpConstraintFree(GripConstraint);
       GripConstraint = NULL;
       GripperEntity.ClearGrippedEntity();
    }
 }
Beispiel #7
0
static void
PostStepRemoveJoint(cpSpace *space, void *key, void *data)
{
//	printf("Removing joint for %p\n", data);
	
	cpConstraint *joint = (cpConstraint *)key;
	cpSpaceRemoveConstraint(space, joint);
	cpConstraintFree(joint);
}
void GeometrySpringDynamic::clearJoints()
{
	if( 0 != m_ConstraintGear && true == cpSpaceContainsConstraint( m_Space, m_ConstraintGear ) )
	{
		cpSpaceRemoveConstraint( m_Space, m_ConstraintGear );
		cpConstraintFree( m_ConstraintGear );
		m_ConstraintGear = 0;
	}
}
Beispiel #9
0
void PlayLayer::onForwardUp() {
	
    DoodleTruck *doodleTruck = DoodleTruck::sharedDoodleTruck();
	if (cpSpaceContainsConstraint(doodleTruck->getSpace(), (cpConstraint*)doodleTruck->getMotor()))
		cpSpaceRemoveConstraint(doodleTruck->getSpace(), (cpConstraint*)doodleTruck->getMotor());
	forward_touched = false;
    
    measureRPM->removeRPM();
    measureBoost->removeBoost();
}
Beispiel #10
0
int kmport(port_t* port)
{
  if(port == NULL) {
    return -1; // nyurupo~
  }
  if(port->kind == NULL) {
    return -1; // bad port
  }
  if(port->kind->km == NULL) {
    return -1; // not implemented
  }

  cpSpace* space = current_space();

  cpBodyEachConstraint_b(port->body,
      ^(cpConstraint* c) { cpSpaceRemoveConstraint(space, c); });
static void
update(cpSpace *space, double dt)
{
	// Set the first anchor point (the one attached to the static body) of the dolly servo to the mouse's x position.
	cpPivotJointSetAnchorA(dollyServo, cpv(ChipmunkDemoMouse.x, 100));
	
	// Set the max length of the winch servo to match the mouse's height.
	cpSlideJointSetMax(winchServo, cpfmax(100 - ChipmunkDemoMouse.y, 50));
	
	if(hookJoint && ChipmunkDemoRightClick){
		cpSpaceRemoveConstraint(space, hookJoint);
		cpConstraintFree(hookJoint);
		hookJoint = NULL;
	}
	
	cpSpaceStep(space, dt);
}
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;
	}
}
Beispiel #13
0
/* Mouse handling is a bit tricky. We want the user to move
 * tiles using the mouse but because tiles are dynamic bodies
 * managed by Chipmunk2D, we cannot directly control them.
 * This is resolved by creating a pivot joint between an
 * invisible mouse body that we can control and the tile body
 * that we cannot directly control.
 */
static void apply_mouse_motion(struct state* state)
{
    struct mouse m;
    update_mouse(&m);
    int w, h;
    get_screen_size(&w, &h);
    int x = m.x_position * w;
    int y = m.y_position * h;
    cpVect mouse_pos = cpv(x, y);
    cpVect new_point =
        cpvlerp(cpBodyGetPosition(state->mouse_body), mouse_pos, 0.25f);
    cpBodySetVelocity(
        state->mouse_body,
        cpvmult(cpvsub(new_point, cpBodyGetPosition(state->mouse_body)),
                60.0f));
    cpBodySetPosition(state->mouse_body, new_point);
    if (m.left_click && state->mouse_joint == NULL) {
        cpFloat radius = 5.0;
        cpPointQueryInfo info = { 0 };
        cpShape* shape = cpSpacePointQueryNearest(state->space, mouse_pos,
                                                  radius, GRAB_FILTER, &info);
        if (shape && cpBodyGetMass(cpShapeGetBody(shape)) < INFINITY) {
            cpVect nearest = (info.distance > 0.0f ? info.point : mouse_pos);
            cpBody* body = cpShapeGetBody(shape);
            state->mouse_joint =
                cpPivotJointNew2(state->mouse_body, body, cpvzero,
                                 cpBodyWorldToLocal(body, nearest));
            cpConstraintSetMaxForce(state->mouse_joint, 5000000.0f);
            cpConstraintSetErrorBias(state->mouse_joint,
                                     cpfpow(1.0f - 0.15f, 60.0f));
            cpSpaceAddConstraint(state->space, state->mouse_joint);
        }
    }
    if (m.left_click == false && state->mouse_joint != NULL) {
        cpSpaceRemoveConstraint(state->space, state->mouse_joint);                                                 
        cpConstraintFree(state->mouse_joint);                                                               
        state->mouse_joint = NULL;  
    }
}
Beispiel #14
0
static void
update(int ticks)
{
	int steps = 1;
	cpFloat dt = 1.0f/60.0f/(cpFloat)steps;
	
	for(int i=0; i<steps; i++){
		// Set the first anchor point (the one attached to the static body) of the dolly servo to the mouse's x position.
		cpPivotJointSetAnchr1(dollyServo, cpv(ChipmunkDemoMouse.x, 100));
		
		// Set the max length of the winch servo to match the mouse's height.
		cpSlideJointSetMax(winchServo, cpfmax(100 - ChipmunkDemoMouse.y, 50));
		
		if(hookJoint && ChipmunkDemoKeyboard.y < 0){
			cpSpaceRemoveConstraint(space, hookJoint);
			cpConstraintFree(hookJoint);
			hookJoint = NULL;
		}
		
		cpSpaceStep(space, dt);
	}
}
static void
click(int button, int state, int x, int y)
{
	if(button == GLUT_LEFT_BUTTON){
		if(state == GLUT_DOWN){
			cpVect point = mouseToSpace(x, y);
		
			cpShape *shape = cpSpacePointQueryFirst(space, point, GRABABLE_MASK_BIT, CP_NO_GROUP);
			if(shape){
				cpBody *body = shape->body;
				mouseJoint = cpPivotJointNew2(mouseBody, body, cpvzero, cpBodyWorld2Local(body, point));
				mouseJoint->maxForce = 50000.0f;
				mouseJoint->errorBias = cpfpow(1.0f - 0.15f, 60.0f);
				cpSpaceAddConstraint(space, mouseJoint);
			}
		} else if(mouseJoint){
			cpSpaceRemoveConstraint(space, mouseJoint);
			cpConstraintFree(mouseJoint);
			mouseJoint = NULL;
		}
	}
}
void DynamicObjectStabilizator::clearResources()
{
	{
		vector<cpBody *>::iterator begin = m_KineticBodies.begin();
		vector<cpBody *>::iterator end = m_KineticBodies.end();
		vector<cpBody *>::iterator iter = begin;
		for(  ; iter != end ; iter++ )
		{
			cpBody * kineticBody = (* iter );
			cpSpaceRemoveBody( m_Space, kineticBody );
		}
	}
	{
		vector<cpConstraint *>::iterator begin = m_Joints.begin();
		vector<cpConstraint *>::iterator end = m_Joints.end();
		vector<cpConstraint *>::iterator iter = begin;
		for(  ; iter != end ; iter++ )
		{
			cpConstraint * joint = (* iter );
			cpSpaceRemoveConstraint( m_Space, joint );
		}
	}
}
void PhysicsWorld::doRemoveJoint(PhysicsJoint* joint)
{
    for (auto constraint : joint->_cpConstraints)
    {
        cpSpaceRemoveConstraint(_cpSpace, constraint);
    }
    _joints.remove(joint);
    joint->_world = nullptr;

    if (joint->getBodyA())
    {
        joint->getBodyA()->removeJoint(joint);
    }

    if (joint->getBodyB())
    {
        joint->getBodyB()->removeJoint(joint);
    }

    if (joint->_destoryMark)
    {
        delete joint;
    }
}
Beispiel #18
0
__declspec( dllexport ) void deletevar( const void * _in, int in_size, void * _out, int out_sz )
{
	cpBody *body;
	int i;
	int index;
	Variable *var;
	Variable *mainVar = vhRemoveVariable(&mVariableHandler,PEEKINT(INPUT_MEMBLOCK,0));
	
	if (mainVar->mType == VarTypeBody)
	{
		body = (cpBody*)mainVar->mPtr;
		resetVariableList();
		cpBodyEachShape(body,listShape,0);
		index = 0;
		for (i = 0;i != mVariableListIterator;i++)
		{
			var = mVariableList[i];
			
			POKEINT(OUTPUT_MEMBLOCK,index,var->mCBPtr);index += 4;
			
			cpSpaceRemoveShape(&mSpace,(cpShape*)var->mPtr);
			cpShapeFree((cpShape*)var->mPtr);
			//MessageBoxA(NULL,"ShapeRemoved2","Error",MB_OK);

			vhRemoveVariable(&mVariableHandler,var->mLocalHandle);
			varFree(var);
		}

		resetVariableList();
		cpBodyEachConstraint(body,listConstraint,0);
		for (i = 0;i != mVariableListIterator;i++)
		{
			var = mVariableList[i];
			POKEINT(OUTPUT_MEMBLOCK,index,var->mCBPtr);index+=4;
			//MessageBoxA(NULL,"Constraint removed2","Error",MB_OK);
			cpSpaceRemoveConstraint(&mSpace,(cpConstraint*)var->mPtr);
			cpConstraintFree((cpConstraint*)var->mPtr);
			vhRemoveVariable(&mVariableHandler,var->mLocalHandle);
			varFree(var);
		}
		POKEINT(OUTPUT_MEMBLOCK,index,0);//End point
		//MessageBoxA(NULL,"BodyRemoved","Error",MB_OK);
		cpSpaceRemoveBody(&mSpace,body);
		cpBodyFree(body);
		varFree(mainVar);
	}
	else if (mainVar->mType == VarTypeShape)
	{
		cpSpaceRemoveShape(&mSpace,(cpShape*)mainVar->mPtr);
		cpShapeFree((cpShape*)mainVar->mPtr);
		//MessageBoxA(NULL,"ShapeRemoved","Error",MB_OK);
		varFree(mainVar);
		POKEINT(OUTPUT_MEMBLOCK,0,0);
	}
	else if (mainVar->mType == VarTypeConstraint)
	{
		cpSpaceRemoveConstraint(&mSpace,(cpConstraint*)mainVar->mPtr);
		cpConstraintFree((cpConstraint*)mainVar->mPtr);
		varFree(mainVar);

		POKEINT(OUTPUT_MEMBLOCK,0,0);
	}
	else if (mainVar->mType == VarTypeDataArray)
	{
		daFree((DataArray*)mainVar->mPtr);
		varFree(mainVar);
		POKEINT(OUTPUT_MEMBLOCK,0,0);
	}
	else
	{
		//Failure...
		MessageBoxA(NULL,"Unknown cpChipmunk error 11","Error",MB_OK);
		varFree(mainVar);
		POKEINT(OUTPUT_MEMBLOCK,0,0);
		
	}
	
}
Beispiel #19
0
void Space::removeConstraint(cp::Constraint *constraint)
{
		cpSpaceRemoveConstraint(space,constraint ? constraint->get() : 0);
}
Beispiel #20
0
CCJoint::~CCJoint()
{
	cpSpaceRemoveConstraint(this->world->getSpace(), this->m_constraint);
	cpConstraintFree(this->m_constraint);
}
Beispiel #21
0
static void constraintFreeWrap(cpSpace *space, cpConstraint *constraint, void *unused){
	cpSpaceRemoveConstraint(space, constraint);
	cpConstraintFree(constraint);
}
Beispiel #22
0
void Chains::BreakablejointPostStepRemove(cpSpace *space, cpConstraint *joint, void *unused)
{
	cpSpaceRemoveConstraint(space, joint);
	cpConstraintFree(joint);
}