Example #1
0
cpSpace *Slice::Init()
{
    ChipmunkDemo::Init();

    message = "Hold right bottom corner and slice with touch.";

    space = cpSpaceNew();
    cpSpaceSetIterations(space, 30);
    cpSpaceSetGravity(space, cpv(0, -500));
    cpSpaceSetSleepTimeThreshold(space, 0.5f);
    cpSpaceSetCollisionSlop(space, 0.5f);

    cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
    cpShape *shape;

    // Create segments around the edge of the screen.
    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-1000,-240), cpv(1000,-240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    cpFloat width = 200.0f;
    cpFloat height = 300.0f;
    cpFloat mass = width*height*DENSITY;
    cpFloat moment = cpMomentForBox(mass, width, height);

    body = cpSpaceAddBody(space, cpBodyNew(mass, moment));

    shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0));
    cpShapeSetFriction(shape, 0.6f);
	
	return space;
}
Example #2
0
/**
 * Initializes a new player.
 * The player is placed in the queue of available players.
 */
struct player *player_init(struct player *p, struct game *g, double x, double y,
                           double w, double h, uint32_t score, uint8_t data) {
	cpVect all[4] = {cpv(0,0), cpv(0,h), cpv(w,h), cpv(w,0)};
	cpBody *body = cpBodyInit(&p->body, 10, cpMomentForBox(10, w, h));
	if (!body) {
		ERR_ERRNO();
		return 0;
	}
	cpBodySetPos(body, cpv(x,y));
	//cpShape *shape = cpPolyShapeNew(body,4,all,cpv((p->l+p->r)/2.0,(p->b+p->t)/2.0));
	cpShape *shape = cpPolyShapeInit(&p->shape, body, 4, all, cpv(0, 0));
	if (!shape) {
		ERR_ERRNO();
		cpBodyDestroy(body);
		return 0;
	}
	shape->data = p;
	shape->collision_type = PLAYER;
	if (linkedlist_add_last(g->p_q, p)) {
		ERR_TRACE();
		cpBodyDestroy(body);
		cpShapeDestroy(shape);
		return 0;
	}
	p->x = x;
	p->y = y;
	p->node = g->p_q->last;
	return p;
}
Example #3
0
static cpSpace *
init(void)
{
	ChipmunkDemoMessageString = "Right click and drag to change the blocks's shape.";
	
	cpSpace *space = cpSpaceNew();
	cpSpaceSetIterations(space, 30);
	cpSpaceSetGravity(space, cpv(0, -500));
	cpSpaceSetSleepTimeThreshold(space, 0.5f);
	cpSpaceSetCollisionSlop(space, 0.5f);
	
	cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
	
	// Create segments around the edge of the screen.
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetLayers(shape, NOT_GRABABLE_MASK);

	cpFloat width = 50.0f;
	cpFloat height = 70.0f;
	cpFloat mass = width*height*DENSITY;
	cpFloat moment = cpMomentForBox(mass, width, height);
	
	body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
	
	shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height));
	cpShapeSetFriction(shape, 0.6f);
		
	return space;
}
Example #4
0
static cpSpace *
init(void)
{
	space = cpSpaceNew();
	cpSpaceSetGravity(space, cpv(0, -600));
	
	cpBody *body;
	cpShape *shape;
	
	// We create an infinite mass rogue body to attach the line segments too
	// This way we can control the rotation however we want.
	rogueBoxBody = cpBodyNew(INFINITY, INFINITY);
	cpBodySetAngVel(rogueBoxBody, 0.4f);
	
	// Set up the static box.
	cpVect a = cpv(-200, -200);
	cpVect b = cpv(-200,  200);
	cpVect c = cpv( 200,  200);
	cpVect d = cpv( 200, -200);
	
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, a, b, 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetLayers(shape, NOT_GRABABLE_MASK);

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, b, c, 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetLayers(shape, NOT_GRABABLE_MASK);

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, c, d, 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetLayers(shape, NOT_GRABABLE_MASK);

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(rogueBoxBody, d, a, 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetLayers(shape, NOT_GRABABLE_MASK);
	
	cpFloat mass = 1;
	cpFloat width = 60;
	cpFloat height = 30;
	
	// Add the bricks.
	for(int i=0; i<3; i++){
		for(int j=0; j<7; j++){
			body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height)));
			cpBodySetPos(body, cpv(i*60 - 150, j*30 - 150));
			
			shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height));
			cpShapeSetElasticity(shape, 0.0f);
			cpShapeSetFriction(shape, 0.7f);
		}
	}
	
	return space;
}
Example #5
0
cpFloat
cpMomentForBox2(cpFloat m, cpBB box)
{
	cpFloat width = box.r - box.l;
	cpFloat height = box.t - box.b;
	cpVect offset = cpvmult(cpv(box.l + box.r, box.b + box.t), 0.5f);
	
	return cpMomentForBox(m, width, height) + m*cpvlengthsq(offset);
}
Example #6
0
int lc_MomentForBox(lua_State *vm){
    //m, width, height -> number
    cpFloat m = 0, width = 0, height = 0;
    m = lua_tonumber(vm, 1);
    width = lua_tonumber(vm, 2);
    height = lua_tonumber(vm, 3);
    lua_pushnumber(vm, cpMomentForBox(m, width, height));
    return 1;
}
Example #7
0
bool AnimationLayer::init()
{
	bool bRet = false;

	do
	{
		CC_BREAK_IF(!Layer::init());

		SpriteFrameCache::getInstance()->addSpriteFramesWithFile("running.plist");
		spriteSheet = SpriteBatchNode::create("running.png");

		addChild(spriteSheet);

		//init running action
		Vector< SpriteFrame * > animFrames;

		for(int i = 0; i < 8; i++)
		{
			String str = String::createWithFormat("runner%d.png",i)->getCString();

			SpriteFrame*  frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(str.getCString());
			animFrames.pushBack(frame);
		}

		Animation *animation = Animation::createWithSpriteFrames(animFrames, 0.1);
		runningAction = RepeatForever::create(Animate::create(animation));


		//sprite = Sprite::createWithSpriteFrameName("runner0.png");
		//sprite->setPosition(ccp(80,85));

		sprite = PhysicsSprite::createWithSpriteFrameName("runner0.png");
		auto contentSize = sprite->getContentSize();

		//初始化身体
        this->body = cpBodyNew(1,cpMomentForBox(1, contentSize.width, contentSize.height));
        this->body->p = cpv(GlobalUtils::g_runnerStartX, GlobalUtils::g_groundHeight + contentSize.height/2);
		cpBodyApplyImpulse(this->body,cpv(150,0), cpv(0,0));
		cpSpaceAddBody(this->space, this->body);

		//init shape
        this->shape = cpBoxShapeNew(this->body,contentSize.width-14,contentSize.height);
        cpSpaceAddShape(this->space, this->shape);

        sprite->setCPBody(this->body);


        sprite->runAction(runningAction);
        spriteSheet->addChild(sprite);

        scheduleUpdate();

        bRet = true;
    }while(0);
	return bRet;
}
cpBody*
PhysicsWorld::AddRectBody(cpFloat width, cpFloat height, cpFloat xPosition, cpFloat yPosition, cpFloat mass)
{
    // Create and add the body
    cpBody *boxBody = cpSpaceAddBody(mySpace, cpBodyNew(mass, cpMomentForBox(mass, width, height)));
    // Set it's position in the world
    cpBodySetPos(boxBody, cpv(xPosition, yPosition));
    
    return boxBody;
}
Example #9
0
cpFloat
cpMomentForBox2(cpFloat m, cpBB box)
{
	cpFloat width = box.r - box.l;
	cpFloat height = box.t - box.b;
	cpVect offset = cpvmult(cpv(box.l + box.r, box.b + box.t), 0.5f);
	
	// TODO NaN when offset is 0 and m is INFINITY
	return cpMomentForBox(m, width, height) + m*cpvlengthsq(offset);
}
Example #10
0
static struct cpShapeMassInfo
cpSegmentShapeMassInfo(cpFloat mass, cpVect a, cpVect b, cpFloat r)
{
	struct cpShapeMassInfo info = {
		mass, cpMomentForBox(1.0f, cpvdist(a, b) + 2.0f*r, 2.0f*r), // TODO is an approximation.
		cpvlerp(a, b, 0.5f),
		cpAreaForSegment(a, b, r),
	};
	
	return info;
}
static cpSpace *
init(void)
{
	cpSpace *space = cpSpaceNew();
	cpSpaceSetIterations(space, 30);
	cpSpaceSetGravity(space, cpv(0, -100));
	cpSpaceSetSleepTimeThreshold(space, 0.5f);
	cpSpaceSetCollisionSlop(space, 0.5f);
	
	cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
	cpShape *shape;
	
	// Create segments around the edge of the screen.
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
	
	// Add lots of boxes.
	for(int i=0; i<14; i++){
		for(int j=0; j<=i; j++){
			body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForBox(1.0f, 30.0f, 30.0f)));
			cpBodySetPosition(body, cpv(j*32 - i*16, 300 - i*32));
			
			shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 30.0f, 30.0f, 0.5f));
			cpShapeSetElasticity(shape, 0.0f);
			cpShapeSetFriction(shape, 0.8f);
		}
	}
	
	// Add a ball to make things more interesting
	cpFloat radius = 15.0f;
	body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
	cpBodySetPosition(body, cpv(0, -240 + radius+5));

	shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
	cpShapeSetElasticity(shape, 0.0f);
	cpShapeSetFriction(shape, 0.9f);
	
	return space;
}
Example #12
0
// adds nonstatic box shape to space
// currently does not roate box
static cpShape *core_add_box_shape ( cpSpace *space, Box *box, const int index ) {
    
    // calculate mass and moment of a box
    cpFloat mass = box->density * box->width * box->height;
    cpFloat moment = cpMomentForBox ( mass, box->width, box->height );
    
    // add body with mass and moment of a square to space
    cpBody *body = cpBodyNew ( mass, moment );
    cpSpaceAddPostStepCallback ( space, (cpPostStepFunc) postStepAddBody, body, NULL );
    
    cpBodySetPos ( body, cpv ( box->x, box->y ) );
    
    // set index of body
    BodyInfo * bi = body_info_new(0);
    bi->index = index;
    bi->type = BOX_TYPE;
    bi->p1x = box->x - (box->width) / 2.0;
    bi->p1y = box->y + (box->height) / 2.0;
    bi->p2x = box->x + (box->width) / 2.0;;
    bi->p2y = box->y - (box->height) / 2.0;
    bi->color->r = box->color->r;
    bi->color->g = box->color->g;
    bi->color->b = box->color->b;
    bi->friction = box->friction;
    bi->density = box->density;
    bi->elasticity = box->elasticity;
    
    body->data = bi;
    
    double hw = ( box->width ) / 2.0;
    double hh = ( box->height ) / 2.0;
    cpVect cpv1 = cpv ( -hw,-hh );
    cpVect cpv2 = cpv ( -hw, hh );
    cpVect cpv3 = cpv ( hw, hh );
    cpVect cpv4 = cpv ( hw, -hh );
    
    cpVect verts [4]= { cpv1, cpv2, cpv3, cpv4 };
    
    // add box collision shape to body
    cpShape *boxShape = cpPolyShapeNew ( body, 4, verts, cpv ( 0, 0 ) );
    cpSpaceAddPostStepCallback ( space, (cpPostStepFunc) postStepAddShape, boxShape, NULL );
    
    cpShapeSetFriction ( boxShape, box->friction );
    cpShapeSetElasticity ( boxShape, box->elasticity );
    cpBodySetAngle ( body, box->angle);
    
    DrawShapeInfo *info = add_box_draw_shape_info ( box );
    boxShape->data= ( cpDataPointer ) info;
    return boxShape;
}
Example #13
0
static void
add_domino(cpSpace *space, cpVect pos, cpBool flipped)
{
	cpFloat mass = 1.0f;
	cpFloat moment = cpMomentForBox(mass, WIDTH, HEIGHT);
	
	cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
	cpBodySetPos(body, pos);

	cpShape *shape = (flipped ? cpBoxShapeNew(body, HEIGHT, WIDTH) : cpBoxShapeNew(body, WIDTH, HEIGHT));
	cpSpaceAddShape(space, shape);
	cpShapeSetElasticity(shape, 0.0f);
	cpShapeSetFriction(shape, 0.6f);
}
Example #14
0
static cpSpace *
init(void)
{
	cpResetShapeIdCounter();
	
	space = cpSpaceNew();
	space->iterations = 30;
	cpSpaceResizeStaticHash(space, 40.0f, 1000);
	cpSpaceResizeActiveHash(space, 40.0f, 1000);
	space->gravity = cpv(0, -100);
	space->sleepTimeThreshold = 0.5f;
	
	cpBody *body, *staticBody = &space->staticBody;
	cpShape *shape;
	
	// Create segments around the edge of the screen.
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	shape->layers = NOT_GRABABLE_MASK;
	
	// Add lots of boxes.
	for(int i=0; i<14; i++){
		for(int j=0; j<=i; j++){
			body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForBox(1.0f, 30.0f, 30.0f)));
			body->p = cpv(j*32 - i*16, 300 - i*32);
			
			shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 30.0f, 30.0f));
			shape->e = 0.0f; shape->u = 0.8f;
		}
	}
	
	// Add a ball to make things more interesting
	cpFloat radius = 15.0f;
	body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
	body->p = cpv(0, -240 + radius+5);

	shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
	shape->e = 0.0f; shape->u = 0.9f;
	
	return space;
}
Example #15
0
void weapon_init(Entity *ent) {
    if (!weapon_sprite) {
        ALLEGRO_PATH *path = game_asset_path("sword.png");
        weapon_sprite = al_load_bitmap(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
        al_destroy_path(path);
    }
    cpBody *body = entity_body(ent);

    cpShape *shape = cpBoxShapeNew(body, WEAPON_WIDTH, WEAPON_HEIGHT, 0);
    cpSpaceAddShape(game.space, shape);
    cpShapeSetFriction(shape, 1);
    cpShapeSetElasticity(shape, 0);

    cpBodySetMass(body, 1);
    cpBodySetMoment(body, cpMomentForBox(1, WEAPON_WIDTH, WEAPON_HEIGHT));
}
Example #16
0
static cpBody *
addChassis(cpVect pos, cpVect boxOffset)
{
	cpFloat mass = 5.0f;
	cpFloat width = 80;
	cpFloat height = 30;
	
	cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height)));
	cpBodySetPos(body, cpvadd(pos, boxOffset));
	
	cpShape *shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height));
	cpShapeSetElasticity(shape, 0.0f);
	cpShapeSetFriction(shape, 0.7f);
	cpShapeSetGroup(shape, 1); // use a group to keep the car parts from colliding
	
	return body;
}
Example #17
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;
}
Example #18
0
void init_physics()
{
	// setup space
	space = cpSpaceNew();

	// setup tires
	int i;
	for(i=0; i<1; i++) {
		cpFloat x = 0, y = 0, w = 0.5, h = 1.75;
		cpFloat mass = 50;
		cpFloat moment = cpMomentForBox(mass, w, h);
		tire[i] = cpSpaceAddBody(space, cpBodyNew(mass, moment));
		cpSpaceAddShape(space, cpBoxShapeNew2(tire[i], 
					cpBBNew(x-(w/2), y-(h/2), x+(w/2), y+(h/2))));
		cpSpaceReindexShapesForBody(space, tire[0]);
		cpBodySetAngle(tire[i], -M_PI/6);
		//cpBodySetVel(tire[i], cpv(0, 10));
		//cpBodyApplyImpulse(tire[i], cpv(0, 1000), cpvzero);
	}
}
Example #19
0
cpSpace *Chains::Init()
{
    ChipmunkDemo::Init();

    space = cpSpaceNew();
    cpSpaceSetIterations(space, 30);
    cpSpaceSetGravity(space, cpv(0, -100));
    cpSpaceSetSleepTimeThreshold(space, 0.5f);

    cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
    cpShape *shape;

    // Create segments around the edge of the screen.
    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    cpFloat mass = 1;
    cpFloat width = 20;
    cpFloat height = 30;

    cpFloat spacing = width*0.3;

    // Add lots of boxes.
    for(int i=0; i<CHAIN_COUNT; i++){
        cpBody *prev = NULL;

        for(int j=0; j<LINK_COUNT; j++){
            cpVect pos = cpv(40*(i - (CHAIN_COUNT - 1)/2.0), 240 - (j + 0.5)*height - (j + 1)*spacing);

            body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height)));
            cpBodySetPosition(body, pos);

            shape = cpSpaceAddShape(space, cpSegmentShapeNew(body, cpv(0, (height - width)/2.0), cpv(0, (width - height)/2.0), width/2.0));
            cpShapeSetFriction(shape, 0.8f);

            cpFloat breakingForce = 80000;

            cpConstraint *constraint = NULL;
            if(prev == NULL){
                constraint = cpSpaceAddConstraint(space, cpSlideJointNew(body, staticBody, cpv(0, height/2), cpv(pos.x, 240), 0, spacing));
            } else {
                constraint = cpSpaceAddConstraint(space, cpSlideJointNew(body, prev, cpv(0, height/2), cpv(0, -height/2), 0, spacing));
            }

            cpConstraintSetMaxForce(constraint, breakingForce);
            cpConstraintSetPostSolveFunc(constraint, BreakableJointPostSolve);
            cpConstraintSetCollideBodies(constraint, cpFalse);

            prev = body;
        }
    }

    cpFloat radius = 15.0f;
    body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
    cpBodySetPosition(body, cpv(0, -240 + radius+5));
    cpBodySetVelocity(body, cpv(0, 300));

    shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
    cpShapeSetElasticity(shape, 0.0f);
    cpShapeSetFriction(shape, 0.9f);
	
	return space;
}
Example #20
0
// add segment shape to the space 
cpShape *core_add_single_segment_shape ( cpSpace * space, const double p1x, const double p1y, const double p2x, const double p2y, Color *color, const double friction, const double elasticity, const double density, const int index ) {
    
    // calculate mass and moment of a box
    double length = sqrt ( ( p1x - p2x ) * ( p1x - p2x ) + ( p1y - p2y ) * ( p1y - p2y ) );
    cpFloat mass = density * SINGLE_SEGMENT_WIDTH * length;
    
    //if segment is too small
    if ( mass <= .01 ) {
        return NULL;
    }
    
    cpFloat moment = cpMomentForBox ( mass, SINGLE_SEGMENT_WIDTH, length );
    
    // add body with mass and moment of a square to space
    cpBody *body = cpBodyNew ( mass, moment );
    cpSpaceAddPostStepCallback ( space, (cpPostStepFunc)postStepAddBody, body, NULL );
    cpBodySetPos ( body, cpv ( ( p1x + p2x ) / 2, ( p1y + p2y ) / 2 ) );
    
    // add body info to body
    BodyInfo * bi = body_info_new(0);
    bi->index = index;
    bi->type = SINGLE_SEGMENT_TYPE;
    bi->p1x = p1x;
    bi->p1y = p1y;
    bi->p2x = p2x;
    bi->p2y = p2y;
    bi->color->r = color->r;
    bi->color->g = color->g;
    bi->color->b = color->b;
    bi->friction = friction;
    bi->density = density;
    bi->elasticity = elasticity;
    
    body->data = bi;
    
    double hw = SINGLE_SEGMENT_WIDTH / 2.0;
    double hh = length / 2.0;
    cpVect cpv1 = cpv ( -hw, -hh );
    cpVect cpv2 = cpv ( -hw, hh );
    cpVect cpv3 = cpv ( hw, hh );
    cpVect cpv4 = cpv ( hw, -hh );
    
    cpVect verts [4]= {cpv1, cpv2, cpv3, cpv4};
    
    
    
    // add box collision shape to body
    cpShape *boxShape = cpPolyShapeNew ( body, 4, verts, cpv ( 0, 0 ) );
    cpSpaceAddPostStepCallback ( space, (cpPostStepFunc) postStepAddShape, boxShape, NULL );
    cpShapeSetFriction ( boxShape, friction );
    cpShapeSetElasticity ( boxShape, elasticity );
    
    double angle = -atan((p1x-p2x)/(p1y-p2y));
    cpBodySetAngle( body, angle);
    
    DrawShapeInfo *info = (DrawShapeInfo *) malloc ( sizeof ( DrawShapeInfo ) );
    info->x = 0;
    info->y = 0;
    info->color = (Color *) malloc ( sizeof ( Color ) );
    info->color ->r = color->r;
    info->color->g = color->g;
    info->color->b = color->b;
    info->type = SINGLE_SEGMENT_TYPE;
    
    boxShape->data = ( cpDataPointer ) info;
    return boxShape;
}
Example #21
0
GLMFloat world_momentForBox(GLMFloat aMass, vec2_t aSize)
{
    return cpMomentForBox(aMass, aSize.w, aSize.h);
}
Example #22
0
File: physics.c Project: dns/CLove
/*
 * The moment is like the rotational mass of a body
 * Note: !This function may also set the mass of the object!
 */
