PhysicsShape* PhysicsWorld::getShape(const Vec2& point) const { cpShape* shape = cpSpaceNearestPointQueryNearest(this->_info->getSpace(), PhysicsHelper::point2cpv(point), 0, CP_ALL_LAYERS, CP_NO_GROUP, nullptr); return shape == nullptr ? nullptr : PhysicsShapeInfo::getMap().find(shape)->second->getShape(); }
PhysicsShape* PhysicsWorld::getShape(const Vec2& point) const { cpShape* shape = cpSpaceNearestPointQueryNearest(_cpSpace, PhysicsHelper::point2cpv(point), 0, CP_ALL_LAYERS, CP_NO_GROUP, nullptr); return shape == nullptr ? nullptr : s_physicsShapeMap.find(shape)->second; }
NearestResult physics_nearest(Vec2 point, Scalar max_dist) { cpNearestPointQueryInfo info; NearestResult res; if (!cpSpaceNearestPointQueryNearest(space, cpv_of_vec2(point), max_dist, CP_ALL_LAYERS, CP_NO_GROUP, &info)) { /* no result */ res.ent = entity_nil; return res; } res.ent = cpShapeGetUserData(info.shape); res.p = vec2_of_cpv(info.p); res.d = info.d; res.g = vec2_of_cpv(info.g); return res; }
static void update(cpSpace *space, double dt) { if(ChipmunkDemoRightDown){ cpShape *nearest = cpSpaceNearestPointQueryNearest(space, ChipmunkDemoMouse, 0.0, GRABABLE_MASK_BIT, CP_NO_GROUP, NULL); if(nearest){ cpBody *body = cpShapeGetBody(nearest); if(cpBodyIsStatic(body)){ cpSpaceConvertBodyToDynamic(space, body, pentagon_mass, pentagon_moment); cpSpaceAddBody(space, body); } else { cpSpaceRemoveBody(space, body); cpSpaceConvertBodyToStatic(space, body); } } } cpSpaceStep(space, dt); cpSpaceEachBody(space, &eachBody, NULL); }
static void draw(void) { ChipmunkDemoDefaultDrawImpl(); cpVect start = QUERY_START; cpVect end = ChipmunkDemoMouse; ChipmunkDebugDrawSegment(start, end, RGBAColor(0,1,0,1)); ChipmunkDemoPrintString("Query: Dist(%f) Point%s, ", cpvdist(start, end), cpvstr(end)); cpSegmentQueryInfo segInfo = {}; if(cpSpaceSegmentQueryFirst(space, start, end, CP_ALL_LAYERS, CP_NO_GROUP, &segInfo)){ cpVect point = cpSegmentQueryHitPoint(start, end, segInfo); // Draw red over the occluded part of the query ChipmunkDebugDrawSegment(point, end, RGBAColor(1,0,0,1)); // Draw a little blue surface normal ChipmunkDebugDrawSegment(point, cpvadd(point, cpvmult(segInfo.n, 16)), RGBAColor(0,0,1,1)); // Draw a little red dot on the hit point. ChipmunkDebugDrawPoints(3, 1, &point, RGBAColor(1,0,0,1)); ChipmunkDemoPrintString("Segment Query: Dist(%f) Normal%s", cpSegmentQueryHitDist(start, end, segInfo), cpvstr(segInfo.n)); } else { ChipmunkDemoPrintString("Segment Query (None)"); } cpNearestPointQueryInfo nearestInfo = {}; cpSpaceNearestPointQueryNearest(space, ChipmunkDemoMouse, 100.0, CP_ALL_LAYERS, CP_NO_GROUP, &nearestInfo); if(nearestInfo.shape){ // Draw a grey line to the closest shape. ChipmunkDebugDrawPoints(3, 1, &ChipmunkDemoMouse, RGBAColor(0.5, 0.5, 0.5, 1.0)); ChipmunkDebugDrawSegment(ChipmunkDemoMouse, nearestInfo.p, RGBAColor(0.5, 0.5, 0.5, 1.0)); // Draw a red bounding box around the shape under the mouse. if(nearestInfo.d < 0) ChipmunkDebugDrawBB(cpShapeGetBB(nearestInfo.shape), RGBAColor(1,0,0,1)); } }
cp::Shape *Space::nearestPointQueryNearest(const cp::Vect& point,cpFloat maxDistance,cpLayers layers,cpGroup group,cpNearestPointQueryInfo *out) { cpShape * temp = cpSpaceNearestPointQueryNearest(space,point,maxDistance,layers,group,out); return static_cast<cp::Shape *>(temp ? temp->data : 0); }