cpBody *cpSpaceSerializer::createBody(TiXmlElement *elm)
{	
	TiXmlElement *bodyElm;;
	cpBody *body;
	
	CPSS_ID b_id = createValue<CPSS_ID>("body_id", elm);
	BodyMap::iterator itr = _bodyMap.find(b_id);
	
	//If it doesn't exist, try to create it
	if (itr == _bodyMap.end())
	{
		bodyElm = elm->FirstChildElement("body");
		
		if (bodyElm)
		{
			cpFloat mass = createValue<cpFloat>("mass", bodyElm);
			cpFloat inertia = createValue<cpFloat>("inertia", bodyElm);
			
            if (mass == INFINITY)
                body = cpBodyNewStatic();
            else
                body = cpBodyNew(mass, inertia);
			
			body->p = createPoint("p", bodyElm);
			body->v = createPoint("v", bodyElm);
			body->f = createPoint("f", bodyElm);
			body->w = createValue<cpFloat>("w", bodyElm);
			body->t = createValue<cpFloat>("t", bodyElm);
            cpBodySetAngle(body, createValue<cpFloat>("a", bodyElm));
			
			_bodyMap[b_id] = body;

            if (delegate && b_id != 0)
            {
                if (!delegate->reading(body, b_id))
                {
                    cpBodyFree(body);
                    body = NULL;
                }
            }
		}
		else
			body = cpBodyNewStatic(); //Fail case, should throw or something
	}
	else 
		body = itr->second; //Else grab it
	
	return body;
}
Example #2
0
World::World(cpVect size) {
	space=cpSpaceNew();

	static_body=cpBodyNewStatic();

	printf("new space\n"); fflush(stdout);

	cpVect s2=cpvmult(size,0.5);

	cpVect c1=s2;
	cpVect c2=cpv(s2.x,-s2.y);
	cpVect c3=cpv(-s2.x,-s2.y);
	cpVect c4=cpv(-s2.x,s2.y);

	printf("foo space\n"); fflush(stdout);

	cpBodyInitStatic(space->staticBody);

	addStaticLine(c1,c2);
	addStaticLine(c2,c3);
	addStaticLine(c3,c4);
	addStaticLine(c4,c1);

	printf("space done\n"); fflush(stdout);
}
MachineSystem::MachineSystem(int width, int height, int hPegs, int vPegs, cpVect position)
: parts(hPegs*vPegs),
  attachments((hPegs*vPegs), std::vector<Attachment *>(hPegs*vPegs, NULL)),
  space(cpSpaceNew()),
  nMachines(0),
  nAttachments(0)
{
    cpSpaceSetIterations(space, 20);
    gridSpacing = cpv((float)width/(hPegs + 1), (float)height/(vPegs + 1));
    size = cpv(hPegs, vPegs);
    body = cpBodyNewStatic();
    cpBodySetPos(body, position);
    
    cpShape *wallShape = cpSegmentShapeNew(body, cpv(-width/2, height/2), cpv(width/2, height/2), .5);
    cpShapeSetLayers(wallShape, WALL_LAYER);
    cpSpaceAddStaticShape(space, wallShape);
    
    wallShape = cpSegmentShapeNew(body, cpv(-width/2, -height/2), cpv(+width/2, -height/2), 0.5);
    cpShapeSetLayers(wallShape, WALL_LAYER);
    cpSpaceAddStaticShape(space, wallShape);
    
    wallShape = cpSegmentShapeNew(body, cpv(-width/2, +height/2), cpv(-width/2, -height/2), 0.5);
    cpShapeSetLayers(wallShape, WALL_LAYER);
    cpSpaceAddStaticShape(space, wallShape);
    
    wallShape = cpSegmentShapeNew(body, cpv(+width/2, +height/2), cpv(+width/2, -height/2), 0.5);
    cpShapeSetLayers(wallShape, WALL_LAYER);
    cpSpaceAddStaticShape(space, wallShape);
    
    inputMachinePosition = cpv(-1,-1);
    outputMachinePosition = cpv(-1,-1);
    
}
Example #4
0
static palette_object_t *createPaletteObject(cpVect p, char *file, int dam, float m) {
    image_t *img;
    palette_object_t *obj;

    int numVerts = 4;
    cpVect verts[] = {
        {-16, -16},
        {-16,  16},
        { 16,  16},
        { 16, -16}
    };

    obj = (palette_object_t *)malloc(sizeof(palette_object_t));
    obj->maxDamage = dam;
    obj->m = m;

    if (file) {
        img = atlFindImageFile(file, false, GL_CLAMP);
        strncpy(obj->texfile, img->name, PATH_MAX);
        obj->texnum = img->texnum;
    } else {
        obj->texnum = -1;
    }

    obj->body = cpBodyNewStatic();
    obj->body->p = p;
    obj->shape = cpPolyShapeNew(obj->body, numVerts, verts, cpvzero);
    obj->shape->data = obj;
    obj->shape->collision_type = PALETTE_TYPE;
    cpSpaceAddShape(g_Space, obj->shape);

    return obj;
}
Example #5
0
File: LevelGrid.cpp Project: ofx/dr
void LevelGrid::InitializeLevelPhysics(void)
{
    // Fetch the physics space
    cpSpace *space = this->m_Engine->GetWorld()->GetSpace();

    // Add the physics body
    this->m_Body = cpBodyNewStatic();
        
    // Add the physics shapes
    for (int i = 0 ; i < this->m_NumY - 1 ; ++i)
    {
        hgeVector *vec  = this->m_LeftLevelVertices[i];
        hgeVector *nvec = this->m_LeftLevelVertices[i + 1];

        cpShape *shape = cpSegmentShapeNew(this->m_Body, cpv(vec->x, vec->y), cpv(nvec->x, nvec->y), 0.0f);
        shape->e = 1.0; shape->u = 1.0;
        shape->collision_type = COLLISION_TYPE_LEVEL;
        cpSpaceAddStaticShape(space, shape);
        cpBodyActivateStatic(this->m_Body, shape);
        this->m_Shapes.push_back(shape);
    }
    for (int i = 0 ; i < this->m_NumY - 1 ; ++i)
    {
        hgeVector *vec  = this->m_RightLevelVertices[i];
        hgeVector *nvec = this->m_RightLevelVertices[i + 1];
        
        cpShape *shape = cpSegmentShapeNew(this->m_Body, cpv(vec->x, vec->y), cpv(nvec->x, nvec->y), 0.0f);
        shape->e = 1.0; shape->u = 1.0;
        shape->collision_type = COLLISION_TYPE_LEVEL;
        cpSpaceAddStaticShape(space, shape);
        cpBodyActivateStatic(this->m_Body, shape);
        this->m_Shapes.push_back(shape);
    }

    // Create the activator physics shapes
    for (int i = 0 ; i < this->m_NumActivators ; ++i)
    {
        Activator *activator = this->m_Activators[i];

        // Create the cpVects
        cpVect *verts = (cpVect*) malloc(sizeof(cpVect) * 4);
        verts[3].x = activator->Vertices[0].x; verts[3].y = activator->Vertices[0].y;
        verts[2].x = activator->Vertices[1].x; verts[2].y = activator->Vertices[1].y;
        verts[1].x = activator->Vertices[2].x; verts[1].y = activator->Vertices[2].y;
        verts[0].x = activator->Vertices[3].x; verts[0].y = activator->Vertices[3].y;

        // Create and add the poly shape
        cpShape *shape = cpPolyShapeNew(this->m_Body, 4, verts, cpvzero);
        shape->e = 1.0; shape->u = 1.0;
        shape->sensor         = true;
        shape->collision_type = COLLISION_TYPE_ACTIVATOR;
        shape->data           = activator;
        cpSpaceAddStaticShape(space, shape);
        cpBodyActivateStatic(this->m_Body, shape);
        this->m_Shapes.push_back(shape);

        free(verts);
    }
}
Example #6
0
int lc_NewStaticBody(lua_State *vm){
    //-> body
    lc_object *object_body = lua_newuserdata(vm, sizeof(lc_object));
    object_body->type = Body;
    object_body->object = cpBodyNewStatic();
    lua_getfield(vm, LUA_REGISTRYINDEX, "chipmunk.bodymeta");
    lua_setmetatable(vm, -2);
    return 1;
}
Example #7
0
static cpBody *MakeBody(const float x, const float y, const float w)
{
	cpBody *body = cpSpaceAddBody(space.Space, cpBodyNewStatic());
	cpBodySetPosition(body, cpv(x + w / 2, y - GAP_HEIGHT / 2));
	cpShape *shape = cpSpaceAddShape(
		space.Space, cpBoxShapeNew(body, w, GAP_HEIGHT, 0.0));
	cpShapeSetElasticity(shape, BLOCK_ELASTICITY);
	cpShapeSetFriction(shape, 1.0f);
	return body;
}
PhysicsShapeInfo::PhysicsShapeInfo(PhysicsShape* shape)
: _shape(shape)
, _group(CP_NO_GROUP)
{
    if (_sharedBody == nullptr)
    {
        _sharedBody = cpBodyNewStatic();
    }
    
    _body = _sharedBody;
}
Example #9
0
PhysicsShapeInfo::PhysicsShapeInfo(PhysicsShape* shape)
: shape(shape)
, group(CP_NO_GROUP)
{
    if (shareBody == nullptr)
    {
        shareBody = cpBodyNewStatic();
    }
    
    body = shareBody;
}
Example #10
0
Coin::Coin(int p) : price(p)
{
#if STATIC_OBJECT == 1
	coinBody = cpBodyNewStatic();
#else
	coinBody = cpBodyNew(INFINITY, INFINITY);
#endif
	coinShape = cpCircleShapeNew(coinBody, COIN_R, cpv(0,0));
	//CCLog("coin shape %x", coinShape);
	coinShape->collision_type = GameScreen::COIN_COLLISION;
	//coinBody->
	coinBody->v = cpv(0, absV);
}
Example #11
0
	cpBody* RigidBody2D::Create(float mass, float moment)
	{
		cpBody* handle;
		if (IsKinematic())
		{
			if (IsStatic())
				handle = cpBodyNewStatic();
			else
				handle = cpBodyNewKinematic();
		}
		else
			handle = cpBodyNew(mass, moment);

		cpBodySetUserData(handle, this);

		return handle;
	}
