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; }
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; }
static cpSpace * init(void) { QUERY_START = cpvzero; space = cpSpaceNew(); cpSpaceSetIterations(space, 5); { // add a fat segment cpFloat mass = 1.0f; cpFloat length = 100.0f; cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f); cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b))); cpBodySetPos(body, cpv(0.0f, 100.0f)); cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 20.0f)); } { // add a static segment cpSpaceAddShape(space, cpSegmentShapeNew(cpSpaceGetStaticBody(space), cpv(0, 300), cpv(300, 0), 0.0f)); } { // add a pentagon cpFloat mass = 1.0f; 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(30*cos(angle), 30*sin(angle)); } cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero))); cpBodySetPos(body, cpv(50.0f, 50.0f)); cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero)); } { // add a circle cpFloat mass = 1.0f; cpFloat r = 20.0f; cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, r, cpvzero))); cpBodySetPos(body, cpv(100.0f, 100.0f)); cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero)); } return space; }
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; }
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; }
void physics_add(Entity ent) { PhysicsInfo *info; if (entitypool_get(pool, ent)) return; /* already has physics */ transform_add(ent); info = entitypool_add(pool, ent); info->mass = 1.0; info->type = PB_DYNAMIC; /* create, init cpBody */ info->body = cpSpaceAddBody(space, cpBodyNew(info->mass, 1.0)); cpBodySetUserData(info->body, ent); /* for cpBody -> Entity mapping */ cpBodySetPos(info->body, cpv_of_vec2(transform_get_position(ent))); cpBodySetAngle(info->body, transform_get_rotation(ent)); info->last_dirty_count = transform_get_dirty_count(ent); /* initially no shapes */ info->shapes = array_new(ShapeInfo); /* initialize last_pos/last_ang info for kinematic bodies */ info->last_pos = cpBodyGetPos(info->body); info->last_ang = cpBodyGetAngle(info->body); info->collisions = NULL; }
static void _set_type(PhysicsInfo *info, PhysicsBody type) { if (info->type == type) return; /* already set */ info->type = type; switch (type) { case PB_KINEMATIC: info->last_pos = cpBodyGetPos(info->body); info->last_ang = cpBodyGetAngle(info->body); /* fall through */ case PB_STATIC: if (!cpBodyIsStatic(info->body)) { cpSpaceRemoveBody(space, info->body); cpSpaceConvertBodyToStatic(space, info->body); } break; case PB_DYNAMIC: cpSpaceConvertBodyToDynamic(space, info->body, info->mass, 1.0); cpSpaceAddBody(space, info->body); _recalculate_moment(info); break; } }
void world_addEntity(World_t *aWorld, WorldEntity_t *aEntity) { dynamo_assert(aEntity != aWorld->staticEntity, "You cannot re-add static entity to world"); cpSpaceAddBody(aWorld->cpSpace, aEntity->cpBody); llist_apply(aEntity->shapes, (LinkedListApplier_t)&_addShapeToSpace, aWorld); llist_pushValue(aWorld->entities, aEntity); }
static void add_box(cpSpace *space) { const cpFloat size = 10.0f; const cpFloat mass = 1.0f; cpVect verts[] = { cpv(-size,-size), cpv(-size, size), cpv( size, size), cpv( size,-size), }; cpFloat radius = cpvlength(cpv(size, size)); cpVect pos = rand_pos(radius); cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, 4, verts, cpvzero, 0.0f))); body->velocity_func = planetGravityVelocityFunc; cpBodySetPosition(body, pos); // Set the box's velocity to put it into a circular orbit from its // starting position. cpFloat r = cpvlength(pos); cpFloat v = cpfsqrt(gravityStrength / r) / r; cpBodySetVelocity(body, cpvmult(cpvperp(pos), v)); // Set the box's angular velocity to match its orbital period and // align its initial angle with its position. cpBodySetAngularVelocity(body, v); cpBodySetAngle(body, cpfatan2(pos.y, pos.x)); cpShape *shape = cpSpaceAddShape(space, cpPolyShapeNew(body, 4, verts, cpTransformIdentity, 0.0)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.7f); }
ETERM *space_add_body(ETERM *fromp, ETERM *argp) { // get the args ETERM *space_refp = erl_element(1, argp); ETERM *idp = erl_element(2, argp); ETERM *massp = erl_element(3, argp); // ETERM *inertiap = erl_element(4, argp); erlmunk_space *s; int space_id = ERL_REF_NUMBER(space_refp); HASH_FIND_INT(erlmunk_spaces, &space_id, s); int object_id = ERL_INT_VALUE(idp); cpBody *body = cpSpaceAddBody(s->space, cpBodyNew(ERL_FLOAT_VALUE(massp), INFINITY)); // the body is created inactive, it is explicitly activated // when all it's values have been set. cpBodySleep(body); erlmunk_body_data *data = malloc(sizeof(erlmunk_body_data)); data->id = object_id; data->term = NULL; cpBodySetUserData(body, (cpDataPointer) data); space_add_body_hash(s, object_id, body); return NULL; }
int lc_space_AddBody(lua_State *vm){ //space, body lc_space *space = lc_GetSpace(1, vm); if (space == NULL){ printf("chipmunk: Object can't call :AddBody\n"); return 0; } lc_body *body = lc_GetBody(2, vm); if (body == NULL){ printf("space:AddBody(): Can't add a object that isn't a body.\n"); return 0; } char pbody[32] = {0}; snprintf(pbody, 32, "%p", body); //Check if the body exists in bodies table. If not exists then add to the table lua_rawgeti(vm, LUA_REGISTRYINDEX, space->bodies); lua_getfield(vm, -1, pbody); if (!lua_isnil(vm, -1)){ //The body already exists. Then return. return 0; } lua_pop(vm, 1); lua_pushvalue(vm, 2); lua_setfield(vm, -2, pbody); lua_pop(vm, 1);//bodies table //--- cpSpaceAddBody(space->space, body->body); return 0; }
void PhysicsBody::setDynamic(bool dynamic) { if (dynamic != _dynamic) { _dynamic = dynamic; if (dynamic) { if (_world && _cpBody->CP_PRIVATE(space)) { cpSpaceConvertBodyToDynamic(_world->_cpSpace, _cpBody, _mass, _moment); cpSpaceAddBody(_world->_cpSpace, _cpBody); } else { cpBodySetMass(_cpBody, _mass); cpBodySetMoment(_cpBody, _moment); } } else { if (_world && _cpBody->CP_PRIVATE(space)) { cpSpaceRemoveBody(_world->_cpSpace, _cpBody); cpSpaceConvertBodyToStatic(_world->_cpSpace, _cpBody); } else { cpBodySetMass(_cpBody, PHYSICS_INFINITY); cpBodySetMoment(_cpBody, PHYSICS_INFINITY); cpBodySetVel(_cpBody, cpvzero); cpBodySetAngVel(_cpBody, 0.0); } } } }
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); }
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; }
int mkparticle(particle_kind_t kind, cpVect pos, cpVect impulse, double energy) { particle_t* p = malloc(sizeof *p); if(p == NULL) { return -1; // bad malloc } cpSpace* space = current_space(); cpBody* body = cpBodyNew(energy / 1000.0, particle_moi); cpShape* shape = cpCircleShapeNew(body, particle_r, cpvzero); cpBodySetUserData(body, p); cpShapeSetUserData(shape, p); cpSpaceAddBody(space, body); cpSpaceAddShape(space, shape); *p = (particle_t){ .id = id, .kind = kind, .energy = energy, .life = energy, .body = body }; HASH_ADD_INT(particles, id, p); return id++; }
void drawing_activate(cpSpace *space, cpBody *drawing) { drawing = utils_update_drawing(drawing); cpSpaceAddBody(space, drawing); Point_array * parray = cpBodyGetUserData(drawing); point_array_free_arrays(parray); }
void PhysicsWorld::addChild(PhysicsBody* body) { auto shapes = body->getShapes(); // add body to space if (body->isDynamic()) { cpSpaceAddBody(_info->space, body->_info->body); } // add shapes to space for (auto it = shapes.begin(); it != shapes.end(); it++) { addShape(*it); } if (_bodys == nullptr) { _bodys = Array::create(body, NULL); _bodys->retain(); }else { _bodys->addObject(body); } }
static void do_logic(game_state *state) { switch_state_data *data = state->data->switch_data; if (data->mouse_down_last_step && state->game->mouse_down == false) { /* clicked and released; create a circle */ cpFloat radius = 15; cpFloat mass = 10; cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero); cpBody *ball = cpSpaceAddBody(data->space, cpBodyNew(mass, moment)); cpBodySetPos(ball, state->game->mouse_pos); cpShape *ball_shape = cpSpaceAddShape(data->space, cpCircleShapeNew(ball, radius, cpvzero)); cpShapeSetElasticity(ball_shape, 0.7); cpShapeSetFriction(ball_shape, 0.7); cpShapeSetLayers(ball_shape, L_PLAYER); } cpSpaceStep(data->space, TARGET_SEC_PER_FRAME); data->mouse_down_last_step = state->game->mouse_down; if (ent_switch_get_state(data->sw)) debug_puts("switch is on"); else debug_puts("switch is off"); }
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 TestColliderDetector::initWorld() { //! create physic space space = cpSpaceNew(); //! set space gravity as no gravity space->gravity = cpv(0, 0); //! Physics debug layer CCPhysicsDebugNode *debugLayer = CCPhysicsDebugNode::create(space); this->addChild(debugLayer, INT_MAX); //! get size of bullet CCSize size = bullet->getContentSize(); //! define bullet's collider body int num = 4; cpVect verts[] = { cpv(-size.width/2,-size.height/2), cpv(-size.width/2,size.height/2), cpv(size.width/2,size.height/2), cpv(size.width/2,-size.height/2), }; //! build body as verts' shape cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)); cpSpaceAddBody(space, body); cpShape* shape = cpPolyShapeNew(body, num, verts, cpvzero); shape->collision_type = eBulletTag; cpSpaceAddShape(space, shape); bullet->setCPBody(body); //! define armature2's body,get shape from armature data body = cpBodyNew(INFINITY, INFINITY); cpSpaceAddBody(space, body); armature2->setCPBody(body); shape = body->shapeList_private; while(shape){ cpShape *next = shape->next_private; shape->collision_type = eEnemyTag; shape = next; } cpSpaceAddCollisionHandler(space, eEnemyTag, eBulletTag, beginHit, NULL, NULL, endHit, NULL); }
cpBody* createNewBodyFrom(cpSpace* space, cpFloat xPos, cpFloat yPos, cpFloat mass, cpFloat moment, JSObject* outterJsObj) { cpBody* circularBody = cpSpaceAddBody(space, cpBodyNew(mass, moment)); cpBodySetPos(circularBody, cpv(xPos, yPos)); cpBodySetUserData(circularBody, outterJsObj); return circularBody; }
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; }
void Player::Initialize(void) { // Load the texture if (!(this->m_Texture = this->m_Hge->Texture_Load("data/particles.png"))) { MessageBox(NULL, "Can't load particles.png texture.", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL); this->m_Engine->Shutdown(); return; } // Create the player sprite this->m_Sprite = new hgeSprite(this->m_Texture, 60, 40, 20, 20); this->m_Sprite->SetColor(this->m_Color); this->m_Sprite->SetHotSpot(10, 10); // Create the particle sprite this->m_ParticleSprite = new hgeSprite(this->m_Texture, 20, 20, 20, 20); this->m_ParticleSprite->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE); this->m_ParticleSprite->SetHotSpot(10, 10); // Create the particle system this->m_ParticleSystem = new hgeParticleSystem("data/trail.psi", this->m_ParticleSprite); this->m_ParticleSystem->Fire(); // Initialize the weapon slots this->m_Weaponslots = (Weapon**) malloc(sizeof(Weapon*) * NUM_WEAPON_SLOTS); for (int i = 0 ; i < NUM_WEAPON_SLOTS ; ++i) { this->m_Weaponslots[i] = 0; } this->m_Weaponslots[0] = new Neoshooter(this, this->m_Engine->GetWorld()); // Initialize physics { // Define moment cpFloat moment = cpMomentForCircle(1, 0, 10, cpvzero); // Fetch the physics space cpSpace *space = this->m_Engine->GetWorld()->GetSpace(); // Add the physics body this->m_Body = cpSpaceAddBody(space, cpBodyNew(1, moment)); cpBodySetPos(this->m_Body, this->m_Position); // Add the physics shape this->m_Shape = cpSpaceAddShape(space, cpCircleShapeNew(this->m_Body, 10, cpvzero)); this->m_Shape->data = this; this->m_Shape->collision_type = COLLISION_TYPE_GO; cpShapeSetElasticity(this->m_Shape, 1.0f); cpShapeSetFriction(this->m_Shape, 0.7); // Define the collision handlers for various types of collisions cpSpaceAddCollisionHandler(space, COLLISION_TYPE_GO, COLLISION_TYPE_ACTIVATOR, BeginCollisionD, NULL, NULL, SeparateCollisionD, this); cpSpaceAddCollisionHandler(space, COLLISION_TYPE_GO, COLLISION_TYPE_BULLET, BeginCollisionD, NULL, NULL, NULL, this); } }
cpSpace* cpSpaceSerializer::load(cpSpace *space, const char* filename) { if (!_doc.LoadFile(filename)) return space; //Grab our space TiXmlElement *root = _doc.FirstChildElement("space"); if (!root) return space; _space = space; //Initialize _bodyMap.clear(); _shapeMap.clear(); //A body id of zero is the space's static body _bodyMap[0] = space->staticBody; space->iterations = createValue<int>("iterations", root); space->gravity = createPoint("gravity", root); space->damping = createValue<cpFloat>("damping", root); TiXmlElement *child = root->FirstChildElement("shape"); //Read Shapes while (child) { //attempt a shape cpShape *shape = createShape(child); if (shape) { //This should not happen like this, need to reflect reality -rkb if (shape->body->m != INFINITY && !cpSpaceContainsBody(space, shape->body)) cpSpaceAddBody(space, shape->body); cpSpaceAddShape(space, shape); } //Next! child = child->NextSiblingElement("shape"); } //Read Constraints child = root->FirstChildElement("constraint"); while (child) { //else attempt a constraint cpConstraint *constraint = createConstraint(child); if (constraint) cpSpaceAddConstraint(space, constraint); child = child->NextSiblingElement("constraint"); } return space; }
Ship* addship(int vn, cpVect* vl, cpVect pos, Genome* g = 0) { Ship* s = new Ship(vn, vl, pos, g); ship_list.push_back(s); cpSpaceAddBody(space, s->body); cpSpaceAddShape(space, s->cpshape); return *ship_list.rbegin(); }
Shell* addshell(cpVect pos, cpVect vel, float angle) { Shell* s = new Shell(pos, vel, angle); shell_list.push_back(s); cpSpaceAddBody(space, s->body); cpSpaceAddShape(space, s->cpshape); return *shell_list.rbegin(); }
static void make_leg(cpSpace *space, cpFloat side, cpFloat offset, cpBody *chassis, cpBody *crank, cpVect anchor) { cpVect a, b; cpShape *shape; cpFloat leg_mass = 1.0f; // make leg a = cpvzero, b = cpv(0.0f, side); cpBody *upper_leg = cpSpaceAddBody(space, cpBodyNew(leg_mass, cpMomentForSegment(leg_mass, a, b, 0.0f))); cpBodySetPosition(upper_leg, cpv(offset, 0.0f)); shape = cpSpaceAddShape(space, cpSegmentShapeNew(upper_leg, a, b, seg_radius)); cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); cpSpaceAddConstraint(space, cpPivotJointNew2(chassis, upper_leg, cpv(offset, 0.0f), cpvzero)); // lower leg a = cpvzero, b = cpv(0.0f, -1.0f*side); cpBody *lower_leg = cpSpaceAddBody(space, cpBodyNew(leg_mass, cpMomentForSegment(leg_mass, a, b, 0.0f))); cpBodySetPosition(lower_leg, cpv(offset, -side)); shape = cpSpaceAddShape(space, cpSegmentShapeNew(lower_leg, a, b, seg_radius)); cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); shape = cpSpaceAddShape(space, cpCircleShapeNew(lower_leg, seg_radius*2.0f, b)); cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 1.0f); cpSpaceAddConstraint(space, cpPinJointNew(chassis, lower_leg, cpv(offset, 0.0f), cpvzero)); cpSpaceAddConstraint(space, cpGearJointNew(upper_leg, lower_leg, 0.0f, 1.0f)); cpConstraint *constraint; cpFloat diag = cpfsqrt(side*side + offset*offset); constraint = cpSpaceAddConstraint(space, cpPinJointNew(crank, upper_leg, anchor, cpv(0.0f, side))); cpPinJointSetDist(constraint, diag); constraint = cpSpaceAddConstraint(space, cpPinJointNew(crank, lower_leg, anchor, cpvzero)); cpPinJointSetDist(constraint, diag); }
int l_physics_newCircleBody(lua_State* state) { l_tools_checkUserDataPlusErrMsg(state, 1, "You must provide a space"); l_physics_PhysicsData* physics = (l_physics_PhysicsData*)lua_touserdata(state, 1); const char* type = luaL_optstring(state, 2, "dynamic"); float outer_radius = luaL_optnumber(state, 3, 1.0f); cpVect offset = cpvzero; offset.x = luaL_optnumber(state, 4, 0.0f); offset.y = luaL_optnumber(state, 5, 0.0f); float mass = luaL_optnumber(state, 6, 1.0f); float moment = luaL_optnumber(state, 7, 0.0f); float inner_radius = luaL_optnumber(state, 8, 0.0f); moduleData.body = (l_physics_Body*)lua_newuserdata(state, sizeof(l_physics_Body)); moduleData.body->physics = malloc(sizeof(physics_PhysicsData)); moduleData.body->physics = physics->physics; cpFloat _moment = moment; if (_moment == 0) _moment = cpMomentForCircle(mass, inner_radius, outer_radius, offset); if (strcmp(type, "dynamic") == 0) moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNewDynamic()); else if (strcmp(type, "kinematic") == 0) moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNewKinematic()); else if (strcmp(type, "static") == 0) moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNewStatic()); else { moduleData.body->body = cpSpaceAddBody(physics->physics->space, cpBodyNew(mass, _moment)); /* const char* err = util_concatenate("Undefined type: ", type); l_tools_trowError(state, err); return -1; */ } lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.bodyMT); lua_setmetatable(state, -2); return 1; }
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; }
static cpSpace * init(void) { ChipmunkDemoMessageString = "One way platforms are trivial in Chipmunk using a very simple collision callback."; cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 10); cpSpaceSetGravity(space, cpv(0, -100)); 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 our one way segment shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-100), cpv(160,-100), 10.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLISION_TYPE_ONE_WAY); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); // We'll use the data pointer for the OneWayPlatform struct platformInstance.n = cpv(0, 1); // let objects pass upwards cpShapeSetUserData(shape, &platformInstance); // Add a ball to test it out cpFloat radius = 15.0f; body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero))); cpBodySetPosition(body, cpv(0, -200)); cpBodySetVelocity(body, cpv(0, 170)); shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.9f); cpShapeSetCollisionType(shape, 2); cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, COLLISION_TYPE_ONE_WAY); handler->preSolveFunc = PreSolve; return space; }