Example #1
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 #2
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;
}
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);
}
Example #4
0
WorldShape_t *worldShape_createBox(vec2_t aSize)
{
    WorldShape_t *out = obj_create_autoreleased(&Class_WorldShape);
    out->cpShape = cpBoxShapeNew(NULL, aSize.w, aSize.h);
    out->cpShape->data = out;
    return out;
}
Example #5
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 #6
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 #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;
}
Example #8
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;
}
Example #9
0
/**
 * Creates and sets up the hero entity
 * @return The hero entity created
 */
Entity_T *SetupHero()
{
	int i, x;
	const GLfloat SPRITE_WIDTH = 32.0f / 128;
	const GLfloat SPRITE_HEIGHT = 48.0f / 192;
	Rect frames[20];
	Animation_T animations[MAX_ANIMATIONS];
	Entity_T *e = NewEntity();
	e->currentAnimation = 0;
	e->currentFrame = 0;
	e->sprite = NewSprite();
	e->body = cpBodyNew(100.0f, 100.0f);
	e->size.x = TILE_WIDTH;
	e->size.y = 1.5f * TILE_HEIGHT;
	e->shape = cpBoxShapeNew(e->body, e->size.x/2, e->size.y/2);
	

	for(i = 0;i < 20;i++)
	{
		frames[i].x = (i % 4) * SPRITE_WIDTH;
		frames[i].y = 1.0f - (i/4+1) * SPRITE_HEIGHT;
		frames[i].w = SPRITE_WIDTH;
		frames[i].h = SPRITE_HEIGHT;
	}
	
	for(x = 0;x < 4;x++)
	{
		animations[x].numFrames = 1;
		animations[x].frames[0] = x*4;
		animations[x].frameLengths[0] = 100;
		animations[x].flags = ANIMFLAG_NONE;
	}

	for(x = 4;x < 8;x++){
		animations[x].numFrames = 4;
		for(i = 0;i < 4;i++){
			animations[x].frames[i] = (x-4)*4+i;
			animations[x].frameLengths[i] = 100;
		}
		animations[x].flags = ANIMFLAG_NONE;
	}
	SetupSprite(e->sprite, "hero.png", frames, 20, animations, 8);
	//Set up the hero's inner light
	e->light = NewLight();
	e->light->brightness = 0.75f;
	e->light->color.x = 1.0f;
	e->light->color.y = .95f;
	e->light->color.z = 0.5f;
	e->light->span = TILE_WIDTH / 2;
	e->light->offset.x = TILE_WIDTH / 2;
	e->light->offset.y = 0;
	SET_FLAG(e->flags, ENTFLAG_LIGHT);
	e->next = NULL;
	return e;
}
Example #10
0
static cpSpace *
init(void)
{
	cpSpace *space = cpSpaceNew();
	space->iterations = 10;
	space->gravity = cpv(0, -GRAVITY);
//	space->sleepTimeThreshold = 1000;
	space->enableContactGraph = cpTrue;

	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;
	
	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;
	
	// Set up the player
	cpFloat radius = 25.0f;
	body = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY));
	body->p = cpv(0, -200);
	body->velocity_func = playerUpdateVelocity;
	playerBody = body;

	shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
	shape->e = 0.0f; shape->u = 0.0f;
	shape->collision_type = 1;
	playerShape = shape;
	
	// Add some boxes to jump on
	for(int i=0; i<6; i++){
		for(int j=0; j<3; j++){
			body = cpSpaceAddBody(space, cpBodyNew(4.0f, INFINITY));
			body->p = cpv(100 + j*60, -200 + i*60);
			
			shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 50, 50));
			shape->e = 0.0f; shape->u = 0.7f;
		}
	}
	
	return space;
}
Example #11
0
static cpSpace *
init(void)
{
	cpSpace *space = cpSpaceNew();
	space->iterations = 10;
	space->gravity = cpv(0, -GRAVITY);
//	space->sleepTimeThreshold = 1000;

	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));
	shape->e = 1.0f; shape->u = 1.0f;
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);

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

	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
	
	shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f));
	shape->e = 1.0f; shape->u = 1.0f;
	cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
	
	// Set up the player
	body = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY));
	body->p = cpv(0, -200);
	body->velocity_func = playerUpdateVelocity;
	playerBody = body;

	shape = cpSpaceAddShape(space, cpBoxShapeNew2(body, cpBBNew(-15.0, -27.5, 15.0, 27.5), 10.0));
