void PhysicsDebugDraw::drawShape(PhysicsShape& shape) { const Color4F fillColor(1.0f, 0.0f, 0.0f, 0.3f); const Color4F outlineColor(1.0f, 0.0f, 0.0f, 1.0f); for (auto it = shape._cpShapes.begin(); it != shape._cpShapes.end(); ++it) { cpShape *subShape = *it; switch ((*it)->klass_private->type) { case CP_CIRCLE_SHAPE: { float radius = PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(subShape)); Vec2 centre = PhysicsHelper::cpv2point(cpBodyGetPos(cpShapeGetBody(subShape))); Vec2 offset = PhysicsHelper::cpv2point(cpCircleShapeGetOffset(subShape)); Vec2 rotation(PhysicsHelper::cpv2point(cpBodyGetRot(cpShapeGetBody(subShape)))); centre += offset.rotate(rotation); static const int CIRCLE_SEG_NUM = 12; Vec2 seg[CIRCLE_SEG_NUM] = {}; for (int i = 0; i < CIRCLE_SEG_NUM; ++i) { float angle = (float)i * M_PI / (float)CIRCLE_SEG_NUM * 2.0f; Vec2 d(radius * cosf(angle), radius * sinf(angle)); seg[i] = centre + d; } _drawNode->drawPolygon(seg, CIRCLE_SEG_NUM, fillColor, 1, outlineColor); break; } case CP_SEGMENT_SHAPE: { cpSegmentShape *seg = (cpSegmentShape *)subShape; _drawNode->drawSegment(PhysicsHelper::cpv2point(seg->ta), PhysicsHelper::cpv2point(seg->tb), PhysicsHelper::cpfloat2float(seg->r==0 ? 1 : seg->r), outlineColor); break; } case CP_POLY_SHAPE: { cpPolyShape* poly = (cpPolyShape*)subShape; int num = poly->numVerts; Vec2* seg = new (std::nothrow) Vec2[num]; PhysicsHelper::cpvs2points(poly->tVerts, seg, num); _drawNode->drawPolygon(seg, num, fillColor, 1.0f, outlineColor); delete[] seg; break; } default: break; } } }
// calculates and adds forces triggered by holding the movement keys down. // it also has to stop the forces applied by the prev call to this function void blastengines(struct objnode *player) { struct movement *thrust; cpFloat force, tforce; struct forces newf; cpVect rotv; cpFloat angvel; thrust = &player->pinfo->thrust; rotv = cpBodyGetRot(player->b); force = 0; // these test cases are to enforce soft limits on the rate of movement if (thrust->up && !thrust->down) { if (cpvunrotate(cpBodyGetVel(player->b), rotv).y < MAXVEL) force = FORCE; } else if (!thrust->up && thrust->down) { if (cpvunrotate(cpBodyGetVel(player->b), rotv).y > -MAXVEL) force = -FORCE; } tforce = 0; angvel = cpBodyGetAngVel(player->b); if (thrust->ccw && !thrust->cw) { if (angvel < MAXANGVEL) tforce += TFORCE; } else if (thrust->cw && !thrust->ccw) { if (angvel > -MAXANGVEL) tforce += -TFORCE; } newf.force = cpvrotate(cpv(0, force), rotv); newf.tforce = cpv(0, tforce); applyforces(player, thrust->prevf); applyforces(player, newf); thrust->prevf.force = cpvneg(newf.force); thrust->prevf.tforce = cpvneg(newf.tforce); }
cVect cBody::Rot() const { return tovect( cpBodyGetRot( mBody ) ); }
static int cpBody_getRotation (lua_State *L){ cpBody *b = check_cpBody(L, 1); push_cpVect(L, cpBodyGetRot(b)); return 2; }
cp::Vect Body::getRot(void) { return cpBodyGetRot(body); }