static int l_physics_setBodyMoment(lua_State* state)
{
    l_tools_checkUserDataPlusErrMsg(state, 1, "You must provide a body");
    l_physics_Body* body = (l_physics_Body*)lua_touserdata(state, 1);

    /*
     * You can provide a custom moment
     * for this body *or* you can choose what
     * type of body this is and let chipmunk
     * calculate the value based on some attributes
     */

    int index = 2;

    cpFloat _mass = 0;
    cpFloat moment = 0;
    if (lua_type(state, 2) == LUA_TNUMBER)
    {
        moment = l_tools_toNumberOrError(state, 2);
    }
    else if (lua_type(state, 2) == LUA_TSTRING)
    {
        const char* whatFor = lua_tostring(state, 2);
        if (strcmp(whatFor, "box") == 0)
        {
            cpFloat mass = l_tools_toNumberOrError(state, 3);
            cpFloat width = l_tools_toNumberOrError(state, 4);
            cpFloat height = l_tools_toNumberOrError(state, 5);
            _mass = mass;
            moment = cpMomentForBox(mass, width, height);
        }
        else if (strcmp(whatFor, "circle") == 0)
        {
            cpFloat mass = l_tools_toNumberOrError(state, index++);
            cpFloat inner_radius = l_tools_toNumberOrError(state, index++);
            cpFloat outer_radius = l_tools_toNumberOrError(state, index++);
            cpVect offset = cpvzero;
            offset.x = l_tools_toNumberOrError(state, index++);
            offset.y = l_tools_toNumberOrError(state, index++);
            _mass = mass;
            moment = cpMomentForCircle(mass, inner_radius, outer_radius, offset);
        }
        else if (strcmp(whatFor, "segment") == 0)
        {

            cpFloat mass = l_tools_toNumberOrError(state, index++);

            cpVect a = cpvzero;
            a.x = l_tools_toNumberOrError(state, index++);
            a.y = l_tools_toNumberOrError(state, index++);
            cpVect b = cpvzero;
            b.x = l_tools_toNumberOrError(state, index++);
            b.y = l_tools_toNumberOrError(state, index++);

            cpFloat radius = l_tools_toNumberOrError(state, index++);
            _mass = mass;
            moment = cpMomentForSegment(mass, a, b, radius);
        }
    }

    if (_mass > 0)
        cpBodySetMass(body->body, _mass);

    cpBodySetMoment(body->body, moment);

    return 0;
}
Example #23
0
cpSpace *Buoyancy::Init()
{
    ChipmunkDemo::Init();

    space = cpSpaceNew();
    cpSpaceSetIterations(space, 30);
    cpSpaceSetGravity(space, cpv(0, -500));
    //	cpSpaceSetDamping(space, 0.5);
    cpSpaceSetSleepTimeThreshold(space, 0.5f);
    cpSpaceSetCollisionSlop(space, 0.5f);

    cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
    cpShape *shape;

    // Create segments around the edge of the screen.
    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
    cpShapeSetFriction(shape, 1.0f);
    cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

    {
        // Add the edges of the bucket
        cpBB bb = cpBBNew(-300, -200, 100, 0);
        cpFloat radius = 5.0f;

        shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(bb.l, bb.b), cpv(bb.l, bb.t), radius));
        cpShapeSetElasticity(shape, 1.0f);
        cpShapeSetFriction(shape, 1.0f);
        cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

        shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(bb.r, bb.b), cpv(bb.r, bb.t), radius));
        cpShapeSetElasticity(shape, 1.0f);
        cpShapeSetFriction(shape, 1.0f);
        cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

        shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(bb.l, bb.b), cpv(bb.r, bb.b), radius));
        cpShapeSetElasticity(shape, 1.0f);
        cpShapeSetFriction(shape, 1.0f);
        cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

        // Add the sensor for the water.
        shape = cpSpaceAddShape(space, cpBoxShapeNew2(staticBody, bb, 0.0));
        cpShapeSetSensor(shape, cpTrue);
        cpShapeSetCollisionType(shape, 1);
    }


    {
        cpFloat width = 200.0f;
        cpFloat height = 50.0f;
        cpFloat mass = 0.3*FLUID_DENSITY*width*height;
        cpFloat moment = cpMomentForBox(mass, width, height);

        body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
        cpBodySetPosition(body, cpv(-50, -100));
        cpBodySetVelocity(body, cpv(0, -100));
        cpBodySetAngularVelocity(body, 1);

        shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0));
        cpShapeSetFriction(shape, 0.8f);
    }

    {
        cpFloat width = 40.0f;
        cpFloat height = width*2;
        cpFloat mass = 0.3*FLUID_DENSITY*width*height;
        cpFloat moment = cpMomentForBox(mass, width, height);

        body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
        cpBodySetPosition(body, cpv(-200, -50));
        cpBodySetVelocity(body, cpv(0, -100));
        cpBodySetAngularVelocity(body, 1);

        shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0));
        cpShapeSetFriction(shape, 0.8f);
    }

    cpCollisionHandler *handler = cpSpaceAddCollisionHandler(space, 1, 0);
    handler->preSolveFunc = (cpCollisionPreSolveFunc)WaterPreSolve;
    handler->userData = this;
	
	return space;
}
Example #24
0
static cpSpace *
init(void)
{
	ChipmunkDemoMessageString = "Control the crane by moving the mouse. Press the down arrow to release.";
	
	space = cpSpaceNew();
	cpSpaceSetIterations(space, 30);
	cpSpaceSetGravity(space, cpv(0, -100));
	cpSpaceSetDamping(space, 0.8);
	
	cpBody *staticBody = cpSpaceGetStaticBody(space);
	cpShape *shape;
	
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 1.0f);
	cpShapeSetLayers(shape, NOT_GRABABLE_MASK);
	
	// Add a body for the dolly.
	dollyBody = cpSpaceAddBody(space, cpBodyNew(10, INFINITY));
	cpBodySetPos(dollyBody, cpv(0, 100));
	
	// Add a block so you can see it.
	cpSpaceAddShape(space, cpBoxShapeNew(dollyBody, 30, 30));
	
	// Add a groove joint for it to move back and forth on.
	cpSpaceAddConstraint(space, cpGrooveJointNew(staticBody, dollyBody, cpv(-250, 100), cpv(250, 100), cpvzero));
	
	// Add a pivot joint to act as a servo motor controlling it's position
	// By updating the anchor points of the pivot joint, you can move the dolly.
	dollyServo = cpSpaceAddConstraint(space, cpPivotJointNew(staticBody, dollyBody, cpBodyGetPos(dollyBody)));
	// Max force the dolly servo can generate.
	cpConstraintSetMaxForce(dollyServo, 10000);
	// Max speed of the dolly servo
	cpConstraintSetMaxBias(dollyServo, 100);
	// You can also change the error bias to control how it slows down.
	//cpConstraintSetErrorBias(dollyServo, 0.2);
	
	
	// Add the crane hook.
	cpBody *hookBody = cpSpaceAddBody(space, cpBodyNew(1, INFINITY));
	cpBodySetPos(hookBody, cpv(0, 50));
	
	// Add a sensor shape for it. This will be used to figure out when the hook touches a box.
	shape = cpSpaceAddShape(space, cpCircleShapeNew(hookBody, 10, cpvzero));
	cpShapeSetSensor(shape, cpTrue);
	cpShapeSetCollisionType(shape, HOOK_SENSOR);
	
	// Add a slide joint to act as a winch motor
	// By updating the max length of the joint you can make it pull up the load.
	winchServo = cpSpaceAddConstraint(space, cpSlideJointNew(dollyBody, hookBody, cpvzero, cpvzero, 0, INFINITY));
	// Max force the dolly servo can generate.
	cpConstraintSetMaxForce(winchServo, 30000);
	// Max speed of the dolly servo
	cpConstraintSetMaxBias(winchServo, 60);
	
	// TODO cleanup
	// Finally a box to play with
	cpBody *boxBody = cpSpaceAddBody(space, cpBodyNew(30, cpMomentForBox(30, 50, 50)));
	cpBodySetPos(boxBody, cpv(200, -200));
	
	// Add a block so you can see it.
	shape = cpSpaceAddShape(space, cpBoxShapeNew(boxBody, 50, 50));
	cpShapeSetFriction(shape, 0.7);
	cpShapeSetCollisionType(shape, CRATE);
	
	cpSpaceAddCollisionHandler(space, HOOK_SENSOR, CRATE, (cpCollisionBeginFunc)HookCrate, NULL, NULL, NULL, NULL);
	
	return space;
}
Example #25
0
void Truck::setupChipmunk3() {
    DoodleTruck * doodleTruck = DoodleTruck::sharedDoodleTruck();
    cpSpace *space = doodleTruck->getSpace();
    
    cpVect s_p = cpv(doodleTruck->getScaleY(), doodleTruck->getScaleY());
    double mass = TRUCK_MASS;
    
    //body
    cpVect wd_ht_half = cpv(248, 84);
    cpBody *body = cpBodyNew(mass, cpMomentForBox(mass, wd_ht_half.x * 2 * doodleTruck->getScaleX(), wd_ht_half.y * 2 * doodleTruck->getScaleY()));
    int num = 4;
    cpVect body_verts1[] = {
        cpv((286 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 5) * s_p.y),
        cpv((430 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 5) * s_p.y),
        cpv((430 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 70) * s_p.y),
        cpv((255 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 70) * s_p.y)
    };
    
    cpShape *shape = cpPolyShapeNew(body, num, body_verts1, cpvzero);
    shape->e = TRUCK_BODY_E; shape->u = TRUCK_BODY_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
    shape->data = bodySprite;
    cpSpaceAddShape(space, shape);
    
    num = 5;
    cpVect body_verts2[] = {
        cpv((255 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 70) * s_p.y),
        cpv((490 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 70) * s_p.y),
        cpv((490 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 165) * s_p.y),
        cpv((455 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 165) * s_p.y),
        cpv((255 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 110) * s_p.y)
    };
    
    shape = cpPolyShapeNew(body, num, body_verts2, cpvzero);
    shape->e = TRUCK_BODY_E; shape->u = TRUCK_BODY_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
    cpSpaceAddShape(space, shape);
    
    num = 4;
    cpVect body_verts3[] = {
        cpv((255 - wd_ht_half.x) * s_p.x , (wd_ht_half.y - 85) * s_p.y),
        cpv((255 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 130) * s_p.y),
        cpv((70 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 130) * s_p.y),
        cpv((20 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 85) * s_p.y)
    };
    
    shape = cpPolyShapeNew(body, num, body_verts3, cpvzero);
    shape->e = TRUCK_BODY_E; shape->u = TRUCK_BODY_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
    cpSpaceAddShape(space, shape);
    
    cpVect body_verts4[] = {
        cpv((0 - wd_ht_half.x) * s_p.x , (wd_ht_half.y - 48) * s_p.y),
        cpv((20 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 48) * s_p.y),
        cpv((40 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 85) * s_p.y),
        cpv((20 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 85) * s_p.y)
    };
    
    shape = cpPolyShapeNew(body, num, body_verts4, cpvzero);
    shape->e = TRUCK_BODY_E; shape->u = TRUCK_BODY_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
    cpSpaceAddShape(space, shape);
    
    body->p = cpv(bodySprite->getPosition().x, bodySprite->getPosition().y);
    cpSpaceAddBody(space, body);
    
    mass = TRUCK_LEFT_WHEELMASS;
    //left wheel
    cpBody * l_wheel = cpBodyNew(mass, cpMomentForCircle(mass,
                                                         TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(),
                                                         TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(),
                                                         cpvzero));
    shape = cpCircleShapeNew(l_wheel, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero);
    shape->e = TRUCK_WHEEL_E; shape->u = TRUCK_WHEEL_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->data	= lWheelSprite;
    
    cpSpaceAddShape(space, shape);
    l_wheel->p = cpv(lWheelSprite->getPosition().x, lWheelSprite->getPosition().y);
    cpSpaceAddBody(space, l_wheel);
    
    mass = TRUCK_RIGHT_WHEELMASS;
    //right wheel
    cpBody * r_wheel = cpBodyNew(mass, cpMomentForCircle(mass,
                                                         TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(),
                                                         TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(),
                                                         cpvzero));
    
    shape = cpCircleShapeNew(r_wheel, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero);
    shape->e = TRUCK_WHEEL_E; shape->u = TRUCK_WHEEL_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->data	= rWheelSprite;
    cpSpaceAddShape(space, shape);
    r_wheel->p = cpv(rWheelSprite->getPosition().x, rWheelSprite->getPosition().y);
    cpSpaceAddBody(space, r_wheel);
    
    cpVect l_wheel_offset = cpv(-100 * doodleTruck->getScaleY(), -50 * doodleTruck->getScaleY());
    cpVect r_wheel_offset = cpv(130 * doodleTruck->getScaleY(), -50 * doodleTruck->getScaleY());
    cpSpaceAddConstraint(space, cpGrooveJointNew(body, l_wheel, l_wheel_offset, cpv(l_wheel_offset.x, 0), cpvzero));
    cpSpaceAddConstraint(space, cpGrooveJointNew(body, r_wheel, r_wheel_offset, cpv(r_wheel_offset.x, 0), cpvzero));
    cpVect pt_reverse = cpv(1, 0);
    
    cpSpaceAddConstraint(space, cpDampedSpringNew(body, l_wheel,
                                                  cpv(l_wheel_offset.x * pt_reverse.x, l_wheel_offset.y * pt_reverse.y),
                                                  cpvzero, fabs(l_wheel_offset.y),
                                                  TRUCK_LEFT_SPRING_STIFFNESS,
                                                  TRUCK_SPRING_DAMPING));
    
    cpSpaceAddConstraint(space, cpDampedSpringNew(body, r_wheel,
                                                  cpv(r_wheel_offset.x * pt_reverse.x, r_wheel_offset.y * pt_reverse.y),
                                                  cpvzero, fabs(r_wheel_offset.y),
                                                  TRUCK_RIGHT_SPRING_STIFFNESS,
                                                  TRUCK_SPRING_DAMPING));
    
    cpConstraint * motor = cpSimpleMotorNew(body, l_wheel, 0);
    doodleTruck->setMotor((cpSimpleMotor *)motor);
    cpSpaceAddConstraint(doodleTruck->getSpace(), motor);
    
    doodleTruck->setBody(body);
    doodleTruck->setLeftW(l_wheel);
    doodleTruck->setRightW(r_wheel);
}
Example #26
0
void Truck::setupChipmunk1() {
    
    DoodleTruck * doodleTruck = DoodleTruck::sharedDoodleTruck();
    cpSpace *space = doodleTruck->getSpace();
    
    Vec2 s_p = Vec2(doodleTruck->getScaleY(), doodleTruck->getScaleY());
    double mass = TRUCK_MASS;
    
    //body
    Vec2 wd_ht_half = Vec2(128, 64);
    cpBody *body = cpBodyNew(mass, cpMomentForBox(mass, wd_ht_half.x * 2 * doodleTruck->getScaleX(), wd_ht_half.y * 2 * doodleTruck->getScaleY()));
    
    int num = 6;
    cpVect body_verts1[] = {
        cpv(-115.4f * doodleTruck->getScaleY(), -26.0f * doodleTruck->getScaleY()),
        cpv(-114.9f * doodleTruck->getScaleY(), 14.0f * doodleTruck->getScaleY()),
        cpv(68.9f * doodleTruck->getScaleY(), 14.5f * doodleTruck->getScaleY()),
        cpv(124.8f * doodleTruck->getScaleY(), 1.0f * doodleTruck->getScaleY()),
        cpv(125.8f * doodleTruck->getScaleY(), -46.5f * doodleTruck->getScaleY()),
        cpv(111.8f * doodleTruck->getScaleY(), -57.4f * doodleTruck->getScaleY()),
        cpv(40.0f * doodleTruck->getScaleY(), -57.4f * doodleTruck->getScaleY())
    };
    
    cpShape *shape = cpPolyShapeNew(body, num, body_verts1, cpvzero);
    shape->e = TRUCK_BODY_E;
    shape->u = TRUCK_BODY_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
    shape->data = bodySprite;
    cpSpaceAddShape(space, shape);
    
    num = 5;
    cpVect body_verts2[] = {
        cpv(46.2f * doodleTruck->getScaleY(), 42.3f * doodleTruck->getScaleY()),
        cpv(69.1f * doodleTruck->getScaleY(), 12.0f * doodleTruck->getScaleY()),
        cpv(53.9f * doodleTruck->getScaleY(), 11.3f * doodleTruck->getScaleY()),
        cpv(37.0f * doodleTruck->getScaleY(), 34.9f * doodleTruck->getScaleY()),
        cpv(39.1f * doodleTruck->getScaleY(), 41.6f * doodleTruck->getScaleY())
    };
    
    shape = cpPolyShapeNew(body, num, body_verts2, cpvzero);
    shape->e = TRUCK_BODY_E; shape->u = TRUCK_BODY_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
    cpSpaceAddShape(space, shape);
    
    body->p.x = bodySprite->getPosition().x;
    body->p.y = bodySprite->getPosition().y;
    cpSpaceAddBody(space, body);
    
    // ------------ driver body------------
    mass = 0.5;
    
    cpBody * driver_body = cpBodyNew(mass, cpMomentForBox(mass, 27 * doodleTruck->getScaleX(), 60 * doodleTruck->getScaleY()));
    
    num = 8;
    cpVect body_verts4[] = {
        cpv(-23.0f, 31.2f),
        cpv(-12.0f, 31.2f),
        cpv(-3.5f, 14.0f),
        cpv(-1.5f, -24.0f),
        cpv(-8.0f, -31.0f),
        cpv(-27.5f, -30.5f),
        cpv(-31.0f, -25.2f),
        cpv(-30.2f, 19.2f)
    };
    
    shape = cpPolyShapeNew(driver_body, num, body_verts4, cpvzero);
    shape->e = 0; shape->u = TRUCK_BODY_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
    shape->data = driver_Body;
    cpSpaceAddShape(space, shape);
    
    num = 4;
    cpVect body_verts5[] = {
        cpv(-2.7f, 8.3f),
        cpv(17.1f, 8.8f),
        cpv(18.5f, -1.6f),
        cpv(-3.0f, -4.1f)
    };
    
    shape = cpPolyShapeNew(driver_body, num, body_verts5, cpvzero);
    shape->e = 0; shape->u = TRUCK_BODY_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
   // shape->data = driver_Body;
    cpSpaceAddShape(space, shape);
    
    num = 5;
    cpVect body_verts6[] = {
        cpv(16.9f, 8.5f),
        cpv(26.1f, 13.1f),
        cpv(32.0f, 6.7f),
        cpv(26.8f, -1.4f),
        cpv(18.0f, -0.7f)
    };
    
    shape = cpPolyShapeNew(driver_body, num, body_verts6, cpvzero);
    shape->e = 0; shape->u = TRUCK_BODY_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
     shape->data = driver_Body;
    cpSpaceAddShape(space, shape);
    
    driver_body->p.x = driver_Body->getPosition().x;
    driver_body->p.y = driver_Body->getPosition().y;
    cpSpaceAddBody(space, driver_body);
    
    // ------- driver-head------------
    mass = 0.3;
    
    cpBody * driver_head = cpBodyNew(mass, cpMomentForCircle(mass, 20 * doodleTruck->getScaleY(), 20 * doodleTruck->getScaleY(), cpvzero));
    
    num = 4;
    cpVect body_verts7[] = {
        cpv(9.9f, -15.6f),
        cpv(8.3f, -24.0f),
        cpv(-6.7f, -23.0f),
        cpv(-6.0f, -11.3f)
    };

    shape = cpPolyShapeNew(driver_head, num, body_verts7, cpvzero);
    shape->e = 0; shape->u = TRUCK_BODY_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
    shape->data = driver_Head;
    cpSpaceAddShape(space, shape);
    
    num = 7;
    cpVect body_verts8[] = {
        cpv(3.2f, 30.7f),
        cpv(26.5f, 8.0f),
        cpv(19.7f, -12.0f),
        cpv(9.5f, -16.2f),
        cpv(-13.5f, -9.5f),
        cpv(-20.7f, 17.7f),
        cpv(-12.2f, 29.2f)
    };
    
    
    shape = cpPolyShapeNew(driver_head, num, body_verts8, cpvzero);
    shape->e = 0; shape->u = TRUCK_BODY_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
     shape->data = driver_Body;
    cpSpaceAddShape(space, shape);
    
    driver_head->p.x = driver_Head->getPosition().x;
    driver_head->p.y = driver_Head->getPosition().y;
    cpSpaceAddBody(space, driver_head);
    
  /*
    */
    
    mass = TRUCK_LEFT_WHEELMASS;
    //left wheel
    cpBody * l_wheel = cpBodyNew(mass, cpMomentForCircle(mass, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero));
    shape = cpCircleShapeNew(l_wheel, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero);
    shape->e = TRUCK_WHEEL_E; shape->u = TRUCK_WHEEL_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
    shape->data	= lWheelSprite;
    
    cpSpaceAddShape(space, shape);
    l_wheel->p.x = lWheelSprite->getPosition().x;
    l_wheel->p.y = lWheelSprite->getPosition().y;
    cpSpaceAddBody(space, l_wheel);
    
    mass = TRUCK_RIGHT_WHEELMASS;
    //right wheel
    cpBody * r_wheel = cpBodyNew(mass, cpMomentForCircle(mass, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero));
    shape = cpCircleShapeNew(r_wheel, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero);
    shape->e = TRUCK_WHEEL_E; shape->u = TRUCK_WHEEL_U;
    shape->group = BOX2D_TRUCK_GROUP;
    shape->collision_type = BODY_COLLISION;
    shape->data	= rWheelSprite;
    cpSpaceAddShape(space, shape);
    r_wheel->p.x = rWheelSprite->getPosition().x;
    r_wheel->p.y = rWheelSprite->getPosition().y;
    cpSpaceAddBody(space, r_wheel);
    
    cpVect l_wheel_offset = cpv(-80 * doodleTruck->getScaleY(), -70 * doodleTruck->getScaleY());
    
     cpVect r_wheel_offset = cpv(80 * doodleTruck->getScaleY(), -70 * doodleTruck->getScaleY());
    
    cpSpaceAddConstraint(space, cpGrooveJointNew(body, l_wheel, l_wheel_offset, cpv(l_wheel_offset.x, 0), cpvzero));
    cpSpaceAddConstraint(space, cpGrooveJointNew(body, r_wheel, r_wheel_offset, cpv(r_wheel_offset.x, 0), cpvzero));
    
    cpVect pt_reverse = cpv(1.0, 0);
    
    cpSpaceAddConstraint(space, cpDampedSpringNew(body, l_wheel,
                                                  cpv(l_wheel_offset.x * pt_reverse.x, l_wheel_offset.y * pt_reverse.y),
                                                  cpvzero, fabs(l_wheel_offset.y),
                                                  TRUCK_LEFT_SPRING_STIFFNESS, TRUCK_SPRING_DAMPING));
    
    cpSpaceAddConstraint(space, cpDampedSpringNew(body, r_wheel,
                                                  cpv(r_wheel_offset.x * pt_reverse.x, r_wheel_offset.y * pt_reverse.y),
                                                  cpvzero, fabs(r_wheel_offset.y),
                                                  TRUCK_RIGHT_SPRING_STIFFNESS, TRUCK_SPRING_DAMPING));
    
    
    //cpVect l_wheel_offset = cpv(-80 * doodleTruck->getScaleY(), -60 * doodleTruck->getScaleY());
    
   // cpVect r_wheel_offset = cpv(80 * doodleTruck->getScaleY(), -60 * doodleTruck->getScaleY());
    
   // cpSpaceAddConstraint(space, cpGrooveJointNew(driver_body, driver_head, cpv(-18, 31), cpv(1.5, -25), cpvzero));
    cpSpaceAddConstraint(space, cpSlideJointNew(driver_body, driver_head, cpv(-17.7, 30.7), cpv(1.2, -13.3), 0.3, 1));
//   cpSpaceAddConstraint(space, cpGrooveJointNew(body, driver_body, cpv(0, -5), cpv(0, 35), cpvzero));
    //cpSpaceAddConstraint(space, cpGrooveJointNew(body, driver_body, cpv(10, 5), cpv(15, 5), cpvzero));
    
   
    
   /* cpSpaceAddConstraint(space, cpGrooveJointNew(body, l_wheel, l_wheel_offset, cpv(l_wheel_offset.x, 0), cpvzero));
    cpSpaceAddConstraint(space, cpGrooveJointNew(body, r_wheel, r_wheel_offset, cpv(r_wheel_offset.x, 0), cpvzero));
    */
    
    
    


    
  /*  cpSpaceAddConstraint(space, cpDampedSpringNew(driver_body, driver_head,
                                                  cpv(0, -5), cpv(0, 30),
                                                  fabs(10),
                                                  2000, 3000));
   
    cpSpaceAddConstraint(space, cpRotaryLimitJointNew(body, driver_body, -0.2, 0.3));
    cpSpaceAddConstraint(space, cpRotaryLimitJointNew(driver_body, driver_head, -0.2, 0.3));
    
   
    
    */
    cpConstraint * motor = cpSimpleMotorNew(body, l_wheel, 0);
    doodleTruck->setMotor((cpSimpleMotor *)motor);
    cpSpaceAddConstraint(doodleTruck->getSpace(), motor);
    
    doodleTruck->setBody(body);
    doodleTruck->setLeftW (l_wheel);
    doodleTruck->setRightW(r_wheel);
}
Example #27
0
static VALUE
rb_cpMomentForBox(VALUE self, VALUE m, VALUE w, VALUE h) {
  cpFloat i = cpMomentForBox(NUM2DBL(m), NUM2DBL(w), NUM2DBL(h));
  return rb_float_new(i);
}
Example #28
0
static cpSpace *
init(void)
{
	cpSpace *space = cpSpaceNew();
	cpSpaceSetIterations(space, 5);
	space->damping = 0.1;
	
	cpFloat mass = 1.0f;
	
	{
		cpFloat size = 100.0;
		
		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size)));
		cpBodySetPosition(body, cpv(100.0, 50.0f));
		
		shape1 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0));
		shape1->group = 1;
	}{
		cpFloat size = 100.0;
		
		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size)));
		cpBodySetPosition(body, cpv(120.0, -40.0f));
		cpBodySetAngle(body, 1e-2);
		
		shape2 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0));
		shape2->group = 1;
	}
	
