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"); }
float PhysicsShapeCircle::calculateMoment(float mass, float radius, const Vec2& offset) { return mass == PHYSICS_INFINITY ? PHYSICS_INFINITY : PhysicsHelper::cpfloat2float(cpMomentForCircle(mass, 0, radius, PhysicsHelper::point2cpv(offset))); }
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); } }
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; }
// Init is called by the demo code to set up the demo. static cpSpace * init(void) { // Create an infinite mass body to attach ground segments and other static geometry to. // We won't be adding this body to the space, because we don't want it to be simulated at all. // Adding bodies to the space simulates them. (fall under the influence of gravity, etc) // We want the static body to stay right where it is at all times. staticBody = cpBodyNew(INFINITY, INFINITY); // Create a space, a space is a simulation world. It simulates the motions of rigid bodies, // handles collisions between them, and simulates the joints between them. space = cpSpaceNew(); // Lets set some parameters of the space: // More iterations make the simulation more accurate but slower space->iterations = 10; // These parameters tune the efficiency of the collision detection. // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpSpace cpSpaceResizeStaticHash(space, 30.0f, 1000); cpSpaceResizeActiveHash(space, 30.0f, 1000); // Give it some gravity space->gravity = cpv(0, -100); // Create A ground segment along the bottom of the screen cpShape *ground = cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f); // Set some parameters of the shape. // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpShape ground->e = 1.0f; ground->u = 1.0f; ground->layers = NOT_GRABABLE_MASK; // Used by the Demo mouse grabbing code // Add the shape to the space as a static shape // If a shape never changes position, add it as static so Chipmunk knows it only needs to // calculate collision information for it once when it is added. // Do not change the postion of a static shape after adding it. cpSpaceAddStaticShape(space, ground); // Add a moving circle object. cpFloat radius = 15.0f; cpFloat mass = 10.0f; // This time we need to give a mass and moment of inertia when creating the circle. cpBody *ballBody = cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)); // Set some parameters of the body: // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpBody ballBody->p = cpv(0, -240 + radius+5); // Add the body to the space so it will be simulated and move around. cpSpaceAddBody(space, ballBody); // Add a circle shape for the ball. // Shapes are always defined relative to the center of gravity of the body they are attached to. // When the body moves or rotates, the shape will move with it. // Additionally, all of the cpSpaceAdd*() functions return the thing they added so you can create and add in one go. cpShape *ballShape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero)); ballShape->e = 0.0f; ballShape->u = 0.9f; return space; }
float PhysicsShapeCircle::calculateDefaultMoment() { cpShape* shape = _info->getShapes().front(); return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY : PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(_mass), 0, cpCircleShapeGetRadius(shape), cpCircleShapeGetOffset(shape))); }
float PhysicsShapeCircle::calculateDefaultMoment() { auto shape = _cpShapes.front(); return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY : PhysicsHelper::cpfloat2float(cpMomentForCircle(_mass, 0, cpCircleShapeGetRadius(shape), cpCircleShapeGetOffset(shape))); }
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; }
static cpBody * add_ball(cpVect pos) { cpBody *body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForCircle(1.0f, 30, 0, cpvzero))); body->p = pos; cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, 30, cpvzero)); shape->e = 0.0f; shape->u = 0.5f; return body; }
static struct cpShapeMassInfo cpCircleShapeMassInfo(cpFloat mass, cpFloat radius, cpVect center) { struct cpShapeMassInfo info = { mass, cpMomentForCircle(1.0f, 0.0f, radius, cpvzero), center, cpAreaForCircle(0.0f, radius), }; return info; }
static cpBody * add_ball(cpVect pos) { cpBody *body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForCircle(1.0f, 30, 0, cpvzero))); cpBodySetPos(body, pos); cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, 30, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); return body; }
int lc_MomentForCircle(lua_State *vm){ //m, radius1, radius2, offset {x, y} -> number cpFloat m = 0, radius1 = 0, radius2 = 0; cpVect offset = cpvzero; m = lua_tonumber(vm, 1); radius1 = lua_tonumber(vm, 2); radius2 = lua_tonumber(vm, 3); if (lua_gettop(vm) > 3 && lua_istable(vm, 3)){ offset = lc_TableTocpVect(3, vm);} lua_pushnumber(vm, cpMomentForCircle(m, radius1, radius2, offset)); return 1; }
static cpBody * addBall(cpVect pos, cpVect boxOffset) { cpFloat radius = 15.0f; cpFloat mass = 1.0f; cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); body->p = cpvadd(pos, boxOffset); cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); shape->e = 0.0f; shape->u = 0.7f; return body; }
static cpSpace * init(void) { cpResetShapeIdCounter(); space = cpSpaceNew(); space->iterations = 10; space->gravity = cpv(0, -100); 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 our one way segment shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-100), cpv(160,-100), 10.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->collision_type = 1; shape->layers = NOT_GRABABLE_MASK; // We'll use the data pointer for the OneWayPlatform struct platformInstance.n = cpv(0, 1); // let objects pass upwards platformInstance.passThruList = cpArrayNew(0); shape->data = &platformInstance; // 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, -200); body->v = cpv(0, 170); shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); shape->e = 0.0f; shape->u = 0.9f; shape->collision_type = 2; cpSpaceAddCollisionHandler(space, 1, 2, NULL, preSolve, NULL, NULL, NULL); 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) { 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 cpBody * addWheel(cpVect pos, cpVect boxOffset) { cpFloat radius = 15.0f; cpFloat mass = 1.0f; cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); body->p = cpvadd(pos, boxOffset); cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); shape->e = 0.0f; shape->u = 0.7f; shape->group = 1; // use a group to keep the car parts from colliding return body; }
static cpBody * addBall(cpVect pos, cpVect boxOffset) { cpFloat radius = 15.0f; cpFloat mass = 1.0f; cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); cpBodySetPos(body, cpvadd(pos, boxOffset)); cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.7f); return body; }
static projectile_t *createProjectile(cpFloat radius, cpFloat mass) { /* just initialize a projectile. projectiles are added to the space when fired */ projectile_t *p; p = (projectile_t *)malloc(sizeof(projectile_t)); p->body = cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)); p->shape = cpCircleShapeNew(p->body, radius, cpvzero); p->shape->collision_type = PROJECTILE_TYPE; p->shape->e = 1.0f; p->shape->u = 1.0f; return p; }
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; }
static cpBody * addWheel(cpVect pos, cpVect boxOffset) { cpFloat radius = 15.0f; cpFloat mass = 1.0f; cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); cpBodySetPos(body, cpvadd(pos, boxOffset)); cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.7f); cpShapeSetGroup(shape, 1); // use a group to keep the car parts from colliding return body; }
iBOOL mwInitLevel004( void ) { mwInitSpace(CurrLevelNum); cpBody *body;//, *staticBody = &(mwSpace->staticBody); cpShape *shape; cpFloat radius = WATER_RADIUS; for(int i=0; i<3; i++){ for(int j=0; j<10; j++){ cpVect pos = cpv((j)*6+15,(i)*4+90); body = cpSpaceAddBody(mwSpace, cpBodyNew(40.0f, cpMomentForCircle(40.0f, 0.0f, radius, cpvzero))); cpBodySetPos(body, pos); cpBodySetID(body,i*50+j); body->v = cpv(body->v.x,-(cpfabs(body->v.y))); shape = cpSpaceAddShape(mwSpace, cpCircleShapeNew(body, radius, cpvzero)); shape->e = WATER_E; shape->u = WATER_U; } } mwLevelWordInit(CurrLevelNum); mwLevelDuckInit(CurrLevelNum); mwLevelPipeInit(CurrLevelNum); mwLevelWaitInit(CurrLevelNum); mwLevelAlgaeInit(CurrLevelNum); mwLevelDrawBKwall(); i51KitG2DrawImage(mwContainer, MudLevel004, iNULL, iNULL); i51KitG2DrawImage(mwContainer, RockLevel004, iNULL, iNULL); mwLevelDrawPause(); mwLevelDrawRestart(); i51AdeStdMemcpy16( (void *)TempScreenBuf, (void *)mwScreenBuffer, SCREEN_HEIGHT*SCREEN_WIDTH*2); mwLevelSpeedKeepInit(CurrLevelNum); mwTempScreenBufUpdate(CurrLevelNum); mwLevelDrawDuck(); TimeLevelStart = i51AdeOsGetTick(); mwLevelOrganInit(CurrLevelNum); return iTRUE; }
Boule creerBoule() { cpFloat radius = rand() % 16 + 25;// radius entre 25 et 40 cpFloat mass = rand() % 3 + 1;// masse entre 1 et 3; cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero); cpBody *ballBody = cpSpaceAddBody(espace, cpBodyNew(mass, moment)); cpBodySetPos(ballBody, cpv(0, 310)); cpShape *ballShape = cpSpaceAddShape(espace, cpCircleShapeNew(ballBody, radius, cpvzero)); cpShapeSetFriction(ballShape, -0.2); Boule laBoule={radius, cpBodyGetPos(ballBody).x,0,rand() % 136 + 150,rand() % 136 + 100,rand() % 136 + 50,255,ballShape,ballBody, genChar(),FALSE}; return laBoule; }
/* calculate moment for a single shape */ static Scalar _moment(cpBody *body, ShapeInfo *shapeInfo) { Scalar mass = cpBodyGetMass(body); switch (shapeInfo->type) { case PS_CIRCLE: return cpMomentForCircle(mass, 0, cpCircleShapeGetRadius(shapeInfo->shape), cpCircleShapeGetOffset(shapeInfo->shape)); case PS_POLYGON: return cpMomentForPoly(mass, cpPolyShapeGetNumVerts(shapeInfo->shape), ((cpPolyShape *) shapeInfo->shape)->verts, cpvzero); } }
void Unit::updatePhysics( ) { cpBodySetMass( physBody, phys.mass ); cpFloat moment; switch(phys.type){ case potCircle: if( physShape ) cpCircleShapeSetRadius( physShape, phys.radius ); moment = cpMomentForCircle( phys.mass, 0, phys.radius, cpvzero ); break; default: moment = 1.0; break; } cpBodySetMoment( physBody, moment ); if( cpBodyIsStatic(physBody) && phys.type != potNone ) cpSpaceReindexShape( Phys::space, physShape ); }
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; }
static void update(int ticks) { if(arrowDirection.y){ circleRadius = cpfmax(10.0f, circleRadius + arrowDirection.y); for(int i=0; i<NUM_CIRCLES; i++){ circles[i]->body->m = cpMomentForCircle(1.0f, 0.0f, circleRadius, cpvzero); cpCircleShapeSetRadius(circles[i], circleRadius); } } int steps = 1; cpFloat dt = 1.0f/60.0f/(cpFloat)steps; for(int i=0; i<steps; i++){ cpSpaceStep(space, dt); } }
/* Preparing the tiles * ------------------- * * We traverse the tilemap string, detecting any "lit" tile * using the character #. For each "lit" tile, we create a * Chipmunk2D body and position it in screen coordinates. */ static void init_tiles(struct state* state) { cpFloat radius = state->tile_img->width / 2; cpFloat mass = 1; cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero); for (unsigned int i = 0; i < strlen(tile_map); i++) { if (tile_map[i] == '#') { state->tiles[i].is_active = true; state->tiles[i].body = cpSpaceAddBody(state->space, cpBodyNew(mass, moment)); state->tiles[i].shape = cpSpaceAddShape( state->space, cpCircleShapeNew(state->tiles[i].body, radius, cpvzero)); cpShapeSetFriction(state->tiles[i].shape, 0.7); cpVect pp = cpv((i % 54) * 6 + 4, (i / 54) * 6 + 40); cpBodySetPosition(state->tiles[i].body, pp); } else state->tiles[i].is_active = false; } }
static void update(int ticks) { int steps = 1; cpFloat dt = 1.0f/60.0f/(cpFloat)steps; if(!emitterInstance.blocked && emitterInstance.queue){ emitterInstance.queue--; cpBody *body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForCircle(1.0f, 15.0f, 0.0f, cpvzero))); body->p = emitterInstance.position; body->v = cpvmult(cpv(frand_unit(), frand_unit()), 100.0f); cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, 15.0f, cpvzero)); shape->collision_type = BALL_TYPE; } for(int i=0; i<steps; i++){ cpSpaceStep(space, dt); } }
static cpSpace * init(void) { cpResetShapeIdCounter(); space = cpSpaceNew(); space->iterations = 5; space->gravity = cpv(0, -100); cpSpaceResizeStaticHash(space, 40.0f, 999); cpSpaceResizeActiveHash(space, 30.0f, 2999); cpBody *body, *staticBody = &space->staticBody; cpShape *shape; shape = cpSpaceAddStaticShape(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 = cpSpaceAddStaticShape(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 = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; for(int i=0; i<NUM_CIRCLES; i++){ body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForCircle(1.0f, 0.0f, circleRadius, cpvzero))); body->p = cpvmult(cpv(frand()*2.0f - 1.0f, frand()*2.0f - 1.0f), circleRadius*5.0f); circles[i] = shape = cpSpaceAddShape(space, cpCircleShapeNew(body, circleRadius, cpvzero)); shape->e = 0.0f; shape->u = 1.0f; } strcat(messageString, "chipmunk_unsafe.h Contains functions for changing shapes, but they can cause severe stability problems if used incorrectly.\n" "Shape changes occur as instantaneous changes to position without an accompanying velocity change. USE WITH CAUTION!"); return space; }