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; }
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; }
static cpSpace * init(void) { ChipmunkDemoMessageString = "Right click to make pentagons static/dynamic."; cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 5); cpSpaceSetGravity(space, cpv(0, -100)); cpBody *body, *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; // Vertexes for a triangle shape. cpVect tris[] = { cpv(-15,-15), cpv( 0, 10), cpv( 15,-15), }; // Create the static triangles. for(int i=0; i<9; i++){ for(int j=0; j<6; j++){ cpFloat stagger = (j%2)*40; cpVect offset = cpv(i*80 - 320 + stagger, j*70 - 240); shape = cpSpaceAddShape(space, cpPolyShapeNew(staticBody, 3, tris, offset)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); } } // Create vertexes for a pentagon shape. cpVect verts[NUM_VERTS]; for(int i=0; i<NUM_VERTS; i++){ cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS); verts[i] = cpv(10*cos(angle), 10*sin(angle)); } pentagon_mass = 1.0; pentagon_moment = cpMomentForPoly(1.0f, NUM_VERTS, verts, cpvzero); // Add lots of pentagons. for(int i=0; i<300; i++){ body = cpSpaceAddBody(space, cpBodyNew(pentagon_mass, pentagon_moment)); cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320; cpBodySetPos(body, cpv(x, 350)); shape = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.4f); } 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 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); }
__declspec( dllexport ) void push( const void * _in, int in_size, void * _out, int out_sz ) { int index; Variable *var; cpBody *body; cpShape *shape; index = PEEKINT(INPUT_MEMBLOCK,0); var = vhGetVariable(&mVariableHandler,index); switch (var->mType) { case VarTypeBody: body = (cpBody*)var->mPtr; cpBodySetAngle(body,degToRad(PEEKFLOAT(INPUT_MEMBLOCK,4))); cpBodySetPos(body,PEEKVECT(INPUT_MEMBLOCK,8)); cpBodySetAngVel(body,degToRad(PEEKFLOAT(INPUT_MEMBLOCK,16))); cpBodySetVel(body,PEEKVECT(INPUT_MEMBLOCK,20)); break; case VarTypeShape: shape = (cpShape*)var->mPtr; cpShapeSetFriction(shape,PEEKFLOAT(INPUT_MEMBLOCK,4)); cpShapeSetElasticity(shape,PEEKFLOAT(INPUT_MEMBLOCK,8)); cpShapeSetLayers(shape,PEEKUINT(INPUT_MEMBLOCK,12)); cpShapeSetGroup(shape,PEEKUINT(INPUT_MEMBLOCK,16)); break; } }
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; }
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; }
bool PhysicsShapeEdgeChain::init(const Vec2* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/) { cpVect* vec = nullptr; do { _type = Type::EDGECHAIN; vec = new (std::nothrow) cpVect[count]; PhysicsHelper::points2cpvs(points, vec, count); int i = 0; for (; i < count - 1; ++i) { auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[i + 1], border); CC_BREAK_IF(shape == nullptr); cpShapeSetUserData(shape, this); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); addShape(shape); } CC_SAFE_DELETE_ARRAY(vec); CC_BREAK_IF(i < count - 1); _mass = PHYSICS_INFINITY; _moment = PHYSICS_INFINITY; setMaterial(material); return true; } while (false); CC_SAFE_DELETE_ARRAY(vec); return false; }
// adds static box shape to space static void core_add_static_box_shape ( cpSpace * space, Box * box ) { // add box collision shape to body cpFloat hw = box->width / 2.0; cpFloat hh = box->height / 2.0; double x = box->x; double y = box->y; cpVect verts[] = { cpv ( x - hw, y - hh ), cpv ( x - hw, y + hh ), cpv ( x + hw, y + hh ), cpv ( x + hw, y - hh ), }; cpShape * box_shape = cpPolyShapeNew ( space->staticBody, 4, verts, cpvzero ); cpShapeSetFriction ( (cpShape *) box_shape, box->friction ); cpShapeSetElasticity ( (cpShape *) box_shape, box->elasticity ); cpSpaceAddPostStepCallback ( space, (cpPostStepFunc) postStepAddShape, box_shape, NULL ); DrawShapeInfo *info = add_box_draw_shape_info ( box ); box_shape->data= ( cpDataPointer ) info; }
void PhysicsShape::setElasticity(qreal v) { m_elasticity = v; if(m_isPhysicsCreated) { cpShapeSetElasticity(m_shape, m_elasticity); } }
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"); }
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); } }
void PhysicsShape::setRestitution(float restitution) { _material.restitution = restitution; for (cpShape* shape : _cpShapes) { cpShapeSetElasticity(shape, PhysicsHelper::float2cpfloat(restitution)); } }
void PhysicsShape::setRestitution(float restitution) { _material.restitution = restitution; for (cpShape* shape : _cpShapes) { cpShapeSetElasticity(shape, restitution); } }
static int l_physics_setShapeElasticity(lua_State* state) { l_physics_Shape* shape = (l_physics_Shape*)lua_touserdata(state, 1); float value = l_tools_toNumberOrError(state, 2); cpShapeSetElasticity(shape->shape, value); return 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; }
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; }
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); }
// adds static circle shape to space static void core_add_static_circle_shape ( cpSpace * space, Circle * circ ) { cpVect offset = cpv ( circ->x, circ->y ); // add circle collision shape to space cpShape * circ_shape = cpCircleShapeNew ( space->staticBody, circ->radius, offset ); cpShapeSetFriction ( (cpShape *) circ_shape, circ->friction ); cpShapeSetElasticity ( (cpShape *) circ_shape, circ->elasticity ); cpSpaceAddPostStepCallback ( space, (cpPostStepFunc) postStepAddShape, circ_shape, NULL ); DrawShapeInfo *info = add_circle_draw_shape_info ( circ ); circ_shape->data= ( cpDataPointer ) info; }
// adds nonstatic box shape to space // currently does not roate box static cpShape *core_add_box_shape ( cpSpace *space, Box *box, const int index ) { // calculate mass and moment of a box cpFloat mass = box->density * box->width * box->height; cpFloat moment = cpMomentForBox ( mass, box->width, box->height ); // add body with mass and moment of a square to space cpBody *body = cpBodyNew ( mass, moment ); cpSpaceAddPostStepCallback ( space, (cpPostStepFunc) postStepAddBody, body, NULL ); cpBodySetPos ( body, cpv ( box->x, box->y ) ); // set index of body BodyInfo * bi = body_info_new(0); bi->index = index; bi->type = BOX_TYPE; bi->p1x = box->x - (box->width) / 2.0; bi->p1y = box->y + (box->height) / 2.0; bi->p2x = box->x + (box->width) / 2.0;; bi->p2y = box->y - (box->height) / 2.0; bi->color->r = box->color->r; bi->color->g = box->color->g; bi->color->b = box->color->b; bi->friction = box->friction; bi->density = box->density; bi->elasticity = box->elasticity; body->data = bi; double hw = ( box->width ) / 2.0; double hh = ( box->height ) / 2.0; cpVect cpv1 = cpv ( -hw,-hh ); cpVect cpv2 = cpv ( -hw, hh ); cpVect cpv3 = cpv ( hw, hh ); cpVect cpv4 = cpv ( hw, -hh ); cpVect verts [4]= { cpv1, cpv2, cpv3, cpv4 }; // add box collision shape to body cpShape *boxShape = cpPolyShapeNew ( body, 4, verts, cpv ( 0, 0 ) ); cpSpaceAddPostStepCallback ( space, (cpPostStepFunc) postStepAddShape, boxShape, NULL ); cpShapeSetFriction ( boxShape, box->friction ); cpShapeSetElasticity ( boxShape, box->elasticity ); cpBodySetAngle ( body, box->angle); DrawShapeInfo *info = add_box_draw_shape_info ( box ); boxShape->data= ( cpDataPointer ) info; return boxShape; }
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 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); }
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; }
static cpBody * addBar(cpVect pos, cpVect boxOffset) { cpFloat mass = 2.0f; cpVect a = cpv(0, 30); cpVect b = cpv(0, -30); cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b))); cpBodySetPos(body, cpvadd(pos, boxOffset)); cpShape *shape = cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 5.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.7f); return body; }
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)); }
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; }
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")); } }
void PhysicsShape::componentComplete() { if(m_shapeType == PhysicsTypes::Box) { cpVect niz[] = { cpv(-(this->width()/2.0f), -(this->height()/2.0f)), cpv(-(this->width()/2.0f), (this->height()/2.0f)), cpv( (this->width()/2.0f), (this->height()/2.0f)), cpv( (this->width()/2.0f), -(this->height()/2.0f)) }; m_shape = cpPolyShapeNew(NULL, 4, niz, cpv(m_offsetX, -m_offsetY)); } else if(m_shapeType == PhysicsTypes::Circle) { m_shape = cpCircleShapeNew(NULL, m_diameter/2, cpvzero); } else { //TODO error qDebug() << "Error circle"; } m_shape->data = this; cpShapeSetFriction(m_shape, m_friction); cpShapeSetElasticity(m_shape, m_elasticity); if(m_sensor) { cpShapeSetSensor(m_shape, true); } cpShapeSetLayers(m_shape, m_layer); m_isPhysicsCreated = true; m_world->addPhysicsInterface(this); if(m_debugDraw) { this->setFlag(QQuickItem::ItemHasContents, true); } if(m_debugPrint) { qDebug() << "PhysicsShape - componentComplete()"; qDebug() << " Is sensor:" << m_sensor; qDebug() << " Shape type:" << m_shapeType; qDebug() << " Name:" << m_name; qDebug() << " Width:" << this->width(); qDebug() << " Height:" << this->height(); qDebug() << " QQuickitem pos:" << this->x() << this->y(); qDebug() << " Offset pos:" << this->m_offsetX << this->m_offsetY; } QQuickItem::componentComplete(); }