//	{
//		cpFloat size = 100.0;
//		const int NUM_VERTS = 5;
//		
//		cpVect verts[NUM_VERTS];
//		for(int i=0; i<NUM_VERTS; i++){
//			cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
//			verts[i] = cpv(size/2.0*cos(angle), size/2.0*sin(angle));
//		}
//		
//		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
//		cpBodySetPosition(body, cpv(100.0, 50.0f));
//		
//		shape1 = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
//		shape1->group = 1;
//	}
//	{
//		cpFloat size = 100.0;
//		const int NUM_VERTS = 4;
//		
//		cpVect verts[NUM_VERTS];
//		for(int i=0; i<NUM_VERTS; i++){
//			cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
//			verts[i] = cpv(size/2.0*cos(angle), size/2.0*sin(angle));
//		}
//		
//		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
//		cpBodySetPosition(body, cpv(100.0, -50.0f));
//		
//		shape2 = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
//		shape2->group = 1;
//	}
//	
//	{
//		cpFloat size = 150.0;
//		cpFloat radius = 25.0;
//		
//		cpVect a = cpv( size/2.0, 0.0);
//		cpVect b = cpv(-size/2.0, 0.0);
//		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b)));
//		cpBodySetPosition(body, cpv(0, 25));
//		
//		shape1 = cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, radius));
//		shape1->group = 1;
//	}
//	{
//		cpFloat radius = 50.0;
//		
//		cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)));
//		cpBodySetPosition(body, cpv(0, -25));
//		
//		shape2 = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
//		shape2->group = 1;
//	}

	return space;
}
Example #29
0
File: main.cpp Project: GaaH/ecs
int main(int argc, char *argv[])
{
  /*
  sf::RenderWindow app(sf::VideoMode(640, 480), "ECS");
  sf::RectangleShape rectangle(sf::Vector2f(50.f, 50.f));
  rectangle.setFillColor(sf::Color::Blue);

  sf::RectangleShape rectangle2(sf::Vector2f(50.f, 50.f));
  rectangle2.setFillColor(sf::Color::Red);

  GameObject objects[2];

  DescriptionSystem desc_sys;
  MovementSystem move_sys;
  RenderSystem renderer(&app);
  CollisionSystem collision_sys;

  DescriptionComponent desc(objects[0].getId());
  PositionComponent pos(objects[0].getId()), pos2(objects[1].getId());
  VelocityComponent vel(objects[0].getId(), 1.f), vel2(objects[1].getId(), 0.2f, 0.1f);
  DrawableComponent drawable(objects[0].getId()), drawable2(objects[1].getId());
  CollidableComponent c1(objects[0].getId(), rectangle.getGlobalBounds()), c2(objects[1].getId(), rectangle2.getGlobalBounds());
  drawable.drawable = &rectangle;
  drawable2.drawable = &rectangle2;

  //pos.position.y = 50.f;
  pos2.position.x = 250.f;
  pos2.position.y = 20.f;

  desc_sys.registerComponent("Description");

  move_sys.registerComponent("Position");
  move_sys.registerComponent("Velocity");

  renderer.registerComponent("Drawable");
  renderer.registerComponent("Position");

  collision_sys.registerComponent("Position");
  collision_sys.registerComponent("Collidable");

  objects[0].addComponent(&desc);
  objects[0].addComponent(&pos);
  objects[0].addComponent(&vel);
  objects[0].addComponent(&drawable);
  objects[0].addComponent(&c1);

  objects[1].addComponent(&vel2);
  objects[1].addComponent(&drawable2);
  objects[1].addComponent(&pos2);
  objects[1].addComponent(&c2);

  if (desc_sys.canUpdate(objects[0]))
    desc_sys.update(objects[0]);

  sf::Event event;
  while (app.isOpen())
    {
      while (app.pollEvent(event))
	{
	  if (event.type == sf::Event::Closed)
	    app.close();
	}

      for (int i(0) ; i < 2 ; ++i)
	if (move_sys.canUpdate(objects[i]))
	  move_sys.update(objects[i]);


      app.clear(sf::Color::White);

      for (int i(0) ; i < 2 ; ++i)
	if (collision_sys.canUpdate(objects[i]))
	  collision_sys.update(objects[i]);

      for (int i(0) ; i < 2 ; ++i)
	if (renderer.canUpdate(objects[i]))
	  renderer.update(objects[i]);

      app.display();
    }
*/

  GameWorld world;
  GameObject *o1(world.addObject());
  GameObject *o2(world.addObject());

  sf::RectangleShape rectangle1(sf::Vector2f(50.f, 50.f));
  rectangle1.setFillColor(sf::Color::Blue);

  sf::RectangleShape rectangle2(sf::Vector2f(50.f, 50.f));
  rectangle2.setFillColor(sf::Color::Red);

  PhysicSystem *phys_sys(new PhysicSystem);
  phys_sys->registerComponent("Physic");
  phys_sys->registerComponent("Position");
  world.addSystem(phys_sys);

  PhysicComponent phys1(o1->getId(), 50, cpMomentForBox(50, 5, 5)), phys2(o2->getId(), 50, cpMomentForBox(50, 5, 5));
  DescriptionComponent desc1(o1->getId()), desc2(o2->getId());
  PositionComponent pos1(o1->getId()), pos2(o2->getId());
  //VelocityComponent vel1(objects[0].getId(), 1.f), vel2(objects[1].getId(), 0.2f, 0.1f);
  DrawableComponent drawable1(o1->getId()), drawable2(o2->getId());
  //CollidableComponent c1(objects[0].getId(), rectangle.getGlobalBounds()), c2(objects[1].getId(), rectangle2.getGlobalBounds());

  phys1.shape = cpBoxShapeNew(phys1.body, 50.f, 50.f);
  phys2.shape = cpBoxShapeNew(phys2.body, 50.f, 50.f);
  cpBodySetPos(phys1.body, cpv(0, 0));
  cpBodySetPos(phys2.body, cpv(200, 0));

  cpSpaceAddShape(phys_sys->getSpace(), phys1.shape);
  cpSpaceAddShape(phys_sys->getSpace(), phys2.shape);

  cpSpaceAddBody(phys_sys->getSpace(), phys1.body);
  cpSpaceAddBody(phys_sys->getSpace(), phys2.body);

  drawable1.drawable = &rectangle1;
  drawable2.drawable = &rectangle2;

  pos1.position.x = 50.f;
  pos2.position = sf::Vector2f(200.f, 150.f);

  o1->addComponent(&desc1);
  o1->addComponent(&drawable1);
  o1->addComponent(&pos1);
  o1->addComponent(&phys1);

  o2->addComponent(&desc2);
  o2->addComponent(&drawable2);
  o2->addComponent(&pos2);
  o2->addComponent(&phys2);

  while (true)
    {
      world.update();
    }

  return 0;
}