Example #12
0
File: physics.c Project: dns/CLove
int l_physics_newCircleBody(lua_State* state)
{

    l_tools_checkUserDataPlusErrMsg(state, 1, "You must provide a space");
    l_physics_PhysicsData* physics = (l_physics_PhysicsData*)lua_touserdata(state, 1);
    const char* type = luaL_optstring(state, 2, "dynamic");
    float outer_radius = luaL_optnumber(state, 3, 1.0f);
    cpVect offset = cpvzero;
    offset.x = luaL_optnumber(state, 4, 0.0f);
    offset.y = luaL_optnumber(state, 5, 0.0f);
    float mass = luaL_optnumber(state, 6, 1.0f);
    float moment = luaL_optnumber(state, 7, 0.0f);
    float inner_radius = luaL_optnumber(state, 8, 0.0f);

    moduleData.body = (l_physics_Body*)lua_newuserdata(state, sizeof(l_physics_Body));
    moduleData.body->physics = malloc(sizeof(physics_PhysicsData));
    moduleData.body->physics = physics->physics;

    cpFloat _moment = moment;

    if (_moment == 0)
        _moment = cpMomentForCircle(mass, inner_radius, outer_radius, offset);

    if (strcmp(type, "dynamic") == 0)
        moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNewDynamic());
    else if (strcmp(type, "kinematic") == 0)
        moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNewKinematic());
    else if (strcmp(type, "static") == 0)
        moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNewStatic());
    else
    {
        moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNew(mass, _moment));
        /*
        const char* err = util_concatenate("Undefined type: ", type);
        l_tools_trowError(state, err);
        return -1;
        */
    }

    lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.bodyMT);
    lua_setmetatable(state, -2);

    return 1;
}
Example #13
0
File: physics.c Project: dns/CLove
int l_physics_newBoxBody(lua_State* state)
{

    l_tools_checkUserDataPlusErrMsg(state, 1, "You must provide a space");
    l_physics_PhysicsData* physics = (l_physics_PhysicsData*)lua_touserdata(state, 1);
    const char* type = l_tools_toStringOrErrorPlusMsg(state, 2, "You must provide a type, eg: dynamic,static,kinematic");
    float width = l_tools_toNumberOrErrorPlusMsg(state, 3, "You must provide a width");
    float height = luaL_optnumber(state, 4, width);
    float mass = luaL_optnumber(state, 5, 1.0f);
    float moment = luaL_optnumber(state, 6, 0.0f);

    moduleData.body = (l_physics_Body*)lua_newuserdata(state, sizeof(l_physics_Body));
    moduleData.body->physics = malloc(sizeof(physics_PhysicsData));
    moduleData.body->physics = physics->physics;

    cpFloat _moment = moment;

    // If we don't provide a default moment then we let chipmunk calculate one
    if (_moment == 0)
        _moment = cpMomentForBox(mass, width, height);

    if (strcmp(type, "dynamic") == 0)
        moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNewDynamic());
    else if (strcmp(type, "static") == 0)
        moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNewStatic());
    else if (strcmp(type, "kinematic") == 0)
        moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNewKinematic());
    else
    {
        moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNew(mass, _moment));
        /*
        const char* err = util_concatenate("Undefined type: ", type);
        l_tools_trowError(state, err);
        return -1;
        */
    }

    lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.bodyMT);
    lua_setmetatable(state, -2);

    return 1;
}
// NOTE - don't use a GEAR attachment to attach to the wall - the peg does not rotate, so the body will not rotate
void MachineSystem::addPart(MachinePart *newPart, Attachment *attachment, cpVect gridPosition)
{
    int machineNumber = machinePositionToNumber(gridPosition);
    
    MachinePart *alreadyAttached = parts[machineNumber];
    if (!alreadyAttached) {
        cpVect pegPosition = gridPositionToWorld(gridPosition);
        cpBody *pegBody = cpBodyNewStatic();
        cpBodySetPos(pegBody, pegPosition);
        cpShape *pegShape = cpCircleShapeNew(pegBody, 5.0, cpv(0,0));
        cpShapeSetLayers(pegShape, PEG_LAYER);
        cpSpaceAddStaticShape(space, pegShape);
        
        parts[machineNumber] = newPart;
        nMachines++;
        newPart->setOriginalPosition(pegPosition);
        newPart->attachToBody(attachment, pegBody);
        attachments[machineNumber][machineNumber] = attachment;
    }
}
PhysicsShape::PhysicsShape()
: _body(nullptr)
, _type(Type::UNKNOWN)
, _area(0.0f)
, _mass(0.0f)
, _moment(0.0f)
, _sensor(false)
, _scaleX(1.0f)
, _scaleY(1.0f)
, _newScaleX(1.0f)
, _newScaleY(1.0f)
, _tag(0)
, _categoryBitmask(UINT_MAX)
, _collisionBitmask(UINT_MAX)
, _contactTestBitmask(0)
, _group(0)
{
    if (s_sharedBody == nullptr)
    {
        s_sharedBody = cpBodyNewStatic();
    }
}
MachineSystem::MachineSystem(MachineSystem &original, cpVect position)
: space(cpSpaceNew()),
 size(original.size),
