box2d icon indicating copy to clipboard operation
box2d copied to clipboard

Debug draw?

Open ntop001 opened this issue 7 years ago • 1 comments

I see some code about debug draw, but they are commented out. Is there any preferred way to draw debug shapes?

ntop001 avatar Jun 27 '18 05:06 ntop001

For any one who has the same questions. I wrote some code to draw debug shape/joint/boundingbox:

// debug draw
func (b2 *B2System) DebugDraw() {
	w := b2.world
	if b2.debug.shape { // shape
		for b := w.GetBodyList(); b != nil; b = b.GetNext() {
			xf := b.GetTransform()
			for f := b.GetFixtureList(); f != nil; f = f.GetNext() {
				drawShape(f, xf)
			}
		}
	}

	if b2.debug.joint { // joint
		for j := w.GetJointList(); j != nil; j = j.GetNext() {
			drawJoint(j)
		}
	}

	if b2.debug.bounding { // bounding-box
		bp := &w.M_contactManager.M_broadPhase
		for b := w.GetBodyList(); b != nil; b = b.GetNext() {
			if !b.IsActive() {
				continue
			}
			for f := b.GetFixtureList(); f != nil; f = f.GetNext() {
				for i := 0; i < f.M_proxyCount; i++ {
					proxy := f.M_proxies[i]
					aabb := bp.GetFatAABB(proxy.ProxyId)
					vs := [4]box2d.B2Vec2{}
					vs[0] = box2d.B2Vec2{aabb.LowerBound.X, aabb.LowerBound.Y}
					vs[1] = box2d.B2Vec2{aabb.UpperBound.X, aabb.LowerBound.Y}
					vs[2] = box2d.B2Vec2{aabb.UpperBound.X, aabb.UpperBound.Y}
					vs[3] = box2d.B2Vec2{aabb.LowerBound.X, aabb.UpperBound.Y}
					drawPolygon(vs[:])
				}
			}
		}
	}
}

Here, you have to implement drawShapedrawJoint and drawPolygon method. In my engine , it's easy to use dbg system to implement these method.

ntop001 avatar Jul 03 '18 14:07 ntop001