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; }
static cpSpace * init(void) { ChipmunkDemoMessageString = "Sticky collisions using the cpArbiter data pointer."; cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 10); cpSpaceSetGravity(space, cpv(0, -1000)); cpSpaceSetCollisionSlop(space, 2.0); cpBody *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; // Create segments around the edge of the screen. shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340,-260), cpv(-340, 260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv( 340,-260), cpv( 340, 260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340,-260), cpv( 340,-260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-340, 260), cpv( 340, 260), 20.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); for(int i=0; i<200; i++){ cpFloat mass = 0.15f; cpFloat radius = 10.0f; cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); cpBodySetPos(body, cpv(cpflerp(-150.0f, 150.0f, frand()), cpflerp(-150.0f, 150.0f, frand()))); cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius + STICK_SENSOR_THICKNESS, cpvzero)); cpShapeSetFriction(shape, 0.9f); cpShapeSetCollisionType(shape, COLLIDE_STICK_SENSOR); } cpSpaceAddCollisionHandler(space, COLLIDE_STICK_SENSOR, COLLIDE_STICK_SENSOR, NULL, StickyPreSolve, NULL, StickySeparate, NULL); return space; }
ETERM *space_add_boundaries(ETERM *fromp, ETERM *argp) { // get the args ETERM *space_refp = erl_element(1, argp); ETERM *lower_leftp = erl_element(2, argp); ETERM *lower_rightp = erl_element(3, argp); ETERM *upper_leftp = erl_element(4, argp); ETERM *upper_rightp = erl_element(5, argp); ETERM *collision_categoryp = erl_element(6, argp); ETERM *datap = erl_element(7, argp); erlmunk_space *s; int space_id = ERL_REF_NUMBER(space_refp); HASH_FIND_INT(erlmunk_spaces, &space_id, s); cpVect lowerLeft = cpv(ERL_FLOAT_VALUE(erl_element(1, lower_leftp)), ERL_FLOAT_VALUE(erl_element(2, lower_leftp))); cpVect lowerRight = cpv(ERL_FLOAT_VALUE(erl_element(1, lower_rightp)), ERL_FLOAT_VALUE(erl_element(2, lower_rightp))); cpVect upperLeft = cpv(ERL_FLOAT_VALUE(erl_element(1, upper_leftp)), ERL_FLOAT_VALUE(erl_element(2, upper_leftp))); cpVect upperRight = cpv(ERL_FLOAT_VALUE(erl_element(1, upper_rightp)), ERL_FLOAT_VALUE(erl_element(2, upper_rightp))); // get the static body that comes with the space cpBody *static_body = cpSpaceGetStaticBody(s->space); erlmunk_body_data *data = malloc(sizeof(erlmunk_body_data)); data->id = BOUNDARY_BODY_ID; data->term = erl_copy_term(datap); cpBodySetUserData(static_body, (cpDataPointer) data); // bottom cpShape *bottomBoundaryShape = cpSegmentShapeNew(static_body, lowerLeft, lowerRight, 0.0f); cpShapeSetCollisionType(bottomBoundaryShape, ERL_INT_VALUE(collision_categoryp)); cpSpaceAddShape(s->space, bottomBoundaryShape); // top cpShape *topBoundaryShape = cpSegmentShapeNew(static_body, upperLeft, upperRight, 0.0f); cpShapeSetCollisionType(topBoundaryShape, ERL_INT_VALUE(collision_categoryp)); cpSpaceAddShape(s->space, topBoundaryShape); // left cpShape *leftBoundaryShape = cpSegmentShapeNew(static_body, lowerLeft, upperLeft, 0.0f); cpShapeSetCollisionType(leftBoundaryShape, ERL_INT_VALUE(collision_categoryp)); cpSpaceAddShape(s->space, leftBoundaryShape); // right cpShape *rightBoundaryShape = cpSegmentShapeNew(static_body, lowerRight, upperRight, 0.0f); cpShapeSetCollisionType(rightBoundaryShape, ERL_INT_VALUE(collision_categoryp)); cpSpaceAddShape(s->space, rightBoundaryShape); return NULL; }
ETERM *body_set_collision_circle(ETERM *fromp, ETERM *argp) { // get the args ETERM *space_refp = erl_element(1, argp); ETERM *idp = erl_element(2, argp); ETERM *radiusp = erl_element(3, argp); ETERM *collision_typep = erl_element(4, argp); erlmunk_space *s; int space_id = ERL_REF_NUMBER(space_refp); HASH_FIND_INT(erlmunk_spaces, &space_id, s); int body_id = ERL_INT_VALUE(idp); erlmunk_body *b; HASH_FIND_INT(s->bodies, &body_id, b); if (b == NULL) return NULL; cpShape *shape = cpSpaceAddShape(s->space, cpCircleShapeNew(b->body, ERL_FLOAT_VALUE(radiusp), cpvzero)); cpShapeSetCollisionType(shape, ERL_INT_VALUE(collision_typep)); // DEBUGF(("body_set_collision_circle has succeeded")); return NULL; }
Shell(cpVect pos, cpVect vel, float angle) { cpVect vl[4] = {cpv(0, 0), cpv(0.1, 0), cpv(0.07, 0.3), cpv(0.03, 0.3)}; int vn = sizeof(vl)/sizeof(cpVect); float mass = cpAreaForPoly(vn, vl, 0) * shell_density; float moi = cpMomentForPoly(mass, vn, vl, cpv(0, 0), 0); body = cpBodyNew(mass, moi); cpshape = cpPolyShapeNew(body, vn, vl, cpTransformIdentity, 0); cpShapeSetFriction(cpshape, 0.9); cpVect centroid = cpCentroidForPoly(vn, vl); shape.setPointCount(vn); for (int i = 0; i < vn; i++) { shape.setPoint(i, sf::Vector2f(vl[i].x, vl[i].y)); } cpBodySetCenterOfGravity(body, centroid); cpBodySetPosition(body, pos-centroid); cpBodySetVelocity(body, vel); cpBodySetAngle(body, angle); cpShapeSetCollisionType(cpshape, 2); cpShapeSetUserData(cpshape, this); }
// adds an asteroid shape to the cpSpace cpBody * core_add_new_asteroid ( cpSpace *space, const int type, const double p1x, const double p1y, const double p2x, const double p2y, Color *color, const double orientation, const double friction, const double elasticity, const double density, const int index, cpVect impulse, cpVect offset ) { cpShape * shape; if ( type == CIRCLE_TYPE ) { shape = core_add_new_shape ( space, type, p1x, p1y, p2x, p2y, color, orientation, friction, elasticity, density, index ); } else { shape = core_add_single_segment_shape ( space, p1x, p1y, p2x, p2y, color, friction, elasticity, density, index ); } if ( shape == NULL ) return NULL; DrawShapeInfo * dsi = (DrawShapeInfo *) shape->data; dsi->space_shape_type = 1; // set collision type cpShapeSetCollisionType ( shape, ASTEROID_COLLISION_TYPE ); // apply impulse cpBody *body = cpShapeGetBody ( shape ); impulse = cpv ( impulse.x * IMPULSE_MULTIPLIER, impulse.y * IMPULSE_MULTIPLIER ); cpBodyApplyImpulse ( body, impulse, offset ); return body; }
/* * Set shape's collision type, used for * knowing when shape A(of type eg: 1) interacts with shape B(of type eg: 2) */ static int l_physics_setShapeCollisionType(lua_State* state) { l_physics_Shape* shape = (l_physics_Shape*)lua_touserdata(state, 1); uint32_t type = l_tools_toIntegerOrError(state, 2); cpShapeSetCollisionType(shape->shape,(cpCollisionType)type); return 0; }
void fff::kitty::Configure(){ fff::SetOriginByLua(game.vm, sprite, "kitty"); fff::SetOriginByLua(game.vm, flames[0], "flames"); flames[1].SetOrigin( flames[0].GetOrigin() ); flames[0].SetTexture(*game.textures["flames"]); flames[1].SetTexture(*game.textures["flames"]); flames[1].FlipX(true); burst.SetBuffer(*game.soundbuffers["burstinflames"]); shape = cpCircleShapeNew(body, fff::GetRadiusByLua(game.vm, "kitty"), (cpVect){0.f, 0.f} ); cpShapeSetUserData(shape, this); cpShapeSetCollisionType(shape, fff::collisions::types::kitty); }
Ship(int vn, cpVect* vl, cpVect pos, Genome* ng = 0) { float mass = cpAreaForPoly(vn, vl, 0) * ship_density; float moi = cpMomentForPoly(mass, vn, vl, cpv(0, 0), 0); body = cpBodyNew(mass, moi); cpshape = cpPolyShapeNew(body, vn, vl, cpTransformIdentity, 0); cpShapeSetFriction(cpshape, 0.9); cpVect centroid = cpCentroidForPoly(vn, vl); shape.setPointCount(vn); for (int i = 0; i < vn; i++) { shape.setPoint(i, sf::Vector2f(vl[i].x, vl[i].y)); } cpBodySetCenterOfGravity(body, centroid); cpBodySetPosition(body, pos-centroid); cpBodySetVelocity(body, cpv(0, 0)); cpShapeSetCollisionType(cpshape, 1); cpShapeSetUserData(cpshape, this); last_fired = 0; nose_angle = PI/2; player = false; target = 0; score = 0; if (ng == 0) { Genome* braingenome = mutate(readgenome("shipmind.mind")); brain = braingenome->makenetwork(); delete braingenome; } else { brain = ng->makenetwork(); } score = 0; }
Boundary::Boundary(float x1, float x2, float y, BSurface surfaceType) : Surface(cpv(x1, y - 50.0f), cpv(x2, y), false) { cpShapeSetElasticity(shape, 0.5f); cpShapeSetFriction(shape, 1.0f); modelScale = glm::vec3(width, 0.98*height, 10000.0f); cpShapeSetCollisionType(shape, OBJ_SURFACE); if(surfaceType == BS_SAND) { gpuDataList.push_back(gpuStore.add("./data/obj/boundary_sand", 0, false)); shaderList.push_back(shaderStore.add("./data/shader/vBoundaryXZ.glsl", "./data/shader/fBoundary.glsl")); } }
Lander landerMake(cpVect position, cpFloat thrust, int orientation, int fuel, cpSpace* inSpace) { Lander l = malloc(sizeof(struct lander)); const cpFloat radius = 10.0f; const cpFloat mass = 1.0f; l->body = cpSpaceAddBody(inSpace, cpBodyNew(mass, cpMomentForCircle(mass, radius, 0, cpvzero))); cpBodySetPos(l->body, position); cpBodySetAngle(l->body, radiansFromOrientation(orientation)); l->shape = cpSpaceAddShape(inSpace, cpCircleShapeNew(l->body, radius, cpvzero)); cpShapeSetCollisionType(l->shape, LANDER_TAG); cpShapeSetElasticity(l->shape, 0.0f); cpShapeSetFriction(l->shape, 0.5f); l->fuel = fuel; l->thrust = thrust; l->orientation = orientation; return l; }
DynamicObject::DynamicObject(float x, float y, float scale, float mass, float elast, float fric, int type, std::string gpuPath, std::string vPath, std::string fPath) { gpuDataList.push_back(gpuStore.add(gpuPath, 3.1415f)); shaderList.push_back(shaderStore.add(vPath, fPath)); transformOverrides = false; height = scale; modelScale = glm::vec3(scale); width = height*gpuDataList[0]->whRatio; /*** Set physics data ***/ body = cpBodyNew(mass, 0); cpSpaceAddBody(space, body); cpBodySetPosition(body, cpv(x, y)); ObjGPUData* gpuData = gpuDataList[0]; int vertCount = gpuData->vList.size(); cpVect vertices[vertCount]; glm::vec3 pos(x, y, 0); for(int i = 0; i < vertCount; i++) { glm::vec4 currentVert = glm::translate(glm::mat4(1.0f), pos) * glm::scale(glm::mat4(1.0f), modelScale) * gpuData->rotation * gpuData->unitScale * glm::vec4(gpuData->vList[i],0); vertices[i] = cpv(currentVert.x, currentVert.y); } shape = cpSpaceAddShape(space, (cpShape*) cpPolyShapeInitRaw(cpPolyShapeAlloc(), body, vertCount, vertices, 1.0f)); cpBodySetMoment(body, abs(cpMomentForPoly(mass, vertCount, vertices, cpvzero, 1.0f))); cpShapeSetElasticity(shape, elast); cpShapeSetFriction(shape, fric); cpShapeSetUserData(shape, this); cpShapeSetCollisionType(shape, type); draw = true; }
Wall::Wall(float y1, float y2, float xmid, float thickness) : Surface(cpv(xmid-thickness/2,y1), cpv(xmid+thickness/2,y2), false, thickness){ gpuDataList.push_back(gpuStore.add("./data/obj/surface_top", 0, false)); gpuDataList.push_back(gpuStore.add("./data/obj/surface_bot", 0, false)); gpuDataList.push_back(gpuStore.add("./data/obj/surface_left", 0, false)); gpuDataList.push_back(gpuStore.add("./data/obj/surface_right", 0, false)); gpuDataList.push_back(gpuStore.add("./data/obj/surface_front", 0, false)); gpuDataList.push_back(gpuStore.add("./data/obj/surface_back", 0, false)); shaderList.push_back(shaderStore.add("./data/shader/vSurfaceXZ.glsl", "./data/shader/fObject.glsl")); shaderList.push_back(shaderStore.add("./data/shader/vSurfaceXZ.glsl", "./data/shader/fObject.glsl")); shaderList.push_back(shaderStore.add("./data/shader/vSurfaceYZ.glsl", "./data/shader/fObject.glsl")); shaderList.push_back(shaderStore.add("./data/shader/vSurfaceYZ.glsl", "./data/shader/fObject.glsl")); shaderList.push_back(shaderStore.add("./data/shader/vSurfaceXY.glsl", "./data/shader/fObject.glsl")); shaderList.push_back(shaderStore.add("./data/shader/vSurfaceXY.glsl", "./data/shader/fObject.glsl")); cpShapeSetElasticity(shape, 0.5f); cpShapeSetFriction(shape, 1.0f); modelScale = glm::vec3(width, height, 500.0f); cpShapeSetCollisionType(shape, OBJ_SURFACE); }
Mirror::Mirror(ResourceManager& a_ResMgr, cpSpace& a_Space) : GameObject(a_ResMgr, a_Space) { //create le sprite m_resMgr.CreateSprite("media/mirror2.png", &m_Sprite); m_resMgr.AddDrawableSprite(&m_Sprite); sf::Vector2u sprSize = m_Sprite.sprite->getTexture()->getSize(); m_Sprite.sprite->setOrigin(sprSize.x/2,sprSize.y/2); //moment of inertia (rotation thing) cpFloat length = m_Sprite.sprite->getTexture()->getSize().x; cpFloat mass = 1; cpFloat moment = cpMomentForSegment(mass, cpv(0,0), cpv(0,length)); //---------------top //create physbody m_pReflectBody = cpSpaceAddBody(&a_Space, cpBodyNew(mass, moment)); m_pReflectBody->data = this; //create collision line m_pReflectLine = cpSpaceAddShape( &a_Space, cpSegmentShapeNew(m_pReflectBody, cpv(0,0), cpv(sprSize.x,0), 1) ); cpShapeSetFriction(m_pReflectLine, 0); m_pReflectLine->data = this; m_pReflectLine->sensor = 1; //only have the reflect line call the callbacks cpShapeSetCollisionType(m_pReflectLine, MIRROR); //---------------bottom //create physbody m_pBody = cpSpaceAddBody(&a_Space, cpBodyNew(mass, moment)); m_pBody->data = this; //create collision line m_pShape = cpSpaceAddShape( &a_Space, cpSegmentShapeNew(m_pBody, cpv(0,sprSize.y), cpv(sprSize.x,sprSize.y), 1) ); cpShapeSetFriction(m_pShape, 0); m_pShape->data = this; }
void GameInst::LoadLevel() { sf::Vector2f windowDim = m_GUIMgr.GetWindowDim(); //-------------------------------------- chipmunk physics // cpVect is a 2D vector and cpv() is a shortcut for initializing them. cpVect gravity = cpv(0, 980); // Create a physworld m_pSpace = cpSpaceNew(); cpSpaceSetGravity(m_pSpace, gravity); cpFloat offSet = -16; //top m_WorldBounds.Top = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,offSet), cpv(cpFloat(windowDim.x)+offSet,offSet), 1); cpShapeSetFriction(m_WorldBounds.Top, 0.5); cpSpaceAddShape(m_pSpace, m_WorldBounds.Top); cpShapeSetCollisionType(m_WorldBounds.Top, SURFACE_BOTTOM); //bottom m_WorldBounds.Bottom = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,cpFloat(windowDim.y)+offSet), cpv(cpFloat(windowDim.x)+offSet,cpFloat(windowDim.y)+offSet), 1); cpShapeSetFriction(m_WorldBounds.Bottom, 0.5); cpSpaceAddShape(m_pSpace, m_WorldBounds.Bottom); cpShapeSetCollisionType(m_WorldBounds.Bottom, SURFACE_TOP); //left m_WorldBounds.Left = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,offSet), cpv(offSet,cpFloat(windowDim.y)+offSet), 1); cpShapeSetFriction(m_WorldBounds.Left, 0.5); cpSpaceAddShape(m_pSpace, m_WorldBounds.Left); cpShapeSetCollisionType(m_WorldBounds.Left, SURFACE_RIGHT); //right m_WorldBounds.Right = cpSegmentShapeNew(m_pSpace->staticBody, cpv(cpFloat(windowDim.x)+offSet,offSet), cpv(cpFloat(windowDim.x)+offSet,cpFloat(windowDim.y)+offSet), 1); cpShapeSetFriction(m_WorldBounds.Right, 0.5); cpSpaceAddShape(m_pSpace, m_WorldBounds.Right); cpShapeSetCollisionType(m_WorldBounds.Right, SURFACE_LEFT); //player-surface collision callbacks cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_TOP,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL); cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_BOTTOM,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL); cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_LEFT,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL); cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_RIGHT,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL); //create player m_pPlayer = new Player(m_ResMgr, *m_pSpace); m_pPlayer->SetPosition(900, 600); m_Renderer.AddDrawableSprite(m_pPlayer->GetSprite()); //load level data from file std::fstream file; file.open(("media/level1.txt")); std::string line; int curLine = 0; while( std::getline(file,line) ) { // for(unsigned int i = 0; i < line.size(); i++) { char c = line[i]; switch(c) { case('#'): { Block *block = new Block(m_ResMgr, *m_pSpace, Block::BLOCK_SOLID, sf::Vector2f(float(i)*32,float(curLine)*32) ); m_blocks.push_back(block); //block->SetPosition(); m_Renderer.AddDrawableSprite(block->GetSprite()); break; } case('>'): { Emitters.push_back(new Emitter(m_ResMgr, *m_pSpace, sf::Vector2f(float(i * 32), float(curLine * 32)) )); Emitters.back()->Show(); break; } case('<'): { /*Emitters.push_back(new Emitter(m_ResMgr, *m_pSpace)); Emitters.back()->SetPosition(i * 32, curLine * 32); m_Renderer.AddDrawableSprite(Emitters.back()->GetSprite());*/ break; } } // } curLine++; } file.close(); file.open(("media/level1_binds.txt")); while( std::getline(file,line) ) { char buffer[32]; //char* tokenBuffer; sprintf_s(buffer, 32, line.c_str()); // Get the block IDs to link them //int block1 = atoi(strtok_s(buffer,",", &tokenBuffer)) + atoi(strtok_s(NULL ,"=", &tokenBuffer)) * 32; //int block2 = atoi(strtok_s(NULL,",", &tokenBuffer)) + atoi(strtok_s(NULL,",", &tokenBuffer)) * 32; // Link them //m_blocks[block1]->SetOutput(block2); } file.close(); //rebuild static level geometry //cpSpaceRehashStatic(); }
void CCPhysicsShape::setCollisionType(int collisionType) { cpShapeSetCollisionType(m_shape, (cpCollisionType)collisionType); }
void shape::collisionType( cpCollisionType ct ) { cpShapeSetCollisionType( this->m_shape, ct ); }
// make a homebase void core_make_homebase ( cpSpace *space, const double x, const double y, const int start_index, const int planet_num){ Box *defense[4]; double r = (double)rand()/(double)RAND_MAX; double g = (double)rand()/(double)RAND_MAX; double b = (double)rand()/(double)RAND_MAX; for( int i = 0; i < 4; i++){ defense[i] = box_new(); defense[i]->color->r = r; defense[i]->color->g = g; defense[i]->color->b = b; defense[i]->orientation = 0; defense[i]->friction = 0; defense[i]->elasticity = 1; defense[i]->density = 20; defense[i]->width = SIDE_WIDTH; defense[i]->height = SIDE_HEIGHT; } defense[0]->x = x-SIDE_HEIGHT/2; defense[0]->y = y-SIDE_OFFSET; defense[1]->x = x-SIDE_OFFSET; defense[1]->y = y+SIDE_HEIGHT/2; defense[2]->x = x+SIDE_HEIGHT/2; defense[2]->y = y+SIDE_OFFSET; defense[3]->x = x+SIDE_OFFSET; defense[3]->y = y-SIDE_HEIGHT/2; cpShape *side; BodyInfo *sidebi; for(int i = 0; i< 4; i++){ side = core_add_box_shape (space, defense[i], start_index+i); cpBodySetAngle(side->body, PI*i/2); sidebi = (BodyInfo *) (side->body->data); sidebi->space_shape_type = -1; // make it so sides don't timeout } for(int i = 0; i< 4; i++){ box_destroy (defense[i]); } Circle *planet; planet = circle_new(); planet->color->r = r; planet->color->g = g; planet->color->b = b; planet->orientation = 0; planet->friction = 0; planet->elasticity = 1; planet->density = 200; planet->radius = CORE_RADIUS; planet->x = x; planet->y = y; cpShape *core = core_add_circle_shape (space, planet, start_index+4); cpShapeSetCollisionType ( core, GOAL_COLLISION_TYPE ); //pjw midnite DrawShapeInfo *info = (DrawShapeInfo *)cpShapeGetUserData(core); info->originx = x; info->originy = y; BodyInfo *bi = (BodyInfo *)core->body->data; bi->originx = x; bi->originy = y; bi->space_shape_type = planet_num; info->space_shape_type = planet_num; circle_destroy (planet); }
void GameInst::LoadLevel() { sf::Vector2f windowDim = m_GUIMgr.GetWindowDim(); //-------------------------------------- chipmunk physics // cpVect is a 2D vector and cpv() is a shortcut for initializing them. cpVect gravity = cpv(0, 980); // Create a physworld m_pSpace = cpSpaceNew(); cpSpaceSetGravity(m_pSpace, gravity); cpFloat offSet = 0; //top m_WorldBounds.Top = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,offSet), cpv(cpFloat(windowDim.x)+offSet,offSet), 1); cpShapeSetFriction(m_WorldBounds.Top, 0.5); cpSpaceAddShape(m_pSpace, m_WorldBounds.Top); cpShapeSetCollisionType(m_WorldBounds.Top, SURFACE_BOTTOM); //bottom m_WorldBounds.Bottom = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,cpFloat(windowDim.y)+offSet), cpv(cpFloat(windowDim.x)+offSet,cpFloat(windowDim.y)+offSet), 1); cpShapeSetFriction(m_WorldBounds.Bottom, 0.5); cpSpaceAddShape(m_pSpace, m_WorldBounds.Bottom); cpShapeSetCollisionType(m_WorldBounds.Bottom, SURFACE_TOP); //left m_WorldBounds.Left = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,offSet), cpv(offSet,cpFloat(windowDim.y)+offSet), 1); cpShapeSetFriction(m_WorldBounds.Left, 0.5); cpSpaceAddShape(m_pSpace, m_WorldBounds.Left); cpShapeSetCollisionType(m_WorldBounds.Left, SURFACE_RIGHT); //right m_WorldBounds.Right = cpSegmentShapeNew(m_pSpace->staticBody, cpv(cpFloat(windowDim.x)+offSet,offSet), cpv(cpFloat(windowDim.x)+offSet,cpFloat(windowDim.y)+offSet), 1); cpShapeSetFriction(m_WorldBounds.Right, 0.5); cpSpaceAddShape(m_pSpace, m_WorldBounds.Right); cpShapeSetCollisionType(m_WorldBounds.Right, SURFACE_LEFT); //player-surface collision callbacks cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_TOP,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL); cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_BOTTOM,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL); cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_LEFT,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL); cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_RIGHT,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL); //load level data from file std::fstream file; file.open(("media/level1.txt")); std::string line; int curLine = 0; while( std::getline(file,line) ) { // for(unsigned int i = 0; i < line.size();i++) { bool playerMade = false; char c = line[i]; switch(c) { case('p'): { //create player if (!playerMade) { m_pPlayer = new Player(m_ResMgr, *m_pSpace); m_pPlayer->SetPosition(float(i)*32, float(curLine)*32); m_Renderer.AddDrawableSprite(m_pPlayer->GetSprite()); playerMade = true; } break; } case('#'): { Block *block = new Block(m_ResMgr, *m_pSpace, Block::BLOCK_SOLID, sf::Vector2f(float(i)*32,float(curLine)*32) ); m_blocks.push_back(block); m_Renderer.AddDrawableSprite(block->GetSprite()); break; } case('>'): { Emitters.push_back(new Emitter(m_ResMgr, *m_pSpace, sf::Vector2f(float(i * 32), float(curLine * 32)) )); Emitters.back()->Show(); break; } case('<'): { /*Emitters.push_back(new Emitter(m_ResMgr, *m_pSpace)); Emitters.back()->SetPosition(i * 32, curLine * 32); m_Renderer.AddDrawableSprite(Emitters.back()->GetSprite());*/ Block *block = new Block(m_ResMgr, *m_pSpace, Block::BLOCK_END, sf::Vector2f(float(i)*32,float(curLine)*32) ); m_blocks.push_back(block); m_Renderer.AddDrawableSprite(block->GetSprite()); catcherPositions.push_back(block->GetSprite()->sprite->getPosition()); break; } } // } curLine++; } file.close(); //enable user input to the player m_pPlayer->SetInputHandler(m_pInputHandler); //rebuild static level geometry //cpSpaceRehashStatic(); }
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; }
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; }
void fff::explosive::prepareShape(cpSpace *space){ shape = cpCircleShapeNew( cpSpaceGetStaticBody(space), 1.f, (cpVect){0.f, 0.f} ); cpShapeSetUserData(shape, this); cpShapeSetCollisionType(shape, fff::collisions::types::explosive); cpShapeSetSensor(shape, cpTrue); }