gridSpacing(original.gridSpacing),
inputMachinePosition(original.inputMachinePosition),
outputMachinePosition(original.outputMachinePosition),
nMachines(0),
nAttachments(0),
destroyAttachments(original.destroyAttachments)
{
    cpFloat width = (original.size.x +1)*original.gridSpacing.x;
    cpFloat height = (original.size.y +1)*original.gridSpacing.y;
   
    body = cpBodyNewStatic();
    cpBodySetPos(body, position);
    
    cpShape *wallShape = cpSegmentShapeNew(body, cpv(-width/2, height/2), cpv(width/2, height/2), .5);
    cpShapeSetLayers(wallShape, WALL_LAYER);
    cpSpaceAddStaticShape(space, wallShape);
    
    wallShape = cpSegmentShapeNew(body, cpv(-width/2, -height/2), cpv(+width/2, -height/2), 0.5);
    cpShapeSetLayers(wallShape, WALL_LAYER);
    cpSpaceAddStaticShape(space, wallShape);
    
    wallShape = cpSegmentShapeNew(body, cpv(-width/2, +height/2), cpv(-width/2, -height/2), 0.5);
    cpShapeSetLayers(wallShape, WALL_LAYER);
    cpSpaceAddStaticShape(space, wallShape);
    
    wallShape = cpSegmentShapeNew(body, cpv(+width/2, +height/2), cpv(+width/2, -height/2), 0.5);
    cpShapeSetLayers(wallShape, WALL_LAYER);
    cpSpaceAddStaticShape(space, wallShape);
    
    // do the copy of the attachment and machines manually, because we need to add and attach machines
    int nPegs = size.x*size.y;

    parts.resize(nPegs, NULL);
    
    attachments.resize(nPegs);
    
    for (int i=0; i<nPegs; i++)
        attachments[i].resize(nPegs, NULL);
    
    for (int x = 0; x < size.x; x++ ) {
        for (int y = 0; y < size.y; y++) {
            cpVect gridPos = cpv(x,y);
            int machineNum = machinePositionToNumber(gridPos);
            MachinePart *machineToCopy = original.parts[machineNum];
            if (machineToCopy) {
                MachinePart *newPart = new MachinePart(*machineToCopy, space);
                Attachment *wallAttachment = Attachment::copyAttachment(original.attachments[machineNum][machineNum]);
                addPart(newPart, wallAttachment, gridPos);
            }
        }
    }
    
    for (int i=0; i<nPegs; i++) {
        for (int j=0; j<i; j++) {
            Attachment *attachmentToCopy = original.attachments[i][j];
            if (attachmentToCopy) {
                Attachment *attachmentCopy = Attachment::copyAttachment(attachmentToCopy);
                cpVect machine1Pos = machineNumberToPosition(i);
                cpVect machine2Pos = machineNumberToPosition(j);
                
                attachMachines(machine1Pos, machine2Pos, attachmentCopy);
                attachmentCopy->innovationNumber = attachmentToCopy->innovationNumber;
            }
        }
    }
}
Example #17
0
cBody::cBody() :
	mBody( cpBodyNewStatic() ),
	mData( NULL )
{
	SetData();
}
Example #18
0
bool Unit::Create( int id, std::string proto )
{
	UnitId = id;

	if( !setUnitName( TypeName ) )
		return false;

	if( !Image.init( UnitName, TypeName ) )
		return false;

	if( proto == "" ){
		LuaConfig* cfg = new LuaConfig;
		cfg->getValue( "proto", UnitName, TypeName, proto );
		delete cfg;
	}
	if( proto == "" ){
		Debug::debug( Debug::UNIT, "Unit with blank prototype, creation failed. UID: "
						+ citoa(UnitId) + ".\n" );
		return false;
	}

	{
		ProtoManager* pm = new ProtoManager;
		Actions.setProto( pm->GetProtoByName( proto ) );
		Actions.setAction( "init" );
		delete pm;
		if( !Actions.loaded ){
				Debug::debug( Debug::UNIT, "Unit with invalid prototype '" + proto +
					"'. Creation failed. UID: " + citoa(UnitId) + ".\n" );
				return false;
		}
	}

	// Create physics

	if( Actions.proto->statical ){
		physBody = cpBodyNewStatic();
	}else{
		physBody = cpBodyNew( 1.0, 1.0 );
		physBody = cpSpaceAddBody( Phys::space, physBody );
	}
	physBody->data = this;



	if( Actions.proto->physicsType >= 0 && Actions.proto->physicsType < potLast )
		phys.type = (enum PhysObectType)Actions.proto->physicsType;
	else{
		Debug::debug( Debug::UNIT, "Bad physics object type: "
				+ citoa( Actions.proto->physicsType ) + ".\n" );
		phys.type = potNone;
	}

	cpShape* shape = NULL;
	switch( phys.type ){
		case potCircle:
			shape = cpCircleShapeNew( physBody, phys.radius, cpvzero );
			break;
		case potQuad:
			shape = cpBoxShapeNew( physBody, phys.sides.x, phys.sides.y );
			break;
		default:
			break;
	}
	if( shape != NULL ){
		physShape = cpSpaceAddShape( Phys::space, shape );
		physShape->collision_type = UnitType;
	}

	//physBody->velocity_func = call_velocity_func;

	return true;
}
Example #19
0
static VALUE
rb_cpBodyAllocStatic(VALUE klass) {
  cpBody *body = cpBodyNewStatic();
  return Data_Wrap_Struct(c_cpStaticBody, NULL, cpBodyFree, body);
}
Example #20
0
Body::Body(void)
	: body(cpBodyNewStatic()),
	  data(0)
{
		body->data = this;
}
Example #21
0
void ofxChipmunk::StaticBody::setup(cpSpace *space){
	Body::setup(space, cpBodyNewStatic());
}