//	shape = cpSpaceAddShape(space, cpSegmentShapeNew(playerBody, cpvzero, cpv(0, radius), radius));
	shape->e = 0.0f; shape->u = 0.0f;
	shape->type = 1;
	playerShape = shape;
	
	// Add some boxes to jump on
	for(int i=0; i<6; i++){
		for(int j=0; j<3; j++){
			body = cpSpaceAddBody(space, cpBodyNew(4.0f, INFINITY));
			body->p = cpv(100 + j*60, -200 + i*60);
			
			shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 50, 50, 0.0));
			shape->e = 0.0f; shape->u = 0.7f;
		}
	}
	
	return space;
}
Example #12
0
Entity_T *SetupMonster(int monster)
{
	int i, x;
	const GLfloat SPRITE_WIDTH = 32.0f / 128;
	const GLfloat SPRITE_HEIGHT = 48.0f / 192;
	Rect frames[20];
	Animation_T animations[MAX_ANIMATIONS];
	Entity_T *e = NewEntity();
	e->currentAnimation = 0;
	e->currentFrame = 0;
	e->sprite = NewSprite();
	e->body = cpBodyNew(1.0f, 1.0f);
	e->size.x = 3* TILE_WIDTH;
	e->size.y = 3 * 1.5f * TILE_HEIGHT;
	e->shape = cpBoxShapeNew(e->body, e->size.x/2, e->size.y/2);
	
	for(i = 0;i < 20;i++)
	{
		frames[i].x = (i % 4) * SPRITE_WIDTH;
		frames[i].y = 1.0f - (i/4+1) * SPRITE_HEIGHT;
		frames[i].w = SPRITE_WIDTH;
		frames[i].h = SPRITE_HEIGHT;
	}
	
	for(x = 0;x < 4;x++)
	{
		animations[x].numFrames = 1;
		animations[x].frames[0] = x*4;
		animations[x].frameLengths[0] = 100;
		animations[x].flags = ANIMFLAG_NONE;
	}

	for(x = 4;x < 8;x++){
		animations[x].numFrames = 4;
		for(i = 0;i < 4;i++){
			animations[x].frames[i] = (x-4)*4+i;
			animations[x].frameLengths[i] = 100;
		}
		animations[x].flags = ANIMFLAG_NONE;
	}
	//TODO: Kludgy as heck, redo later.
	switch(monster){
	case 0:
		SetupSprite(e->sprite, "monster0.png", frames, 20, animations, 8);
		break;
	case 1:
		SetupSprite(e->sprite, "monster1.png", frames, 20, animations, 8);
		break;
	}
	return e;
}
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;
}
cpShape*
PhysicsWorld::AddRectShape(cpFloat width, cpFloat height, cpFloat friction, cpBody *body)
{
    if(body != NULL)
    {
        // Create it's collision shape
        cpShape *boxShape = cpSpaceAddShape(mySpace, cpBoxShapeNew(body, width, height));
        cpShapeSetFriction(boxShape, friction);
        
        return boxShape;
    }
    
    return NULL;
}
Example #15
0
Player::Player(float x, float y, cpSpace* space) :
	DynamicObject(x, y, 1, 1.5, 5, space)
{
	//cpFloat moment = cpMomentForBox(m_mass, 1.5, 1.5);
	cpFloat moment = INFINITY;  // Infinite moment of inertia - don't let player rotate
	
	m_pBody = cpSpaceAddBody(space, cpBodyNew(m_mass, moment));
	m_pShape = cpSpaceAddShape(space, cpBoxShapeNew(m_pBody, 1, 1.5));
	m_pShape->collision_type = PLAYER_TYPE;
	setPosition(x, y);
	
	cpShapeSetFriction(m_pShape, 0.9);
	cpShapeSetElasticity(m_pShape, 0.0);
}
Example #16
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 #17
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 #18
0
Entity_T *SetupObject(int object)
{
	int i, x;
	const GLfloat SPRITE_WIDTH = 32.0f / 128;
	const GLfloat SPRITE_HEIGHT = 48.0f / 192;
	Rect frames[20];
	Animation_T animations[MAX_ANIMATIONS];
	Entity_T *e = NewEntity();
	e->currentAnimation = 0;
	e->currentFrame = 0;
	e->sprite = NewSprite();
	e->body = cpBodyNew(1.0f, 1.0f);
	e->size.x = TILE_WIDTH;
	e->size.y = 1.5f * TILE_HEIGHT;
	e->shape = cpBoxShapeNew(e->body, e->size.x/2, e->size.y/2);
	
	frames[0].x = 0;
	frames[0].y = 0;
	frames[0].w = 1;
	frames[0].h = 1;
	
	animations[0].numFrames = 1;
	animations[0].frames[0] = 0;
	animations[0].frameLengths[0] = 100;
	animations[0].flags = ANIMFLAG_NONE;

	//TODO: Kludgy as heck, redo later.
	switch(object){
	case 0:
		SetupSprite(e->sprite, "torch.png", frames, 1, animations, 1);
		e->light = NewLight();
		e->light->color.x = 1.0f;
		e->light->color.y = 0.0f;
		e->light->color.z = 1.0f;
		e->light->brightness = 0.75f;
		e->light->span = TILE_WIDTH * .75f;
		e->light->offset.x = TILE_WIDTH / 2;
		e->light->offset.y = 0;
		SET_FLAG(e->flags, ENTFLAG_LIGHT);
		e->currentAnimation = 0;
		e->currentFrame = 0;
		e->dir = ENTDIR_DOWN;
		break;
	}
	return e;
}
Example #19
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 #20
0
Player::Player(){
	std::string bulletPattern1("<bulletml><action label=\"top\"><fire><direction type=\"absolute\">90</direction><bullet><speed>3</speed></bullet></fire></action></bulletml>");
	std::string bulletPattern2("<bulletml><action label=\"top\"><fire><direction type=\"relative\">90</direction><bullet><speed>6</speed></bullet></fire><fire><direction type=\"relative\">90</direction><bulletRef label=\"curveLeft\" /></fire><fire><direction type=\"relative\">90</direction><bulletRef label=\"curveRight\" /></fire></action><bullet label=\"curveLeft\"><speed>6</speed><action><changeDirection><direction type=\"relative\">45</direction><term>5</term></changeDirection></action></bullet><bullet label=\"curveRight\"><speed>6</speed><action><changeDirection><direction type=\"relative\">-45</direction><term>5</term></changeDirection></action></bullet></bulletml>");
	std::string bulletPattern3("<bulletml><action label=\"top\"><fire><direction type=\"relative\">135</direction><bulletRef label=\"curveRight\" /></fire><fire><direction type=\"absolute\">90</direction><bullet><speed>4</speed></bullet></fire><fire><direction type=\"relative\">45</direction><bulletRef label=\"curveLeft\" /></fire></action><bullet label=\"curveLeft\"><speed>4</speed><action><wait>8</wait><changeDirection><direction type=\"relative\">90</direction><term>15</term></changeDirection></action></bullet><bullet label=\"curveRight\"><speed>4</speed><action><wait>8</wait><changeDirection><direction type=\"relative\">-90</direction><term>15</term></changeDirection></action></bullet></bulletml>");


	powerUp=0;
	isShooting=false;

	loadTextures();

#ifdef WIN32
	//	tiro=new sound("../res/tiro.wav",25,100,50);
#else
	tiro=JNIUtil::jni_sound_load("tiro.ogg");
#endif
	shot=&sprBullet[3];


#ifdef WIN32
	bps[0] = new BulletMLParserTinyXML("../res/xml/player_1.xml");
	bps[1] = new BulletMLParserTinyXML("../res/xml/player_2.xml");
	bps[2] = new BulletMLParserTinyXML("../res/xml/player_3.xml");
#else
	bps[0] = new BulletMLParserTinyXML(bulletPattern1);
	bps[1] = new BulletMLParserTinyXML(bulletPattern2);
	bps[2] = new BulletMLParserTinyXML(bulletPattern3);
#endif
	bps[0]->build();
	bps[1]->build();
	bps[2]->build();

	playerBody = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY));
	playerBody->p = cpv(10, 100);
	playerBody->velocity_func = playerUpdateVelocity;

	playerShape = cpSpaceAddShape(space, cpBoxShapeNew(playerBody, 5, 7, 10));
	playerShape->e = 0.0f; 
	playerShape->u = 0.0f;
	playerShape->type = 1;
}
Example #21
0
File: physics.c Project: dns/CLove
int l_physics_newBoxShape(lua_State* state)
{
    l_tools_checkUserDataPlusErrMsg(state, 1, "You must provide a space!");
    l_physics_PhysicsData* physics = (l_physics_PhysicsData*)lua_touserdata(state, 1);
    l_tools_checkUserDataPlusErrMsg(state, 2, "You must provide a body");
    l_physics_Body* body = (l_physics_Body*)lua_touserdata(state, 2);

    float width = l_tools_toNumberOrError(state, 3);
    float height = luaL_optnumber(state, 4, width);
    float radius = luaL_optnumber(state, 5, 0.0f);
    float offset_x = luaL_optnumber(state, 6, 0.0f);
    float offset_y = luaL_optnumber(state, 7, 0.0f);

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

    if (offset_x == 0 && offset_y == 0)
        // No offset is needed
        moduleData.shape->shape = cpSpaceAddShape(physics->physics->space, cpBoxShapeNew(body->body, width, height, radius));
    else
    {
        //Apply offset to shape!

        cpVect off = cpv(offset_x, offset_y);
        cpBB bb = cpBBNewForExtents(off, width * 0.5f, height * 0.5f);

        moduleData.shape->shape = cpSpaceAddShape(physics->physics->space, cpBoxShapeNew2(body->body, bb, radius));

    }

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

    return 1;
}
Example #22
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 #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)
{
	cpSpace *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);
	cpShapeSetLayers(shape, NOT_GRABABLE_MASK);

	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);

	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);
	
	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 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)));
			cpBodySetPos(body, pos);
			
			shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height));
			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);
			
			prev = body;
		}
	}
	
	cpFloat radius = 15.0f;
	body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
	cpBodySetPos(body, cpv(0, -240 + radius+5));
	cpBodySetVel(body, cpv(0, 300));

	shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
	cpShapeSetElasticity(shape, 0.0f);
	cpShapeSetFriction(shape, 0.9f);
	
	return space;
}
Example #25
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;
}
Example #26
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 #27
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;
}