Example #1
0
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));
	}
}
static void
update(cpSpace *space, double dt)
{
    cpSpaceStep(space, dt);

    ChipmunkDemoPrintString("Place objects on the scale to weigh them. The ball marks the shapes it's sitting on.\n");

    // Sum the total impulse applied to the scale from all collision pairs in the contact graph.
    // If your compiler supports blocks, your life is a little easier.
    // You can use the "Block" versions of the functions without needing the callbacks above.
#if USE_BLOCKS
    __block cpVect impulseSum = cpvzero;
    cpBodyEachArbiter_b(scaleStaticBody, ^(cpArbiter *arb) {
        impulseSum = cpvadd(impulseSum, cpArbiterTotalImpulse(arb));
    });