void KRSimulator2D::step(double time) { mCollisions.clear(); cpSpaceStep((cpSpace*)mCPSpace, time); if (mHasChangedAngle) { cpBodySetAngle((cpBody*)mCPStaticBody, mNextAngle); cpSpaceRehashStatic((cpSpace*)mCPSpace); mHasChangedAngle = false; } }
CDynamics2DCylinderEntity::~CDynamics2DCylinderEntity() { if(m_ptBody != NULL) { cpSpaceRemoveConstraint(m_cEngine.GetPhysicsSpace(), m_ptLinearFriction); cpSpaceRemoveConstraint(m_cEngine.GetPhysicsSpace(), m_ptAngularFriction); cpConstraintFree(m_ptLinearFriction); cpConstraintFree(m_ptAngularFriction); cpSpaceRemoveBody(m_cEngine.GetPhysicsSpace(), m_ptBody); cpBodyFree(m_ptBody); cpSpaceRemoveShape(m_cEngine.GetPhysicsSpace(), m_ptShape); } else { cpSpaceRemoveStaticShape(m_cEngine.GetPhysicsSpace(), m_ptShape); cpSpaceRehashStatic(m_cEngine.GetPhysicsSpace()); } cpShapeFree(m_ptShape); }
static void update(int ticks) { int steps = 3; cpFloat dt = 1.0f/60.0f/(cpFloat)steps; for(int i=0; i<steps; i++){ cpSpaceStep(space, dt); // Manually update the position of the static shape so that // the box rotates. cpBodyUpdatePosition(staticBody, dt); // Because the box was added as a static shape and we moved it // we need to manually rehash the static spatial hash. cpSpaceRehashStatic(space); } }
/** * Method that updates the simulated space in case of staticShapes's change * * @author Cantidio Oliveira Fontes * @since 03/10/2010 * @version 03/10/2010 * @details * When you need to change the position of a static shape you need * to call this method or the space won't update its collision detection data */ inline void updateStaticShapes() { cpSpaceRehashStatic(mSpace); }
void handleEditor(void) { int dir, i; cpVect maxv, minv, mousePos; palette_object_t *tmp; cpConstraint *mouseJoint; static cpShape *clickShape = NULL; static bool canRotate = false; static cpShape *controlPoint = NULL; unsigned int w, h; mouseJoint = atlMouseJoint(); if (atlLeftMouseDown()) { clickShape = cpSpacePointQueryFirst(g_Space, atlMouseClickPos(), GRABABLE_MASK_BIT, CP_NO_GROUP); if (clickShape && !mouseJoint) { if (clickShape->collision_type == PALETTE_TYPE) { pullObjectFromPalette(clickShape); } else if (clickShape->collision_type == CONTROL_POINT_TYPE) { controlPoint = clickShape; } else { grabObject(clickShape->body); } } } else { controlPoint = NULL; if (mouseJoint) { atlRemoveMouseJoint(g_Space); } } if (controlPoint) pullControlPoint(controlPoint); if (isKeyPressed(KEY_SPACE) && mouseJoint && canRotate) { canRotate = false; cpBodySetAngle(mouseJoint->b, cpBodyGetAngle(mouseJoint->b)+45.0f*(M_PI/180)); } else if (!isKeyPressed(KEY_SPACE)) { canRotate = true; } w = atlWindowWidth(); h = atlWindowHeight(); maxv = atlMouseToSpace(w, h); minv = cpv(maxv.x-w, maxv.y+h); mousePos = atlMousePos(); dir = 0; /* allow the mouse to scroll the screen at the edges of the X-axis. clamp it * to maxv.x and minv.x so the mouse being outside the app window doesn't * cause side scrolling inadvertently */ if (isKeyPressed(KEY_RIGHT) || (mousePos.x > maxv.x - 15 && mousePos.x < maxv.x)) dir = 1; if (isKeyPressed(KEY_LEFT) || (mousePos.x < minv.x + 15 && mousePos.x > minv.x)) dir = -1; /* when the editorBody is positioned at (0, 0) it can't move left any more */ if (editorBody->p.x < 0) { dir = 0; editorBody->p.x = 0; } if (dir) { editorBody->v = cpvadd(editorBody->v, cpvmult(cpv(1, 0), 30.0f*dir)); cpBodyActivate(editorBody); } else { editorBody->v = cpvzero; /* recalculate static palette objects' positions */ for (i = 0; i < MAX_PALETTE_SHAPES; ++i) { tmp = paletteObjects[i]; if (tmp) { tmp->body->p = cpv(minv.x+25, minv.y-(45*(i+1))); } } cpSpaceRehashStatic(g_Space); } }